Unit-3_Python Functions
Unit-3_Python Functions
● Functions help to break our program into smaller modules and make our
program more organized, readable.
Function arguments and parameters
Some Advantages of Functions:
● Complex codes can be divided into smaller parts.
● Once function is written, it can be reused as and when required. This helps
programmer to avoid writing the same code again and again.
● Code maintenance becomes very easy. To add any new feature in the code, just
create new functions and if the user does not want any specific feature, that can be
removed from the code.
● When there is an error in the software, the corresponding function can be modified
without disturbing any other functions in the software. This makes code debugging
will become easy.
● The use of functions in a program will reduce the length of the program.
Use of Function:
Example: Suppose we have been asked to find the sum of numbers starting
from 1 to 10, 15 to 25 and 30 to 50. Without function we will write the code in
the following manner:
With use Of Function:
Definition of Function:
Syntax is:
def functionName(p1, p2, …):
statement1
statement2
return expression
Note—
● def is the keyword
● Use of parentheses i.e,( ) after function name.
● p1, p2, .. are the parameters
● Colon(:) sign at the end of the function name.
Example—
def user_function( ):
print("Hello...Welcome Back")
Types of Functions in Python
● Built-in library function: These are Standard functions in Python that are available to
use.
● Example: range(), id() , type(), input() ,print() etc.
Note:
id() Function: In Python, id() function is a built-in function that returns the unique identifier
of an object. The identifier is an integer, which represents the memory address of the object.
The id() function is commonly used to check if two variables or objects refer to the same
memory location.
Syntax: id(object)
Return: a unique integer for a given object
Example:1
Note:
● ID is a Memory Address: The id() function returns a unique identifier (memory address) for an object.
● Id() Function on different data types: Immutable data types (strings, tuples) can sometimes share IDs due to optimizations, while
mutable types (lists, sets, dictionaries) will not share IDs unless they reference the same object .
● User-defined function: We can create our own functions based on our requirements.
Example 2:
Working of Function
● To call a function in our code, use the function name followed by parenthesis()
Note:
● The function can be called any number of times to perform the task.
A parameter is the variable defined within the parentheses during function definition. Simply they are written
when we declare a function.
Example Of Parameters:
Arguments:
An argument is a value that is passed to a function when it is called. It might be a variable, value or object
passed to a function or method as input. They are written when we are calling the function.
Example Of Arguments:
Types Of Argument:
● Required arguments
● Keyword arguments
● Default arguments
● Variable-length arguments
Positional Arguments:
Positional Arguments are needed to be included in proper order i.e the first argument is always
listed first when the function is called, second argument needs to be called second and so on.
Example:
Keyword Arguments:
Keyword Arguments is an argument passed to a function or method which is preceded by a keyword and an
equal to sign. The order of keyword argument with respect to another keyword argument does not matter
because the values are being explicitly assigned.
Example:
Default Arguments
● In a function, arguments can have default values. We assign default values to the argument using
the ‘=’ (assignment) operator at the time of function definition. You can define a function with any
number of default arguments.
● The default value of an argument will be used inside a function if we do not pass a value to that
argument at the time of the function call. Due to this, the default arguments become optional during
the function call.
Important points to remember about function argument
Point 1: Default arguments should follow non-default arguments.
*args
● When we are not sure about the number of arguments being passed to a function
then we can use *args as function parameter.
● *args allow us to pass the variable number of Non Keyword Arguments to function.
● We can simply use an asterisk * before the parameter name to pass variable length
arguments.
● These arguments are collected into a tuple within the function and allow us to
work with them.
Examples of *args :
Output:
Example 3: Example 4: Unpacking of Arguments
2. **kwargs
● **kwargs allows us to pass the variable number of Keyword Arguments to
the function.
● We can simply use an double asterisk ** before the parameter name to
pass variable length arguments.
● The arguments are passed as a dictionary.
Example of **kwargs
Example 2:
Combining *args and **kwargs
Lambda, Filter, Map and Reduce Functions
1. Lambda
● A lambda function is an anonymous function (function without a name).
● Lambda functions can have any number of arguments but only one
expression. The expression is evaluated and returned.
● We use lambda functions when we require a nameless function for a short
period of time.
Example
Filter
● It is used to filter the iterables/sequence as per the conditions.
● Filter function filters the original iterable and passes the items that returns
True for the function provided to filter.
● It is normally used with Lambda functions to filter list, tuple, or sets.
● filter() method takes two parameters:
● function - function tests if elements of an iterable returns true or false
● iterable - Sequence which needs to be filtered, could be sets, lists,
tuples, or any iterators
Map
● The map() function applies a given function to each item of an iterable (list,
tuple etc.) and returns a list of the results.
● map() function takes two Parameters :
● function : The function to execute for each item of given iterable.
● iterable : It is a iterable which is to be mapped.
● Returns : Returns a list of the results after applying the given function to
each item of a given iterable (list, tuple etc.)
Reduce
The purpose of using lambda, filter, map, and reduce functions is to make data
processing easier, more efficient, and expressive in Python. Here’s how each
contributes: