Prepared by,
Prof. S. S. Gawali
Computer Engineering
Sanjivani College of Engineering, Kopargaon-423 603
Department of Computer Engineering
Sanjivani Rural Education Society’s
 Function is a reusable block of statements used to perform a specific task.
 Function is execute only when we called.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 2
 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.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 3
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
 In Python a function is defined using the def keyword:
 Syntax:
5
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 6
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
def hello():
''' Function to print Hello.'‘’
print("Hello")
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 8
 After creating a function we can call it by using the name of the function followed by
parenthesis containing parameters of that particular function.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 9
def hello():
''' Function to print hello.'‘’
print("Hello")
hello()
Output
Hello
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 10
 1. Positional arguments.
 2. Keyword arguments.
 3. Default arguments.
 4. Variable length arguments.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 11
 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.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 12
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 13
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 14
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 15
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 16
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 17
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 18
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 19
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 20
 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)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 21
 We can define by using lambda keyword
 Syntax:
 lambda arg_list:expression
 Example
 lambda n:n*n # to calculate n square
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 22
 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.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 23
 Built-in Modules.
 User-defined Modules.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 24
 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”.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 25
 User can create module as per requirement is called user defined module.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 26
 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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 27
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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 28
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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 29
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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 30
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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 31
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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 32
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
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 33
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 34

ESIT135: Unit 3 Topic: functions in python

  • 1.
    Prepared by, Prof. S.S. Gawali Computer Engineering Sanjivani College of Engineering, Kopargaon-423 603 Department of Computer Engineering Sanjivani Rural Education Society’s
  • 2.
     Function isa reusable block of statements used to perform a specific task.  Function is execute only when we called. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 2
  • 3.
     The ideaof 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. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 3
  • 4.
     There aretwo 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
  • 5.
     In Pythona function is defined using the def keyword:  Syntax: 5 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
  • 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 6
  • 7.
     The termsparameter 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
  • 8.
    def hello(): ''' Functionto print Hello.'‘’ print("Hello") Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 8
  • 9.
     After creatinga function we can call it by using the name of the function followed by parenthesis containing parameters of that particular function. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 9
  • 10.
    def hello(): ''' Functionto print hello.'‘’ print("Hello") hello() Output Hello Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 10
  • 11.
     1. Positionalarguments.  2. Keyword arguments.  3. Default arguments.  4. Variable length arguments. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 11
  • 12.
     A positionalparameter/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. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 12
  • 13.
     A keywordargument 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 13
  • 14.
     Default argumentsin 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 14
  • 15.
     Return statementuse 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 15
  • 16.
     To leta 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 17
  • 18.
     Variable: Variablesare 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 18
  • 19.
     The variableswhich 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 19
  • 20.
     The variableswhich 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 20
  • 21.
     Sometimes wecan 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) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 21
  • 22.
     We candefine by using lambda keyword  Syntax:  lambda arg_list:expression  Example  lambda n:n*n # to calculate n square Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 22
  • 23.
     A Pythonmodule 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. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 23
  • 24.
     Built-in Modules. User-defined Modules. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 24
  • 25.
     One ofthe 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”. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 25
  • 26.
     User cancreate module as per requirement is called user defined module. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 26
  • 27.
     The importkeyword 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 27
  • 28.
    import module_name  Example: importmath a=5 print(math.sqrt(a)) print(math.factorial(a)) print(math.pi) Output: 2.23606797749979 120 3.141592653589793 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 28
  • 29.
    import module_name asrename_of_module  Example: import math as m a=int(input(“Enter no.”)) print(sqrt(a)) Output: Enter no. 16 4.0 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 29
  • 30.
    from module_name importmethod/constant-variable  Example: from math import sqrt a=int(input(“Enter no.”)) print(sqrt(a)) Output: Enter no. 16 4.0 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 30
  • 31.
    from module_name importmethod1/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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 32
  • 33.
    from module_name importmethod/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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 33
  • 34.
    Department of ComputerEngineering, Sanjivani College of Engineering, Kopargaon 34