FUNCTIONS IN PYTHON
By : Mrs Sangeeta M Chauhan ,
Gwalior
03/18/2024 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 1
FUNCTIONS IN PYTHON
ef
D Function is a piece of code written to carry out
a specified task.
You can Pass Data(input) known as
parameter to a function
A Function may or may not return any
value(Output)
03/18/2024
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 2
There are three types of functions in
Python
– Built-in functions The Python interpreter has a
number of functions built into it that are always
available. They are listed here in alphabetical
order.
– User-Defined Functions (UDFs): The Functions
defined by User is known as User Defined
Functions. These are defined with the keyword def
– Anonymous functions, which are also called
lambda functions because they are not declared
with the standard def keyword
03/18/2024 www.pythonclassroomdiary.wordpress.com 3
© Sangeeta M Chauhan, Gwalior
Functions vs Methods :
• A method refers to a function which is part of a
class. You access it with an instance or object of
the class.
• A function doesn’t have this restriction: it just
refers to a standalone function. This means that
all methods are functions, but not all functions
are methods.
03/18/2024 www.pythonclassroomdiary.wordpress.com 4
© Sangeeta M Chauhan, Gwalior
Structure of Python Function
def functionname ( parameters ):
"function_docstring“
function_body
return [expression]
03/18/2024 www.pythonclassroomdiary.wordpress.com 5
© Sangeeta M Chauhan, Gwalior
DOC STRING
Python Docstring is the
documentation string which
is string literal, and it occurs in
the class, module, function or
method definition, and it is
written as a first statement.
Docstrings are accessible from
the doc attribute for any of
the Python object and also
with the built-in help()
function can come in handy.
www.pythonclassroomdiary.wordpress.com
03/18/2024 6
© Sangeeta M Chauhan, Gwalior
After execution of the program/function
>>> Info.__doc__
'This Function is to display info about author.'
www.pythonclassroomdiary.wordpress.com
03/18/2024 7
© Sangeeta M Chauhan, Gwalior
Without execution of Program/function
>>> use_doc.Info.__doc__
'This Function is to display info about author.'
>>>
www.pythonclassroomdiary.wordpress.com
03/18/2024 8
© Sangeeta M Chauhan, Gwalior
Example of Function Definition
• Example for Creating a Function without
parameter
• In Python a function is defined using
the def keyword:
>>> def MyMsg1():
print("Learning to create function")
03/18/2024 www.pythonclassroomdiary.wordpress.com 9
© Sangeeta M Chauhan, Gwalior
Example of Function Definition
• Example for Creating a Function with
parameter
• >>> def MyMsg2( name ):
"This prints a passed string into this function"
print (name ,” is learning to define Python Function”)
• return
03/18/2024 www.pythonclassroomdiary.wordpress.com 10
© Sangeeta M Chauhan, Gwalior
Calling function MyMsg1 ()
To call a function, use the function name followed by parenthesis:
• >>> MyMsg1()
Output :
• >>> Learning to create function
Calling Function MyMsg2() twice with different parameter
>>> MyMsg2(‘Divyaditya’)
>>> MyMsg2(‘Manasvi’) Parameters
Output
Divyaditya is learning to define Python Function
Manasvi is learning to define Python Function
03/18/2024 www.pythonclassroomdiary.wordpress.com 11
© Sangeeta M Chauhan, Gwalior
03/18/2024 www.pythonclassroomdiary.wordpress.com 12
© Sangeeta M Chauhan, Gwalior
03/18/2024 www.pythonclassroomdiary.wordpress.com 13
© Sangeeta M Chauhan, Gwalior
03/18/2024 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 14
03/18/2024 www.pythonclassroomdiary.wordpress.com 15
© Sangeeta M Chauhan, Gwalior
03/18/2024 www.pythonclassroomdiary.wordpress.com 16
© Sangeeta M Chauhan, Gwalior
?????Arguments Vs Parameters
Parameter
Argument
www.pythonclassroomdiary.wordpress.com
03/18/2024 17
© Sangeeta M Chauhan, Gwalior
Function Arguments
1. Default 2. Positional
3. Keyword 4. Arbitrary(Variable no.)
www.pythonclassroomdiary.wordpress.com
03/18/2024 18
© Sangeeta M Chauhan, Gwalior
Variable no. of arguments
www.pythonclassroomdiary.wordpress.com
03/18/2024 19
© Sangeeta M Chauhan, Gwalior
Global and Local Variables in Python
a=50 # Global Variable
def MyFunc():
b=100 # Local Variable
print("Function Called :",a)
print("Function Called :",b)
>>>MyFunc() # Function Call
Output
>>> Function Called 50
>>> Function Called 100
03/18/2024 www.pythonclassroomdiary.wordpress.com 20
© Sangeeta M Chauhan, Gwalior
Local and Global Variable……..
03/18/2024 www.pythonclassroomdiary.wordpress.com 21
© Sangeeta M Chauhan, Gwalior
The globals()
method
returns the
dictionary of
the
current global
symbol table.
A symbol
table is a data
structure
maintained by
a compiler
which
contains all
necessary
information
about the
program
03/18/2024 www.pythonclassroomdiary.wordpress.com 22
© Sangeeta M Chauhan, Gwalior
MOST USEFUL
BUILT-IN FUNCTIONS
03/18/2024 www.pythonclassroomdiary.wordpress.com 23
© Sangeeta M Chauhan, Gwalior
Filter Function
– Returns an Object containing sequence of the items
from the sequence for which function(item) is true
Syntax :Filter(function, sequence)
03/18/2024 www.pythonclassroomdiary.wordpress.com 24
© Sangeeta M Chauhan, Gwalior
Example
Computes
primes up
to 20
output
03/18/2024 www.pythonclassroomdiary.wordpress.com 25
© Sangeeta M Chauhan, Gwalior
Map()
– Calls function(item) for each of the sequence’s
items
Syntax:
map(function, sequence)
Ex: map (factorial, list)
03/18/2024 www.pythonclassroomdiary.wordpress.com 26
© Sangeeta M Chauhan, Gwalior
Example of Map function
Computes
the
factorial
of given
values
output
03/18/2024 www.pythonclassroomdiary.wordpress.com 27
© Sangeeta M Chauhan, Gwalior
Python lambda
(Anonymous Functions)
A lambda function is a small anonymous
function.
A lambda function can take any number of
arguments, but can only have one
expression.
Syntax
lambda <arguments> : <expression>
03/18/2024 www.pythonclassroomdiary.wordpress.com 28
© Sangeeta M Chauhan, Gwalior
Example 1 of lambda function
output
03/18/2024 www.pythonclassroomdiary.wordpress.com 29
© Sangeeta M Chauhan, Gwalior
Example 2 : lambda Function
output
03/18/2024 www.pythonclassroomdiary.wordpress.com 30
© Sangeeta M Chauhan, Gwalior
Example 3: lambda Function
output
03/18/2024 www.pythonclassroomdiary.wordpress.com 31
© Sangeeta M Chauhan, Gwalior
Calling of functions with ternary operator.
OUTPUT
www.pythonclassroomdiary.wordpress.com
03/18/2024 32
© Sangeeta M Chauhan, Gwalior
# Function argument unpacking
output
www.pythonclassroomdiary.wordpress.com
03/18/2024 33
© Sangeeta M Chauhan, Gwalior
Output
www.pythonclassroomdiary.wordpress.com
03/18/2024 34
© Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com
03/18/2024 35
© Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com
03/18/2024 36
© Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com
03/18/2024 37
© Sangeeta M Chauhan, Gwalior
Thanks
www.pythonclassroomdiary.wordpress.com
03/18/2024 38
© Sangeeta M Chauhan, Gwalior