0% found this document useful (0 votes)
5 views17 pages

Info4 Chap5

Chapter 5 covers Python functions, explaining their purpose, creation, and usage, including the importance of avoiding repetitive code and enhancing modularity. It details the syntax for defining functions, calling them, using parameters, and the return statement, along with variable scope. Additionally, the chapter introduces modules and packages in Python, providing guidelines for importing and creating them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Info4 Chap5

Chapter 5 covers Python functions, explaining their purpose, creation, and usage, including the importance of avoiding repetitive code and enhancing modularity. It details the syntax for defining functions, calling them, using parameters, and the return statement, along with variable scope. Additionally, the chapter introduces modules and packages in Python, providing guidelines for importing and creating them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 5

Functions

2024/2025

C.SAYAH
Python functions

What’s a function?
• A function is a block of reusable code that performs a specific task.

Why do we use functions?


❖ Functions are used to :
• Avoid repetitive code (DRY Principal).
• Allow modularity by dividing a complex problem into smaller
and more manageable parts.
• Give a structure to code, making it easier to understand.
1
Creating a function in Python

To create a function use the keyword “def”.


Syntax
def functionName(Param1, Param2,…):
#Function body

I created a simple function named greet() that prints Hello, Welcome to the
section L2 ST C!.

2
Creating a function in Python

When naming a function in Python follow these rules :


▪ A function name must start with a letter (a-zA-Z) or “_”, followed by letters, numbers(0-9)
or “_”.
▪ A function name can’t start with a number.
▪ Doesn’t contain special characters only “_” allowed.
▪ Avoid python keywords (if, else, return, …).
▪ Stick to lowercase with underscores “snack_case” for readability.

3
Calling a function

• Creating a function doesn’t mean you are executing the code inside it. To do so you
have to call it using its name followed by parentheses.

4
Calling a function

• Code before the function call runs normally. When a function is called, the program
jumps into the function and executes its code.
• After the function finishes, the program continues with the next statement after
the function call.

5
Functions with parameters

• Information can be passed into functions as arguments.


• Arguments are specified after the function name inside the parentheses.
• You can add as many arguments as you want, separated by commas.

6
Default values of parameters

• In Python, default values allow you to define parameters with a pre-set values. If a
function is called without providing a value for those parameters, the default
values are used.
• To set a default value, use the symbol = followed by the value placed immediately
after the parameter’s name in the function definition.

7
The return statement

• A function can return a value using the return statement.


• The return statement sends back a result that can be stored in a variable.

8
Scope of variables

❖ A scope in Python refers to the region where a variable is defined and can be accessed.
There is two types of scope : global and local.

• Local scope
Variables declared inside a
function, accessible only
within that function.

• Global scope
Variables declared outside
any function, and can be
accessed inside or outside
the function.
9
Exercise : check even or odd

• Create a function called is_even that takes a single integer as parameter and
returns True if the number is even and False if it’s odd.
• If the user enters a negative number, return “invalid input”.

➢ Example of usage :

10
Exercise : check even or odd

Solution

11
Modules in Python
❖ A module in Python is simply a .py file that contains functions, variables and classes that
can be reused in other programs.

How to import and use a module ?


• Use the keyword import to import the module, and to access its functions, use a dot “.”
operator.

12
Modules in Python
❖ Renaming a module
You can rename a module using the keyword as followed by the alias. Aliases are useful to
shorten long module names.

❖ The importation method : from … import …


To import a specific function from a module, you use the method from…import…

13
Packages in Python
• A package is a collection of modules organized in a
directory. For Python to recognize a directory as a
package, it must contain a special file __init__.py.
• The __init__.py file can be empty or contain necessary
setup required for the package.
• The __init__.py file is always executed when you import a
package.

• To import a package, you use the keyword import followed by the


package name.
• If you want to import a specific module from a package you can use :
▪ import PackageName.Module Name
▪ from PackageName import ModuleName
14
Create your own package
❖ To create your own package :

• Create a new folder and give it a name (package name).

• Inside the folder, create a file named __init__.py.

• Inside the folder, create Python files (.py extension) for each module.

• Inside these modules, define multiple functions and variables as needed.

• Create a file main.py outside the package folder, and import the package
so you can use its modules.

15
Thank you!
Do you have questions?

You might also like