SlideShare a Scribd company logo
5
Most read
7
Most read
16
Most read
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 (20)

PPTX
Functions in Python
Kamal Acharya
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
C functions
University of Potsdam
 
PPTX
Python-Functions.pptx
Karudaiyar Ganapathy
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PDF
Java Thread Synchronization
Benj Del Mundo
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PDF
Tuples in Python
DPS Ranipur Haridwar UK
 
PDF
Python recursion
Prof. Dr. K. Adisesha
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPTX
Functions in python
colorsof
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PDF
Python exception handling
Mohammed Sikander
 
PPTX
Built in function
MD. Rayhanul Islam Sayket
 
PPTX
Conditional and control statement
narmadhakin
 
PPTX
This pointer
Kamal Acharya
 
PDF
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Functions in Python
Kamal Acharya
 
Modules and packages in python
TMARAGATHAM
 
Regular expressions in Python
Sujith Kumar
 
Python-Functions.pptx
Karudaiyar Ganapathy
 
Python Modules
Nitin Reddy Katkam
 
Python: Modules and Packages
Damian T. Gordon
 
Java Thread Synchronization
Benj Del Mundo
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Tuples in Python
DPS Ranipur Haridwar UK
 
Python recursion
Prof. Dr. K. Adisesha
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
Functions in python
colorsof
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Looping Statements and Control Statements in Python
PriyankaC44
 
Python exception handling
Mohammed Sikander
 
Built in function
MD. Rayhanul Islam Sayket
 
Conditional and control statement
narmadhakin
 
This pointer
Kamal Acharya
 
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 

Viewers also liked (13)

PDF
Function arguments In Python
Amit Upadhyay
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PDF
Descriptors In Python
Amit Upadhyay
 
ODP
Day2
Karin Lagesen
 
PDF
Functions
Marieswaran Ramasamy
 
PDF
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
PDF
Input and Output
Marieswaran Ramasamy
 
PPTX
Python datatype
건희 김
 
PDF
4. python functions
in4400
 
PDF
Functions in python
Ilian Iliev
 
PDF
Functions and modules in python
Karin Lagesen
 
PPTX
Print input-presentation
Martin McBride
 
Function arguments In Python
Amit Upadhyay
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Descriptors In Python
Amit Upadhyay
 
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
Input and Output
Marieswaran Ramasamy
 
Python datatype
건희 김
 
4. python functions
in4400
 
Functions in python
Ilian Iliev
 
Functions and modules in python
Karin Lagesen
 
Print input-presentation
Martin McBride
 
Ad

Similar to python Function (20)

PPTX
functions.pptxghhhhhhhhhhhhhhhffhhhhhhhdf
pawankamal3
 
PPTX
Functions in python3
Lakshmi Sarvani Videla
 
PDF
Python basic
Saifuddin Kaijar
 
PDF
Class 7a: Functions
Marc Gouw
 
PPTX
Functions in Python with all type of arguments
riazahamed37
 
PPTX
Working with functions.pptx. Hb.
sabarivelan111007
 
PDF
beginners_python_cheat_sheet_pcc_functions.pdf
GuarachandarChand
 
PPTX
Lecture 08.pptx
Mohammad Hassan
 
PPTX
UNIT 3 python.pptx
TKSanthoshRao
 
PPTX
functions new.pptx
bhuvanalakshmik2
 
PDF
Function in Python
Yashdev Hada
 
PPTX
2 Functions2.pptx
RohitYadav830391
 
PDF
functionnotes.pdf
AXL Computer Academy
 
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
PPTX
Py-slides-3 easyforbeginnerspythoncourse.pptx
mohamedsamydeveloper
 
PPTX
UNIT- 2 PPDS R20.pptx
ssuser688516
 
PPTX
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
PPTX
Python PCEP Function Parameters
IHTMINSTITUTE
 
PDF
Notes5
hccit
 
functions.pptxghhhhhhhhhhhhhhhffhhhhhhhdf
pawankamal3
 
Functions in python3
Lakshmi Sarvani Videla
 
Python basic
Saifuddin Kaijar
 
Class 7a: Functions
Marc Gouw
 
Functions in Python with all type of arguments
riazahamed37
 
Working with functions.pptx. Hb.
sabarivelan111007
 
beginners_python_cheat_sheet_pcc_functions.pdf
GuarachandarChand
 
Lecture 08.pptx
Mohammad Hassan
 
UNIT 3 python.pptx
TKSanthoshRao
 
functions new.pptx
bhuvanalakshmik2
 
Function in Python
Yashdev Hada
 
2 Functions2.pptx
RohitYadav830391
 
functionnotes.pdf
AXL Computer Academy
 
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
Py-slides-3 easyforbeginnerspythoncourse.pptx
mohamedsamydeveloper
 
UNIT- 2 PPDS R20.pptx
ssuser688516
 
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
Python PCEP Function Parameters
IHTMINSTITUTE
 
Notes5
hccit
 
Ad

Recently uploaded (20)

PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Design Thinking basics for Engineers.pdf
CMR University
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 

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!