SlideShare a Scribd company logo
1 of 34
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

More Related Content

Similar to ESIT135: Unit 3 Topic: functions in python

Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
vrickens
 

Similar to ESIT135: Unit 3 Topic: functions in python (20)

functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Function
FunctionFunction
Function
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 

Recently uploaded

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 

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 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
  • 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. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon
  • 5.  In Python a 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 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
  • 8. def hello(): ''' Function to print Hello.'‘’ print("Hello") Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 9
  • 10. def hello(): ''' Function to print hello.'‘’ print("Hello") hello() Output Hello Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 10
  • 11.  1. Positional arguments.  2. Keyword arguments.  3. Default arguments.  4. Variable length arguments. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 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: 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
  • 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 21
  • 22.  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
  • 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. 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 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
  • 26.  User can create module as per requirement is called user defined module. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 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 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 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
  • 34. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 34