SlideShare a Scribd company logo
7
Most read
8
Most read
15
Most read
Devashish Kumar
Faculty-IT
iNurture
FUNCTIONS
 A function is a block of organized, reusable code that is
used to perform a single, related action.
 Functions provide better modularity for the applications.
 Functions provide a high degree of code reusing.
RULES FOR DEFINING FUNCTION IN PYTHON
 Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
 Any input parameters or arguments should be placed within these parentheses.
We also define parameters inside these parentheses.
 The code block within every function starts with a colon (:) and is indented.
 The statement return [expression] exits a function, optionally passing back an
expression to the caller.
 A return statement with no arguments is the same as return None.
Function Syntax
The keyword
def
introduces a
function
definition.
Input Parameter is placed within the
parenthesis() and also define
parameter inside the parenthesis.
Return statement exits a
function block. And we can also
use return with no argument.
The code block
within every
function starts
with a colon(:) .
Functions in python slide share
PASSING ARGUMENTS TO FUNCTIONS
 In programming, there are two ways in which arguments can be passed to functions :-
 Pass by Value:
 Function creates a copy of the variable(Object in Python) passed to it as an argument.
 The actual object is not affected.
 Object is of immutable type,because immutable objects cannot be modified.
 Pass by Reference:
 The actual object is passed to the called function.
 All the changes made to the object inside the function affect its original value.
 Object is mutable type, as mutable objects can be changed, the passed objects are
updated.
EXAMPLES OF PASSING IMMUTABLE ARGUMENTS
OUTPUT: New memory address of ‘a’
inside the function.
Actual variable ‘a’ is not
changed.
EXAMPLES OF PASSING MUTABLE ARGUMENTS
Address is same
Value of my_list is
changed after
function call.
FUNCTION ARGUMENTS
DEFAULT ARGUMENT VALUES
 Default Argument- argument that assumes a default value if a value is not
provided in the function call for that argument.
 The default value is evaluated only once.
 EXAMPLE:
Output:
In this code, argument ‘b’
has given a default value.
ie(b=90)
When the value of ‘b’ is not passed
in function ,then it takes default
argument.
DEFAULT ARGUMENTS
 The default value is evaluated only once. This makes a difference when the
default is a mutable object such as a list, dictionary, or instances of most
classes.
 Example:
 if we don’t want the default value to be shared between subsequent calls,
then
 Example :
Output:
Output:
KEYWORD ARGUMENTS
 Keyword arguments are related to the function calls.
 Using keyword arguments in a function call, the caller identifies the arguments
by the parameter name.
 Allows to skip arguments or place them out of order, the Python interpreter
use the keywords provided to match the values with parameters.
 Example:
Output:
 Caller identifies the keyword
argument by the parameter name.
 Calling of the argument Order
doesn’t matter .
KEYWORD ARGUMENTS
 Example 1:
Output:
Example 2: Output:
ARBITRARY ARGUMENT LISTS
 Variable-Length Arguments: more arguments than we specified while defining
the function.
 These arguments are also called variable-length arguments .
 An asterisk (*) is placed before the variable name that holds the values of all non
keyword variable arguments.
 This tuple remains empty if no additional arguments are specified during the
function call.
 Syntax :
def function_name([formal_args], *var_args_tuple ):
statement 1……
statement 2……
return [expression]
This is called
arbitrary argument
and asterisk sign is
placed before the
variable name.
ARBITRARY ARGUMENT LISTS
 EXAMPLE 1:
Output: Output:
 EXAMPLE 2:
 Any formal parameter which
occur after the *args
parameter are keyword-only
arguments.
 If formal parameter are not
keyword argument then error
occurred.
UNPACKING ARGUMENT LISTS
 Arguments are already in a list or tuple, need to be unpacked for a function
call requiring separate positional arguments.
 For instance, the built-in range() function expects separate start and stop
arguments.
 If range() are not available separately, write the function call with the *-operator
to unpack the arguments out of a list or tuple.
 Example 1: Example 2:
Output:
In dictionary, we use ‘**’ for unpacking the
arguments
In tuples or lists, ‘*’ is
used to unpack the
arguments
LAMBDA EXPRESSIONS
 Small functions can be created with the lambda keyword also known as
Anonymous function.
 Used wherever functions object are required.
 These functions are called anonymous because they are not declared in the
standard manner by using the def keyword. .
 Syntax :
 EXAMPLE:
lambda [arg1 [,arg2,.....argn]]:expression
Output:
 The function returns the multiply of
two arguments.
 Lambda function is restricted to a
single expressions.
FUNCTION ANNOTATIONS
 Function annotations are completely optional metadata information about the
types used by user-defined functions.
 Annotations are stored in the “__annotations__” attribute .
 Parameter annotations are defined by a colon after the parameter name,
followed by an expression evaluating to the value of the annotation.
 Return annotations are defined by a literal ->, followed by an expression,
between the parameter list and the colon denoting the end of the def statement.
 Example:
Output:
Literal ‘->’ is used to
define return
annotations.
DOCUMENTATION STRINGS
 The first line should always be a short, concise summary of the object’s
purpose.
 The first line should begin with a capital letter and end with a period.
 If there are more lines in the documentation string, the second line should be
blank.
 The first non-blank line after the first line of the string determines the amount
of indentation for the entire documentation string.
 Example:
Output:
‘.doc’ is used to print the
documentation strings.
CONTRIBUTERS

More Related Content

PPTX
Python Functions
Mohammed Sikander
 
PDF
Strings in python
Prabhakaran V M
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPTX
List in Python
Siddique Ibrahim
 
PPTX
Functions in Python
Kamal Acharya
 
PPTX
File handling in Python
Megha V
 
PPSX
python Function
Ronak Rathi
 
PPTX
Regular expressions in Python
Sujith Kumar
 
Python Functions
Mohammed Sikander
 
Strings in python
Prabhakaran V M
 
Data Structures in Python
Devashish Kumar
 
List in Python
Siddique Ibrahim
 
Functions in Python
Kamal Acharya
 
File handling in Python
Megha V
 
python Function
Ronak Rathi
 
Regular expressions in Python
Sujith Kumar
 

What's hot (20)

PPTX
Python dictionary
Mohammed Sikander
 
PPTX
Functions in python
colorsof
 
PDF
Datatypes in python
eShikshak
 
PDF
Python basic
Saifuddin Kaijar
 
PPT
Abstract data types
Poojith Chowdhary
 
PPTX
Values and Data types in python
Jothi Thilaga P
 
PDF
Python set
Mohammed Sikander
 
PPTX
Array ppt
Kaushal Mehta
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Introduction to python
Ayshwarya Baburam
 
PPTX
Functions in Python
Shakti Singh Rathore
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPT
RECURSION IN C
v_jk
 
PPT
Python Programming ppt
ismailmrribi
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PDF
Python Flow Control
Mohammed Sikander
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PPTX
Conditional and control statement
narmadhakin
 
Python dictionary
Mohammed Sikander
 
Functions in python
colorsof
 
Datatypes in python
eShikshak
 
Python basic
Saifuddin Kaijar
 
Abstract data types
Poojith Chowdhary
 
Values and Data types in python
Jothi Thilaga P
 
Python set
Mohammed Sikander
 
Array ppt
Kaushal Mehta
 
Modules and packages in python
TMARAGATHAM
 
Introduction to python
Ayshwarya Baburam
 
Functions in Python
Shakti Singh Rathore
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
RECURSION IN C
v_jk
 
Python Programming ppt
ismailmrribi
 
Python libraries
Prof. Dr. K. Adisesha
 
Python Flow Control
Mohammed Sikander
 
Introduction to NumPy (PyData SV 2013)
PyData
 
Conditional and control statement
narmadhakin
 
Ad

Similar to Functions in python slide share (20)

PPTX
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
PDF
functions notes.pdf python functions and opp
KirtiGarg71
 
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
PDF
functionnotes.pdf
AXL Computer Academy
 
PPT
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
PPTX
Python programming - Functions and list and tuples
MalligaarjunanN
 
PPT
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
PDF
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
PPTX
Learn more about the concepts Functions of Python
PrathamKandari
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPT
Python programming variables and comment
MalligaarjunanN
 
PPTX
functions.pptx
KavithaChekuri3
 
PPTX
User defined function in C.pptx
Rhishav Poudyal
 
PPTX
Functions
Septi Ratnasari
 
PPTX
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
RuzelAmpoan1
 
PPT
User Defined Functions
Praveen M Jigajinni
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
functions new.pptx
bhuvanalakshmik2
 
PPTX
Functions in C++
home
 
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
functions notes.pdf python functions and opp
KirtiGarg71
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
functionnotes.pdf
AXL Computer Academy
 
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Python programming - Functions and list and tuples
MalligaarjunanN
 
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Learn more about the concepts Functions of Python
PrathamKandari
 
Functions in Python Programming Language
BeulahS2
 
Python programming variables and comment
MalligaarjunanN
 
functions.pptx
KavithaChekuri3
 
User defined function in C.pptx
Rhishav Poudyal
 
Functions
Septi Ratnasari
 
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
RuzelAmpoan1
 
User Defined Functions
Praveen M Jigajinni
 
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
AnuragBharti27
 
functions new.pptx
bhuvanalakshmik2
 
Functions in C++
home
 
Ad

More from Devashish Kumar (6)

PPTX
Python: Data Visualisation
Devashish Kumar
 
PPTX
Pandas csv
Devashish Kumar
 
PPTX
Data Analysis packages
Devashish Kumar
 
PPTX
Data Analysis in Python-NumPy
Devashish Kumar
 
PPTX
Introduction to Python Part-1
Devashish Kumar
 
PPTX
Cloud Computing Introductory-1
Devashish Kumar
 
Python: Data Visualisation
Devashish Kumar
 
Pandas csv
Devashish Kumar
 
Data Analysis packages
Devashish Kumar
 
Data Analysis in Python-NumPy
Devashish Kumar
 
Introduction to Python Part-1
Devashish Kumar
 
Cloud Computing Introductory-1
Devashish Kumar
 

Recently uploaded (20)

PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 

Functions in python slide share

  • 2. FUNCTIONS  A function is a block of organized, reusable code that is used to perform a single, related action.  Functions provide better modularity for the applications.  Functions provide a high degree of code reusing.
  • 3. RULES FOR DEFINING FUNCTION IN PYTHON  Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).  Any input parameters or arguments should be placed within these parentheses. We also define parameters inside these parentheses.  The code block within every function starts with a colon (:) and is indented.  The statement return [expression] exits a function, optionally passing back an expression to the caller.  A return statement with no arguments is the same as return None.
  • 4. Function Syntax The keyword def introduces a function definition. Input Parameter is placed within the parenthesis() and also define parameter inside the parenthesis. Return statement exits a function block. And we can also use return with no argument. The code block within every function starts with a colon(:) .
  • 6. PASSING ARGUMENTS TO FUNCTIONS  In programming, there are two ways in which arguments can be passed to functions :-  Pass by Value:  Function creates a copy of the variable(Object in Python) passed to it as an argument.  The actual object is not affected.  Object is of immutable type,because immutable objects cannot be modified.  Pass by Reference:  The actual object is passed to the called function.  All the changes made to the object inside the function affect its original value.  Object is mutable type, as mutable objects can be changed, the passed objects are updated.
  • 7. EXAMPLES OF PASSING IMMUTABLE ARGUMENTS OUTPUT: New memory address of ‘a’ inside the function. Actual variable ‘a’ is not changed.
  • 8. EXAMPLES OF PASSING MUTABLE ARGUMENTS Address is same Value of my_list is changed after function call.
  • 10. DEFAULT ARGUMENT VALUES  Default Argument- argument that assumes a default value if a value is not provided in the function call for that argument.  The default value is evaluated only once.  EXAMPLE: Output: In this code, argument ‘b’ has given a default value. ie(b=90) When the value of ‘b’ is not passed in function ,then it takes default argument.
  • 11. DEFAULT ARGUMENTS  The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes.  Example:  if we don’t want the default value to be shared between subsequent calls, then  Example : Output: Output:
  • 12. KEYWORD ARGUMENTS  Keyword arguments are related to the function calls.  Using keyword arguments in a function call, the caller identifies the arguments by the parameter name.  Allows to skip arguments or place them out of order, the Python interpreter use the keywords provided to match the values with parameters.  Example: Output:  Caller identifies the keyword argument by the parameter name.  Calling of the argument Order doesn’t matter .
  • 13. KEYWORD ARGUMENTS  Example 1: Output: Example 2: Output:
  • 14. ARBITRARY ARGUMENT LISTS  Variable-Length Arguments: more arguments than we specified while defining the function.  These arguments are also called variable-length arguments .  An asterisk (*) is placed before the variable name that holds the values of all non keyword variable arguments.  This tuple remains empty if no additional arguments are specified during the function call.  Syntax : def function_name([formal_args], *var_args_tuple ): statement 1…… statement 2…… return [expression] This is called arbitrary argument and asterisk sign is placed before the variable name.
  • 15. ARBITRARY ARGUMENT LISTS  EXAMPLE 1: Output: Output:  EXAMPLE 2:  Any formal parameter which occur after the *args parameter are keyword-only arguments.  If formal parameter are not keyword argument then error occurred.
  • 16. UNPACKING ARGUMENT LISTS  Arguments are already in a list or tuple, need to be unpacked for a function call requiring separate positional arguments.  For instance, the built-in range() function expects separate start and stop arguments.  If range() are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple.  Example 1: Example 2: Output: In dictionary, we use ‘**’ for unpacking the arguments In tuples or lists, ‘*’ is used to unpack the arguments
  • 17. LAMBDA EXPRESSIONS  Small functions can be created with the lambda keyword also known as Anonymous function.  Used wherever functions object are required.  These functions are called anonymous because they are not declared in the standard manner by using the def keyword. .  Syntax :  EXAMPLE: lambda [arg1 [,arg2,.....argn]]:expression Output:  The function returns the multiply of two arguments.  Lambda function is restricted to a single expressions.
  • 18. FUNCTION ANNOTATIONS  Function annotations are completely optional metadata information about the types used by user-defined functions.  Annotations are stored in the “__annotations__” attribute .  Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation.  Return annotations are defined by a literal ->, followed by an expression, between the parameter list and the colon denoting the end of the def statement.  Example: Output: Literal ‘->’ is used to define return annotations.
  • 19. DOCUMENTATION STRINGS  The first line should always be a short, concise summary of the object’s purpose.  The first line should begin with a capital letter and end with a period.  If there are more lines in the documentation string, the second line should be blank.  The first non-blank line after the first line of the string determines the amount of indentation for the entire documentation string.  Example: Output: ‘.doc’ is used to print the documentation strings.