0% found this document useful (0 votes)
21 views52 pages

ITP-Day 4 - Deck

The document discusses Python functions including: user-defined functions which are created using the def keyword followed by a function name and arguments; built-in functions that are pre-defined in the Python library; and how to define a user-defined function by declaring it with def, writing arguments in parentheses, ending with a colon, adding statements, and ending the function optionally with a return. Examples are provided of writing basic functions with and without arguments.

Uploaded by

Rohit Angira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views52 pages

ITP-Day 4 - Deck

The document discusses Python functions including: user-defined functions which are created using the def keyword followed by a function name and arguments; built-in functions that are pre-defined in the Python library; and how to define a user-defined function by declaring it with def, writing arguments in parentheses, ending with a colon, adding statements, and ending the function optionally with a return. Examples are provided of writing basic functions with and without arguments.

Uploaded by

Rohit Angira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

gaurrohit0071@gmail.

com

Python Functions
STB6BGNELH

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.
Agenda
● User-Defined Functions
● Function Arguments
● Variable Scope
● Lambda Function
[email protected]
● Recursive Function
STB6BGNELH

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.
User-Defined Functions
[email protected]
STB6BGNELH

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.
Built-in functions

Built-in functions are those that are already defined in the Python library
[email protected]
STB6BGNELH

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.
User-defined functions

● A function that a user defines in a program is known as user defined function

[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

Note: You cannot use the Python keywords as function name


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 def keyword

[email protected]
STB6BGNELH
In python, a user-defined function is created using the def keyword,
followed by the function name.

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.
Defining the user-defined function

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

Step 2: Write the arguments inside the parentheses


[email protected]
STB6BGNELH
of the function

Step 3: End the declaration with a colon

Step 4: Add the program statements that need to be The value to


executed be returned The input
parameters
Step 5: End the function. Note a function can be
ended with or without a return statement
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.
Write your first function using the def keyword

Example

[email protected]
STB6BGNELH

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.
Write a function with an argument

Example

[email protected]
STB6BGNELH

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.
Calling the function without passing argument

● When we declare a
function that expects
input argument, we
should pass the
[email protected]
STB6BGNELH
required value

● If we do not pass the


required value, the
function will throw an
error
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.
A function with return

[email protected]
STB6BGNELH

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.
A function without any return value

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

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.
A function without return keyword

Note: When We don't use the return keyword in the function, it will just execute the
statements inside that function
[email protected]
STB6BGNELH

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.
A function without return keyword

Note: A function without return statement will return None

[email protected]
STB6BGNELH

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.
Function Arguments
[email protected]
STB6BGNELH

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.
Types of function arguments

• Required Arguments

• Keyword Arguments
[email protected]
STB6BGNELH

• Default Arguments

• Variable-Length Arguments

• Variable-Length keyworded Arguments

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.
Required Arguments

In case, if the function does not requires the argument and is passed it will throw an error

[email protected]
STB6BGNELH

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.
Required Arguments

In case, if the function requires the argument and is not passed it will throw an error

[email protected]
STB6BGNELH

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.
Keyword Arguments

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

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.
Keyword Arguments

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

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.
Default Arguments

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 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.
Variable-length arguments using *arg keyword

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

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.
Variable-length keyworded arguments using **kwargs

**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

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.
Variable Scope
[email protected]
STB6BGNELH

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.
Variable scope

● A namespace is a container where names are mapped to variables (Python objects)


[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

● Scope defined the accessibility and lifetime of a variable

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 LEGB rule

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

1. Built-In (B): Reserved names in Python


2. Global Variable (G): Defined at the uppermost level
3. Enclosed (E): Defined inside enclosing or nested functions
4. Local Variable (L): Defined inside a function

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.
Local Scope

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

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.
Local and Global Variable

Declaring the variable


‘multiplication’

Declaring the variable ‘number’

[email protected]
STB6BGNELH

Calling the Variable.


It is a local variable

Calling the Variable.


It is a global variable
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.
Enclosed Scope

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

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.
Built-In Scope

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

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.
Lambda Function
[email protected]
STB6BGNELH

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.
Lambda function

● Lambda functions are anonymous, i.e. to say they have no names

● The lambda is a keyword


[email protected]
STB6BGNELH

● It is a simple one-line function

● No def or return keyword to be used with a lambda function

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 structure of the lambda function

[email protected]
STB6BGNELH

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.
Using lambda function to reduce code size

Normal Python code Using lambda function

[email protected]
STB6BGNELH

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.
Wrong usage of lambda

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

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()

map() functions expect a function_object, in our case a lambda function,and a


sequence (iterables, such as list, dictionary, etc.)

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.

lambda expression (You can also consider any other


function )

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()

The output is often type-casted into a seq type, as follow:

[email protected]
STB6BGNELH

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()

You can pass multiple sequences to the map function as follow:

[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()

You can pass multiple sequences to the map function as follow:

[email protected]
STB6BGNELH

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 filter()

The filter() function expects two arguments: function_object(lambda) and an iterable.

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

lambda expression (You can also consider any other


function )

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()

The output is often type-casted into a seq type, as follow:

[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 reduce() function in Python takes in a function and a sequence as argument.

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.

lambda expression (You can also consider any other


function )

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()

Determining the summation of all elements of a list of numerical values by using


reduce:

[email protected]
STB6BGNELH

Working:
3 5 8 10

16

This file is meant for personal use26 by [email protected] only.


Sharing or publishing the contents in partUnauthorized
Proprietary content. © Great Learning. All Rights Reserved. or full isuse
liable for legal
or distribution action.
prohibited.
The lambda() with reduce()

Determining the maximum of a list of numerical values by using reduce:

[email protected]
STB6BGNELH

Note: reduce() can only have iterables of same type 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 accumulate()

The accumulate() function in Python takes in a function and a sequence object as an


argument.

The function is called with a lambda function and a sequence object. A new reduced result
[email protected]
is returned.
STB6BGNELH

Unlike reduce(), it returns a sequence containing the intermediate results


lambda expression (You can also
consider any other function )

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

The result of above accumulate function can be interpreted as:


p0, p0+p1, p0+p1+p2, …

where, p0, p1, p2,... are elements of the iterable passed


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.
Difference between reduce() and accumulate()

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 reduce(fun,seq) takes function as 1st The accumulate(seq,fun) takes sequence


argument and a sequence object as 2nd as 1st argument and function as 2nd
argument argument

The reduce() is defined in “functools” The accumulate() is defined in “itertools”


module module
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.
Recursive Functions
[email protected]
STB6BGNELH

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.
Recursive Function

A recursive function is a function defined in terms of itself via self-referential


expressions.

The function will continue to call itself and repeat its behavior until some condition
[email protected]
STB6BGNELH
is met to return a result.

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.
Recursive Function

For example:

[email protected]
STB6BGNELH

A base case is a case, where the problem can be solved without further recursion.

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.
Recursive Function

Let us track how the previously defined recursive function, sum_of_n works by adding two
print functions:

[email protected]
STB6BGNELH

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.
[email protected]
STB6BGNELH

Thank You

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.

You might also like