SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Data types in python
Data types in pythonData types in python
Data types in python
RaginiJain21
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python for loop
Python for loopPython for loop
Python for loop
Aishwarya Deshmukh
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 

Similar to Functions in python slide share (20)

functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.pptPy-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
AXL Computer Academy
 
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
MalligaarjunanN
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
 
Functions
FunctionsFunctions
Functions
Septi Ratnasari
 
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
RuzelAmpoan1
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
bhuvanalakshmik2
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
programlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jgprogramlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.pptPy-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
MalligaarjunanN
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
 
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
10 Functions.pptx DSDFDFDFDFDFDFDFDFFDFDFD
RuzelAmpoan1
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
programlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jgprogramlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
Ad

More from Devashish Kumar (6)

Python: Data Visualisation
Python: Data  VisualisationPython: Data  Visualisation
Python: Data Visualisation
Devashish Kumar
 
Pandas csv
Pandas csvPandas csv
Pandas csv
Devashish Kumar
 
Data Analysis packages
Data Analysis packagesData Analysis packages
Data Analysis packages
Devashish Kumar
 
Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
Devashish Kumar
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
Devashish Kumar
 
Cloud Computing Introductory-1
Cloud Computing Introductory-1Cloud Computing Introductory-1
Cloud Computing Introductory-1
Devashish Kumar
 
Python: Data Visualisation
Python: Data  VisualisationPython: Data  Visualisation
Python: Data Visualisation
Devashish Kumar
 
Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
Devashish Kumar
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
Devashish Kumar
 
Cloud Computing Introductory-1
Cloud Computing Introductory-1Cloud Computing Introductory-1
Cloud Computing Introductory-1
Devashish Kumar
 
Ad

Recently uploaded (20)

Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Timber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptxTimber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptx
Tantish QS, UTM
 
Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
High Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptxHigh Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptx
Ayush Srivastava
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Unit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theoriesUnit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theories
bharath321164
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Timber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptxTimber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptx
Tantish QS, UTM
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
High Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptxHigh Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptx
Ayush Srivastava
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Unit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theoriesUnit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theories
bharath321164
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 

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.