ITP-Day 4 - Deck
ITP-Day 4 - Deck
com
Python Functions
STB6BGNELH
Built-in functions are those that are already defined in the Python library
[email protected]
STB6BGNELH
[email protected]
STB6BGNELH
● A user can give any name to a user-defined function. However a function name
should not have space or special character
[email protected]
STB6BGNELH
In python, a user-defined function is created using the def keyword,
followed by the function name.
The keyword
Step 1: Declare the function with the Python ‘def’
The function
keyword def followed by a function name desired by name
the user
Example
[email protected]
STB6BGNELH
Example
[email protected]
STB6BGNELH
● When we declare a
function that expects
input argument, we
should pass the
[email protected]
STB6BGNELH
required value
[email protected]
STB6BGNELH
Note: We use the return keyword in the function but we do not mention what to return.
Hence the function returns no value
[email protected]
STB6BGNELH
Note: When We don't use the return keyword in the function, it will just execute the
statements inside that function
[email protected]
STB6BGNELH
[email protected]
STB6BGNELH
• Required Arguments
• Keyword Arguments
[email protected]
STB6BGNELH
• Default Arguments
• Variable-Length Arguments
In case, if the function does not requires the argument and is passed it will throw an error
[email protected]
STB6BGNELH
In case, if the function requires the argument and is not passed it will throw an error
[email protected]
STB6BGNELH
The arguments have names assigned to them. In the below example, we have Name &
Designation as the named parameters. We pass ‘John’ as Name and ‘CEO’ as Designation
[email protected]
STB6BGNELH
Note: Even if the wrong values are passed, it will NOT throw an error. For example, if we
say ‘CEO’ as Name instead of ‘John’, the function will still work but with wrong values
[email protected]
STB6BGNELH
Note: The function expects 2 arguments - Name and Salary. We have passed a default
value for salary. When we call the function, we pass only Name but not Salary. In this
case, it will consider the default value for Salary that has been passed when the function
was defined
[email protected]
STB6BGNELH
This helps you in passing variable number of arguments. This is especially helpful when
you do not know how many arguments to pass to the function
[email protected]
STB6BGNELH
**kwargs allows the users to pass keyword-ed arguments of variable length to a function.
The **kwargs to be used if named arguments are to be passed to a function
[email protected]
STB6BGNELH
●
[email protected]
STB6BGNELH A scope defines the hierarchical order in which the namespaces need to be searched
in order to obtain the name-to-object mapping
In Python, the LEGB rule is used to decide the order in which the namespaces are to be
searched for scope resolution
[email protected]
Variable scope hierarchy:
STB6BGNELH
Local scope refers to variables defined in a current function. A function will first look up for
a variable name in its local scope. If it does not find it there, only then the outer scopes are
searched for
[email protected]
STB6BGNELH
Global Scope
If a variable is not defined in local scope, then, it is checked for in the higher scope, in this
case, the global scope
[email protected]
STB6BGNELH
For the enclosed scope, we need to define an outer function enclosing the inner function.
Refer to the variable using the nonlocal keyword.
[email protected]
STB6BGNELH
If we have not defined a variable and the name of the variable matches with a built-in
function from an existing Python module, the function will use the built-in function.
[email protected]
STB6BGNELH
[email protected]
STB6BGNELH
[email protected]
STB6BGNELH
You will need to declare the variable which you may use inside the lambda function. In the
below example, we use variable, b, without declaring it.
[email protected]
STB6BGNELH
It executes the function_object for each element in the sequence and returns a
[email protected]
STB6BGNELH
sequence of the elements modified by the function object.
map(lambda_expression, sequence)
map() function iterables such as list, tuples, etc. (you
can only.
This file is meant for personal use by [email protected] pass multiple iterables)
Sharing or publishing
Proprietary content. © Great the contents
Learning. in partUnauthorized
All Rights Reserved. or full isuse
liable for legal
or distribution action.
prohibited.
The lambda() with map()
[email protected]
STB6BGNELH
[email protected]
STB6BGNELH
map(f, li1, li2, li3, …) → applies f to all lists in parallel. That is, first element of result would be
f(e1,e2,e3) where its args are first element of each list.
This file is meant for personal use by [email protected] only.
Sharing or publishing
Proprietary content. © Great the contents
Learning. in partUnauthorized
All Rights Reserved. or full isuse
liable for legal
or distribution action.
prohibited.
The lambda() with map()
[email protected]
STB6BGNELH
function_object returns a boolean value and is called for each element of the iterable.
[email protected]
It returns only those elements for which the function_object returns true.
STB6BGNELH
filter(lambda_expression, sequence)
filter() function iterables such as list, tuples, etc. (only
one only.
This file is meant for personal use by [email protected] iterable as input)
Sharing or publishing
Proprietary content. © Great the contents
Learning. in partUnauthorized
All Rights Reserved. or full isuse
liable for legal
or distribution action.
prohibited.
The lambda() with filter()
[email protected]
STB6BGNELH
Note : Unlike map(), the filter() function can only have one iterable as input.
This file is meant for personal use by [email protected] only.
Sharing or publishing
Proprietary content. © Great the contents
Learning. in partUnauthorized
All Rights Reserved. or full isuse
liable for legal
or distribution action.
prohibited.
The lambda() with reduce()
The function is called with a lambda function and a sequence. A new reduced
result is returned.
[email protected]
STB6BGNELH
This performs a repetitive operation over the pairs of the sequence object.
reduce(lambda_expression, sequence)
reduce() function iterables such as list, tuples, etc. (you
This file is meant for personal use by [email protected] can only. also pass multiple iterables of
Sharing or publishing
Proprietary content. © Great the contents
Learning. in partUnauthorized
All Rights Reserved. or full isuse
liable same
for legal
or distribution type)
action.
prohibited.
The lambda() with reduce()
[email protected]
STB6BGNELH
Working:
3 5 8 10
16
[email protected]
STB6BGNELH
The function is called with a lambda function and a sequence object. A new reduced result
[email protected]
is returned.
STB6BGNELH
accumulate(sequence, lambda_expression)
accumulate() iterables such as list, tuples, etc. (you
function can also pass multiple iterables of
This file is meant for personal use by [email protected] only.
same type)
Sharing or publishing the contents in part or full is liable for legal action.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
The lambda() with accumulate()
Determining the summation of all elements of a list of numerical values by using accumulate:
[email protected]
STB6BGNELH
reduce() accumulate()
The reduce() stores the intermediate result. The accumulate() returns a list containing
Returns only the final summation value the intermediate results. The last number
[email protected] of the list returned is summation of the
STB6BGNELH
elements of the list
The function will continue to call itself and repeat its behavior until some condition
[email protected]
STB6BGNELH
is met to return a result.
For example:
[email protected]
STB6BGNELH
A base case is a case, where the problem can be solved without further recursion.
Let us track how the previously defined recursive function, sum_of_n works by adding two
print functions:
[email protected]
STB6BGNELH
Thank You