Python :
Functions and Modules
Punit A. Lathiya
Assistant Professor
EC Department
GEC Rajkot
Syllabus : 3151108
Syllabus : 3151108
Teaching and Examination Scheme
Books
Online Courses
• https://www.coursera.org/learn/python-programming
• https://nptel.ac.in/courses/106/106/106106145/
• https://nptel.ac.in/courses/106/106/106106182/
Index
• Function in python
• Types
• Examples
• Importing Modules (External and Internals)
Function
• It is piece of code that runs only if it is called.
• Parameters can be passed to function, also there
may have return value.
• Function can be designed using def keyword.
Function Definition
Calling a Function
Function
Types:
(1) No argument and no return
(2) No Argument and single return
(3) arguments and single return
(4) arguments and multiple return
(5) Variable Length Arguments
Function with no argument , No return
Function without argument, With return
Function with argument and no return
Function with Argument and single
Return
Function with Argument and multiple
Return
Way of passing arguments
Position
1. Define function
def function_name( Formal arguments): Keyword
Statements
Default
Return
2. Calling function Variable length
function_name(Actual arguments)
Keyword
Variable length
Passing argument with position
print (“ Age2 : “, age + 5);
Age2 : 40
print (“ Age2 : “, age + 5);
Error : cant add string with int
Not Good Every time
Passing arguments with keyword
Use keyword while calling function Advantage : Sequence of
argument doesn’t matter
Default Arguments
Default value (18) taken if not passed
Passing Variable length arguments
Arguments pass as tuple
Keyworded Variable length arguments
You don’t know, which data is for what !!
Keyworded Variable length arguments
Parameter send as dictionary
• Local
• Global
Scope of Variable Tell Function to
use global
variable
Local
Global
• Local
• Global
Scope of Variable
• If we want both local and global variable with same name and we
want to change global variable inside function
Global
Local
Use Global Value
Passing List to Functions
• count even and odd number from List
Returning List from Functions
• Find and return even and odd
number from List
Returning List from Functions
• Find and return list of Fibonacci
numbers with length n
0, 1, 1, 2, 3, 5, 8, 13, 21,……
b=
s = a+b =
a=
Passing Tuple to function
It is different from variable
length arguments
Passing Dictionary to function
It is different from Keyworded
variable length arguments
Recursion (Function calling itself)
Lambda Function (anonymous function)
• Short (Single line)Function can be defined.
• A lambda function can take any number of
arguments, but can only have one expression.
• Syntax :
• Example:
Lambda Function (anonymous function)
Value passed to function
Function Name
Built-in Function: Filter( )
• The filter( ) function returns an iterator were the
items are filtered through a function to test if the
item is accepted or not.
• Syntax : filter(function, iterable_object)
• Iterable_object == List, Tuple, Dictionary
Built-in Function: Filter( )
Built-in Function: Filter( )
Built-in Function : Map( )
• The map( ) function executes a specified function
for each item in an iterable. The item is sent to the
function as a parameter.
• Syntax : map(function, iterable_object)
Built-in Function : Map( )
Result :
Built-in Function : Reduce
• Import reduce function from functools module
• It takes two value from sequence, do operation
and result will be obtained,
• Then apply same function to next element of
sequence and previously obtained answer.
• Syntax : reduce (function, iterable_object)
Built-in Function : Reduce
Modules
• Use import keyword to call external or internal
functions/class/variables defined in other file into
your file.
• Syntax:
▫ Import math
▫ From math import sin
▫ From math import *
Internal Modules
Time and date module
• Datetime module has 6 classes
▫ Date
▫ Time
▫ Datetime
▫ Timedelta
▫ Tzinfo
▫ Timezone
Time and date module
Time and date module
Special Variable __name__
• __name__ == __main__
• Mostly reflect file_name for the program you
execute. i.e. __main__
Special Variable __name__
Special Variable __name__
Module Imported
Special Variable __name__
Module Imported
Assignment
• Explain with example:
▫ How list can be passed to function
▫ How tuple can be passed to function
▫ How dictionary can be passed to function
• Explain variable length argument with example.
• Explain keyworded variable length argument with
example.
• Explain Difference between filter( ) and Map( ) function.
• Explain lambda function with example.
• Explain reduce function with example.
Assignment (Define function in each code)
1. Print Fibonacci series of n number
2. Find factorial of number given by user. (with/without
recurssion)
3. Find if number is prime or not.
4. Find occurrence of letter ‘a’ in the string passed.
5. Find occurrence of vowels in the string passed.
6. Make function to reverse string word wise
[Ex: “I am in GECRajkot” >> “GECRajkot in am I”]
7. Make function to find roots of quadratic equation
[Ex: ax2 + bx +c = 0 ➔ pass a,b,c to function and take return
value only if roots are real else print imaginary roots ]
Assignment
8. Write python program to find root of the
equation using newton-rapson method.
▫ Equation : x3 – 2x2 + 3 , initial guess x0 : -20,
threshold = 1E-3
▫ Algorithm :
Write function f(x) that returns : x3 – 2x2 + 3
Write function fd(x) that returns : 3x2 – 4x
Step1: New value = x = xold – f(xold)/fd(xold)
Step2 : Repeat step-1 until f(xold)/fd(xold) > threshold
Thank You