2. Function is a reusable block of statements used to perform a specific task.
Function is execute only when we called.
Computer Engineering PSP Prof. S. S. Gawali 2
3. The idea of function is to put some commonly or repeatedly done tasks together and
make a function so that instead of writing the same code again and again for different
inputs, we can do the function calls to reuse code contained in it over and over again.
Computer Engineering PSP Prof. S. S. Gawali 3
4. There are two types of function in Python programming:
Standard library functions - These are built-in functions in Python that are
available to use.
Example: type(), input(), print(), int(), float()
User-defined functions - We can create our own functions based on our
requirements.
4
Computer Engineering PSP Prof. S. S. Gawali
5. In Python a function is defined using the def keyword:
Syntax:
5
Computer Engineering PSP Prof. S. S. Gawali
6. def - keyword used to declare a function
function_name - any name given to the function
parameters - are place holders that define the parameters that go into the function.
return (optional) - returns value from a function
Computer Engineering PSP Prof. S. S. Gawali 6
7. The terms parameter and argument can be used for the same thing: information that
are passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
7
Computer Engineering PSP Prof. S. S. Gawali
8. def hello():
''' Function to print Hello.'‘’
print("Hello")
Computer Engineering PSP Prof. S. S. Gawali 8
9. After creating a function we can call it by using the name of the function followed by
parenthesis containing parameters of that particular function.
Computer Engineering PSP Prof. S. S. Gawali 9
10. def hello():
''' Function to print hello.'‘’
print("Hello")
hello()
Output
Hello
Computer Engineering PSP Prof. S. S. Gawali 10
11. 1. Positional arguments.
2. Keyword arguments.
3. Default arguments.
4. Variable length arguments.
Computer Engineering PSP Prof. S. S. Gawali 11
12. A positional parameter/argument in Python is a parameter/ an argument whose position
matters in a function call.
Syntax: call_func(arg1, arg2, arg3)
Example: Define a function that shows info about a person given the name and age:
def p_info(name, age):
'''Person infrormation.'‘’
print(f"Hi, I am {name}. I am {age} years old.")
#Call this function with two positional arguments name and age.
p_info("Rina",31)
Output:
Hi, I am Rina. I am 31 years
old.
Computer Engineering PSP Prof. S. S. Gawali 12
13. A keyword argument in Python means that a function argument has a name label.
Syntax: call_func(arg_name=arg1)
Example: Define a function that shows info about a person given the name and age:
def p_info(name, age):
'''Person infrormation.'''
print(f"Hi, I am {name}. I am {age} years old.")
p_info(name="Siya",age=40)
p_info(age=31,name="Rina")
Output:
Hi, I am Siya. I am 40 years
old
Hi, I am Rina. I am 31 years
old
Computer Engineering PSP Prof. S. S. Gawali 13
14. Default arguments in Python represent the function arguments that will be used if no
arguments are passed to the function call.
The default arguments are represented as parameter_name = value in the function
definition.
Syntax: call_func(par_name=arg1)
Example: Define a function that shows info about a person given the name and age:
def p_info(name="Priya", age=20): # function creation with default parameter/ argument
'''Person infrormation.'''
print(f"Hi, I am {name}. I am {age} years old")
p_info(name="Siya",age=40)
p_info()
Output:
Hi, I am Siya. I am 40 years
old
Hi, I am Priya. I am 20 years
old
Computer Engineering PSP Prof. S. S. Gawali 14
15. Return statement use to return values.
Function can take input values as parameters and execute statements, and
returns output to caller with return statement.
Two way to return value from function:
Return a value
Return multiple values
Computer Engineering PSP Prof. S. S. Gawali 15
16. To let a function return a value, use the return statement:
Example:
def sq(x):
return x * x
print(sq(3))
print(sq(5))
print(sq(9))
Output:
9
25
81
Computer Engineering PSP Prof. S. S. Gawali 16
17. In python, a function can return any number of values.
Example:
def sq_cu(x):
return x * x, x*x*x
print(sq_cu(10)
a,b=sq_cu(5)
print(a)
print(b)
Output:
(100,1000)
25
125
Computer Engineering PSP Prof. S. S. Gawali 17
18. Variable: Variables are the containers for storing data values.
Scope of Variable: The location where we can find a variable and also access it if
required is called the scope of a variable.
Based on the scope, we can classify Python variables into three types:
1. Local Variables
2. Global Variables
Computer Engineering PSP Prof. S. S. Gawali 18
19. The variables which are declared outside of function are called global variables.
These variables can be accessed in all functions of that module.
Example: Create global variable to Print Square and cube of number using function.
a=5 # a is global variable
def sq():
print(a*a)
def cu():
print(a*a*a)
sq()
cu()
Output
25
125
Computer Engineering PSP Prof. S. S. Gawali 19
20. The variables which are declared inside a function are called local variables.
Local variables are available only for the function in which we declared it. i.e from
outside of the
function we cannot access.
Example: Create a function to display the square of numbers using local variable.
def sq():
a=5 # a is local variable
print(a*a)
sq()
Output
25
Computer Engineering PSP Prof. S. S. Gawali 20
21. Sometimes we can declare a function without any name, such types of nameless
functions are called anonymous functions or lambda functions.
The main purpose of the anonymous function is just for instant use (i.e for one-time
usage)
Computer Engineering PSP Prof. S. S. Gawali 21
22. We can define by using lambda keyword
Syntax:
lambda arg_list:expression
Example
lambda n:n*n # to calculate n square
Computer Engineering PSP Prof. S. S. Gawali 22
23. A Python module is a file containing Python definitions and statements.
A module can define functions, classes, and variables.
A module can also include runnable code.
Grouping related code into a module makes the code easier to understand and use.
It also makes the code logically organized.
Computer Engineering PSP Prof. S. S. Gawali 23
24. Built-in Modules.
User-defined Modules.
Computer Engineering PSP Prof. S. S. Gawali 24
25. One of the feature of Python is “rich standard library”.
This rich standard library contains lots of built-in modules.
Hence, it provides a lot of reusable code.
To name a few, Python contains modules like “math”, “sys”, “datetime”, “random”.
Computer Engineering PSP Prof. S. S. Gawali 25
26. User can create module as per requirement is called user defined module.
Computer Engineering PSP Prof. S. S. Gawali 26
27. The import keyword to import both built-in and user-defined modules in Python.
Different way to import module
Syntax 1: import module_name
Syntax 2: import module_name as rename_of_module
Syntax 3: from module_name import method/constant-variable
Syntax 4: from module_name import method1/constant-var1, method2/constant-
var2…..
Syntax 5: from module_name import *
Syntax6: from module_name import method/constant as rename_of_method/constant
Computer Engineering PSP Prof. S. S. Gawali 27
28. import module_name
Example:
import math
a=5
print(math.sqrt(a))
print(math.factorial(a))
print(math.pi)
Output:
2.23606797749979
120
3.141592653589793
Computer Engineering PSP Prof. S. S. Gawali 28
29. import module_name as rename_of_module
Example:
import math as m
a=int(input(“Enter no.”))
print(sqrt(a)) Output:
Enter no. 16
4.0
Computer Engineering PSP Prof. S. S. Gawali 29
30. from module_name import method/constant-variable
Example:
from math import sqrt
a=int(input(“Enter no.”))
print(sqrt(a))
Output:
Enter no. 16
4.0
Computer Engineering PSP Prof. S. S. Gawali 30
31. from module_name import method1/constant-variable1, method2/constant-
variable2…..
Example:
from math import sqrt, pi, pow
a=int(input(“Enter no.”))
print(sqrt(a))
print(pi)
print(pow(a,3))
Output:
Enter no.2
1.4142135623730951
3.141592653589793
8.0
Computer Engineering PSP Prof. S. S. Gawali 31
32. from module_name import *
Example:
from math import *
a=int(input(“Enter no.”))
print(sqrt(a))
print(pi)
print(pow(a,3))
Output:
Enter no.3
1.7320508075688772
3.141592653589793
27.0
Computer Engineering PSP Prof. S. S. Gawali 32
33. from module_name import method/constant as rename_of_method/constant
Example:
from math import factorial as f
a=int(input(“Enter no.”))
print(f(a)) Output:
Enter no.5
120
Computer Engineering PSP Prof. S. S. Gawali 33