Functions in Python-11
Functions in Python-11
Python Tutorial
Created by Mustafa Germec, PhD
9. Functions in Python
In Python, a function is a group of related statements that performs a specific task.
Functions help break our program into smaller and modular chunks. * As our program grows larger and
larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.
There are two types of functions :
Pre-defined functions
User defined functions
In Python a function is defined using the def keyword followed by the function name and parentheses ().
Keyword def that marks the start of the function header.
A function name to uniquely identify the function.
Function naming follows the same rules of writing identifiers in Python.
Parameters (arguments) through which we pass values to a function. They are optional.
A colon (:) to mark the end of the function header.
Optional documentation string (docstring) to describe what the function does.
One or more valid python statements that make up the function body.
Statements must have the same indentation level (usually 4 spaces).
An optional return statement to return a value from the function.
In [9]:
If you make the above operations with 5, the results will be -3, 13, 40, 0.625, 5, 0.
Out[9]:
In [10]:
1 help(process)
process(x)
In [11]:
1 process(3.14)
If you make the above operations with 3.14, the results will be -4.859999999999999, 11.14, 25.12, 0.39
25, 3.14, 0.0.
Out[11]:
In [2]:
59.370000000000005
59.370000000000005
Out[2]:
59.370000000000005
In [20]:
265
Variables
The input to a function is called a formal parameter.
A variable that is declared inside a function is called a local variable.
The parameter only exists within the function (i.e. the point where the function starts and stops).
localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 2/14
4.06.2022 15:22 09. functions_python - Jupyter Notebook
A variable that is declared outside a function definition is a global variable, and its value is accessible and
modifiable throughout the program.
In [5]:
1 # Define a function
2 def function(x):
3
4 # Take a local variable
5 y = 3.14
6 z = 3*x + 1.618*y
7 print(f'If you make the above operations with {x}, the results will be {z}.')
8 return z
9
10 with_golden_ratio = function(1.618)
11 print(with_golden_ratio)
If you make the above operations with 1.618, the results will be 9.934520000000001.
9.934520000000001
In [8]:
If you make the above operations with 3.14, the results will be 14.500520000000002.
14.500520000000002
In [9]:
If you make the above operations with 2.718, the results will be 13.23452.
Out[9]:
13.23452
In [10]:
Hello, Python!
Hello, World!
In [15]:
1 # Printing the function after a call indicates a None is the default return statement.
2 # See the following prontings what functions returns are.
3
4 print(msg1())
5 print(msg2())
Hello, Python!
None
Hello, World!
None
In [18]:
1 # Define a function
2 def strings(x, y):
3 return x + y
4
5 # Testing the function 'strings(x, y)'
6 strings('Hello', ' ' 'Python')
Out[18]:
'Hello Python'
Simplicity of functions
In [26]:
Out[26]:
37
In [27]:
Out[27]:
In [28]:
Out[28]:
37
In [29]:
Out[29]:
In [31]:
In [32]:
Out[32]:
1808.053
In [33]:
Out[33]:
In [44]:
The fermentation process was successful with the inulinase activity of 1800 U/mL from molasses using A
spergillus niger.
The fermentation process was unsuccessful with the inulinase activity of 785 U/mL from molasses using
Aspergillus niger. You should repeat the fermentation process.
In [50]:
Stirred-tank bioreactor
30°C temperature
1 vvm aeration
pH control at 5.0
In [53]:
You should not watch this film with the rating value of 5.5
You should watch this film with the rating value of 8.6
Global variables
Variables that are created outside of a function (as in all of the examples above) are known as global
variables.
Global variables can be used by everyone, both inside of functions and outside.
In [56]:
---------------------------------------------------------------------------
~\AppData\Local\Temp/ipykernel_21468/4270999454.py in <module>
8 lang(language)
----> 9 lang(global_var)
In [58]:
Variables in functions
The scope of a variable is the part of the program to which that variable is accessible.
Variables declared outside of all function definitions can be accessed from anywhere in the program.
Consequently, such variables are said to have global scope and are known as global variables.
In [76]:
In [77]:
---------------------------------------------------------------------------
~\AppData\Local\Temp/ipykernel_21468/2006816728.py in <module>
In [81]:
1 # When the global variable and local variable have the same name:
2
3 process = 'Continuous fermentation'
4
5 def fermentation(process_name):
6 process = 'Batch fermentation'
7 if process_name == process:
8 return '0.5 g/L/h.'
9 else:
10 return '0.25 g/L/h.'
11
12 print('The productiovity in continuous fermentation is', fermentation('Continuous fermentation'))
13 print('The productiovity in batch fermentation is', fermentation('Batch fermentation'))
14 print(f'My favourite process is {process}.')
When the number of arguments are unkknown for a function, then the arguments can be packet into a tuple or
a dictionary
In [84]:
Number of elements is 4
Aspergillus niger
inulinase
batch
Number of elements is 5
Saccharomyces cerevisia
ethanol
continuous
45% yield
carob
In [98]:
In [88]:
Substrate : Molasses
Product : Inulinase
Fermentation_mode : Batch
In [96]:
1780.053
0.577
1729
Doctsting in Functions
In [97]:
1 # Define a function
2 def addition(x, y):
3 """The following function returns the sum of two parameters."""
4 z = x+y
5 return z
6
7 print(addition.__doc__)
8 print(addition(3.14, 2.718))
5.8580000000000005
Recursive functions
In [103]:
In [107]:
1 # Define a function that gives the total of the first ten numbers
2 def total_numbers(number, sum):
3 if number == 11:
4 return sum
5 else:
6 return total_numbers(number+1, sum+number)
7
8 print('The total of first ten numbers is', total_numbers(1, 0))
Nested functions
In [111]:
25 ------->> 26
nonlocal function
In [112]:
In [117]:
Hi Mustafa