Function - 2
The Anonymous Functions:
These functions are called anonymous because they are not declared in the standard manner by using the def keyword.
Lambda Keyword:
We can use the lambda keyword to create small anonymous functions.
Example 1:
Value of total : 30
Value of total : 40
The return Statement:
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.
Example 2:
Inside the function : 30
Outside the function : 30
Example 3:
343
125
Advantage of Lambda Function
Without using Lambda :- Here, both of them returns the cube of a given number. But, while using def, we needed to define a function with a name cube and needed to pass a value to it. After execution, we also needed to return the result from where the function was called using the return keyword.
Using Lambda :- Lambda definition does not include a “return” statement, it always contains an expression which is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions.
map() function :
The map() function in Python takes in a function and a list as argument.
The function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item.
Python map()
built-in function applies the given function on every item of iterable(s) and returns an iterator object.
Syntax:
map(function, iterable)
fun : It is a function to which map passes each element of given iterable. iter : It is a iterable which is to be mapped.
Returns:
Returns a list,tuple,set of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Note:
The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set)
Example -4:
1
4
9
16
25
Python map equivalent function:
The hello() does the same thing as Python 3 map()
Example-5:
1
4
9
16
25
Use of yield instead of return
The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list.
Return sends a specified value back to its caller whereas Yield can produce a sequence of values. We should use yield when we want to iterate over a sequence, but don’t want to store the entire sequence in memory.
Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
1
4
9
16
25
Python map with lambda:
Example-6:
[9, 25, 49]
Python map with multiple iterables:
Example-7:
6
14
24
36
50
Python map with multiple functions:
Example-8:
[4, 4]
[9, 6]
[25, 10]
[36, 12]
Python List Comprehension:
Example-9:
1
9
25
Problems for practice:
Example-1:
Output:
{2,4,6,8}
Example-2:
Output:
[0, 0]
[1, 2]
[4, 4]
[9, 6]
[16, 8]
Example-3:
[10, 14, 44, 194, 108, 124, 154, 46, 146, 122]
Filter() function in python
The filter() function in Python takes in a function and a list as arguments.
This offers an elegant way to filter out all the elements of a sequence “sequence”, for which the function returns True.
filter() with lambda()
Example 1:
[3, 5]
reduce() function in python
The reduce() function in python takes in a function and a list as argument.
The function is called with a lambda function and a list and a new reduced result is returned.
This performs a repetitive operation over the pairs of the list.
This is a part of functools module.
Use of lambda() with reduce()
Example 2:
193
Last updated