SlideShare a Scribd company logo
PRESENTED BY,
RONAK RATHI & SAMIR
RAWAT.
OBJECTIVE
HOW TO DEFINE A FUNCTION
HOW TO CALL A FUNCTION
FUNCTION ARGUMENTS
FUNCTION RECURSION
FUNCTION
 FUNCTION IS A GROUP OF RELATED
STATEMENTS THAT PERFORM A SPECIFIC TASK
 FUNCTIONS HELP BREAK OUR PROGRAM INTO
SMALLER AND MODULAR CHUCKS
DEFINING FUNCTION
def marks the start of function
function name to uniquely
identify a function.
def function_name (parameter) :
Argument to pass a value in function
colon(:) to mark end of
function header
EXAMPLE OF FUNCTION
def greet (name):
print(“Hello,” + name +” . Good afternoon!”)
CALLING A FUNCTION
HOW TO CALL A FUNCTION?
Once we have defined a function, we can call it from
another function, program or even the Python prompt.
To call a function we simply type the function name
with appropriate parameters.
>>> greet(“everyone")
>>>Hello, everyone. Good afternoon!
EXAMPLE
def my_func():
x = 10
global y
y = 20
print("Value of y inside function:",x)
print("Value of y inside function:",y)
x = 20
y = 10
my_func()
print("Value of y outside function:",x)
print("Value of y outside function:",y)
THERE ARE TWO TYPES OF FUNCTION IN PYTHON
 BUILT-IN FUNCTION
Example: print(), input(), eval().
 USERDEFINE FUNCTION
Example: def my_addition(x,y):
sum = x + y
return sum
 FUNCTION BUILT WITHIN THE PYTHON.
 THERE ARE 68 BUILT-IN FUNCTION VERSION 3.4.
 CHANGES WITH RESPECT TO UPDATED VERSION.
VARIABLE
FUNCTION
ARGUMENTS
DEFAULT
ARGUMENT
KEYWORD
ARGUMENT
ARBITRARY
ARGUMENTS
DEFAULT ARGUMENTS
 Default value to function argument is passed using
assignment operator ‘ = ‘.
Example:
def greet(name, msg = "Good morning!"):
print("Hello, " name + ', ' + msg)
 Non-default argument cannot follow default argument
Example: def greet(msg = "Good morning!", name):
SyntaxError:
non-default argument follows default argument
KEYWORD ARGUMENTS
 When we call a function with some values, these
values get assigned to the arguments according to their
position .
 Keyword arguments follows positional argument.
EXAMPLE OF KEYWORD
ARGUMENT
 greet(name = "Bruce",msg = "How do you do?")
2 keyword arguments
 greet(msg = "How do you do?",name = "Bruce")
2 keyword arguments (out of order)
 greet("Bruce",msg = "How do you do?")
1 positional, 1 keyword argument
 greet(name="Bruce","How do you do?")
SyntaxError:
non-keyword arg after keyword arg
 If number of arguments unknown we use arbitrary
arguments
 ( * ) Asterisk before arguments define arbitrary arguments.
Example:
def greet(*names):
for name in names:
print("Hello",name)
>>>greet("Monica","Luke","Steve","John")
ARBITRARY ARGUMENTS
 Like other programming language in python, function
can call itself.
 Definition of recursion in python is same as other
programming lang. -> C, C++, C#, Java etc.
def recur_fact(x):
if x == 1:
return 1
else:
return (x * recur_fact(x-1))
num = int(input("Enter a number: "))
if num >= 1:
print("The factorial of", num, "is", recur_fact(num))
EXAMPLE OF RECURSION
 Function without function name.
 Function is defined using lambda keyword, hence
function is also called Lambda function.
 Used for short period in python.
 Can have any number of arguments but only single
expression.
 Specially used with built-in functions like filter(),
map() etc.
ANONYMOUS
 double = lambda x: x * 2 print(double(5))
 my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)
EXAMPLE OF LAMBDA
FUNCTION
RESOURCES
 www.programiz.com/python-programming
 https://wiki.python.org/moin/BeginnersGuide/Progra
mmers
THANK YOU
HAVE A NICE DAY!

More Related Content

What's hot

Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
Siddique Ibrahim
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
Edureka!
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Python for loop
Python for loopPython for loop
Python for loop
Aishwarya Deshmukh
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
junnubabu
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Shakti Singh Rathore
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 

What's hot (20)

Functions in python
Functions in pythonFunctions in python
Functions in python
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
Python for loop
Python for loopPython for loop
Python for loop
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Strings in C
Strings in CStrings in C
Strings in C
 
Python list
Python listPython list
Python list
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Python functions
Python functionsPython functions
Python functions
 

Viewers also liked

Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
Amit Upadhyay
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Descriptors In Python
Descriptors In PythonDescriptors In Python
Descriptors In Python
Amit Upadhyay
 
Day2
Day2Day2
Functions
FunctionsFunctions
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
Input and Output
Input and OutputInput and Output
Input and Output
Marieswaran Ramasamy
 
Python datatype
Python datatypePython datatype
Python datatype
건희 김
 
4. python functions
4. python   functions4. python   functions
4. python functions
in4400
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
Martin McBride
 

Viewers also liked (13)

Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 
Functions in python
Functions in python Functions in python
Functions in python
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Descriptors In Python
Descriptors In PythonDescriptors In Python
Descriptors In Python
 
Day2
Day2Day2
Day2
 
Functions
FunctionsFunctions
Functions
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Input and Output
Input and OutputInput and Output
Input and Output
 
Python datatype
Python datatypePython datatype
Python datatype
 
4. python functions
4. python   functions4. python   functions
4. python functions
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
 

Similar to python Function

Functions in python3
Functions in python3Functions in python3
Functions in python3
Lakshmi Sarvani Videla
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
Marc Gouw
 
Lecture 08.pptx
Lecture 08.pptxLecture 08.pptx
Lecture 08.pptx
Mohammad Hassan
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
bhuvanalakshmik2
 
Python : Functions
Python : FunctionsPython : Functions
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
RohitYadav830391
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
AXL Computer Academy
 
UNIT- 2 PPDS R20.pptx
UNIT- 2 PPDS R20.pptxUNIT- 2 PPDS R20.pptx
UNIT- 2 PPDS R20.pptx
ssuser688516
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function Parameters
IHTMINSTITUTE
 
Notes5
Notes5Notes5
Notes5hccit
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
Pedro Rodrigues
 
Viii session
Viii sessionViii session
Viii session
AliMohammad155
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
Acácio Oliveira
 

Similar to python Function (20)

Functions in python3
Functions in python3Functions in python3
Functions in python3
 
Python basic
Python basicPython basic
Python basic
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
Lecture 08.pptx
Lecture 08.pptxLecture 08.pptx
Lecture 08.pptx
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
 
UNIT- 2 PPDS R20.pptx
UNIT- 2 PPDS R20.pptxUNIT- 2 PPDS R20.pptx
UNIT- 2 PPDS R20.pptx
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function Parameters
 
Notes5
Notes5Notes5
Notes5
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Viii session
Viii sessionViii session
Viii session
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 

Recently uploaded

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 

python Function

  • 1. PRESENTED BY, RONAK RATHI & SAMIR RAWAT.
  • 2. OBJECTIVE HOW TO DEFINE A FUNCTION HOW TO CALL A FUNCTION FUNCTION ARGUMENTS FUNCTION RECURSION
  • 3. FUNCTION  FUNCTION IS A GROUP OF RELATED STATEMENTS THAT PERFORM A SPECIFIC TASK  FUNCTIONS HELP BREAK OUR PROGRAM INTO SMALLER AND MODULAR CHUCKS
  • 4. DEFINING FUNCTION def marks the start of function function name to uniquely identify a function. def function_name (parameter) : Argument to pass a value in function colon(:) to mark end of function header
  • 5. EXAMPLE OF FUNCTION def greet (name): print(“Hello,” + name +” . Good afternoon!”)
  • 6. CALLING A FUNCTION HOW TO CALL A FUNCTION? Once we have defined a function, we can call it from another function, program or even the Python prompt. To call a function we simply type the function name with appropriate parameters. >>> greet(“everyone") >>>Hello, everyone. Good afternoon!
  • 7. EXAMPLE def my_func(): x = 10 global y y = 20 print("Value of y inside function:",x) print("Value of y inside function:",y) x = 20 y = 10 my_func() print("Value of y outside function:",x) print("Value of y outside function:",y)
  • 8. THERE ARE TWO TYPES OF FUNCTION IN PYTHON  BUILT-IN FUNCTION Example: print(), input(), eval().  USERDEFINE FUNCTION Example: def my_addition(x,y): sum = x + y return sum
  • 9.  FUNCTION BUILT WITHIN THE PYTHON.  THERE ARE 68 BUILT-IN FUNCTION VERSION 3.4.  CHANGES WITH RESPECT TO UPDATED VERSION.
  • 11. DEFAULT ARGUMENTS  Default value to function argument is passed using assignment operator ‘ = ‘. Example: def greet(name, msg = "Good morning!"): print("Hello, " name + ', ' + msg)  Non-default argument cannot follow default argument Example: def greet(msg = "Good morning!", name): SyntaxError: non-default argument follows default argument
  • 12. KEYWORD ARGUMENTS  When we call a function with some values, these values get assigned to the arguments according to their position .  Keyword arguments follows positional argument.
  • 13. EXAMPLE OF KEYWORD ARGUMENT  greet(name = "Bruce",msg = "How do you do?") 2 keyword arguments  greet(msg = "How do you do?",name = "Bruce") 2 keyword arguments (out of order)  greet("Bruce",msg = "How do you do?") 1 positional, 1 keyword argument  greet(name="Bruce","How do you do?") SyntaxError: non-keyword arg after keyword arg
  • 14.  If number of arguments unknown we use arbitrary arguments  ( * ) Asterisk before arguments define arbitrary arguments. Example: def greet(*names): for name in names: print("Hello",name) >>>greet("Monica","Luke","Steve","John") ARBITRARY ARGUMENTS
  • 15.  Like other programming language in python, function can call itself.  Definition of recursion in python is same as other programming lang. -> C, C++, C#, Java etc.
  • 16. def recur_fact(x): if x == 1: return 1 else: return (x * recur_fact(x-1)) num = int(input("Enter a number: ")) if num >= 1: print("The factorial of", num, "is", recur_fact(num)) EXAMPLE OF RECURSION
  • 17.  Function without function name.  Function is defined using lambda keyword, hence function is also called Lambda function.  Used for short period in python.  Can have any number of arguments but only single expression.  Specially used with built-in functions like filter(), map() etc. ANONYMOUS
  • 18.  double = lambda x: x * 2 print(double(5))  my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(filter(lambda x: (x%2 == 0) , my_list)) print(new_list) EXAMPLE OF LAMBDA FUNCTION
  • 20. THANK YOU HAVE A NICE DAY!