0% found this document useful (0 votes)
29 views

More On Function 2

The document discusses various Python functions such as lambda functions, map(), filter(), reduce(), and nested functions. It provides examples of using lambda functions to find the square of a number, sum of two numbers, and largest of two numbers. Examples of using map() to double values in a list and filter() to extract even numbers from a list are shown. The reduce() function is demonstrated to sum the elements of a list and multiply the elements of a list. Finally, an example of a nested function that calls an inner function is provided.

Uploaded by

aasthaa1805
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)
29 views

More On Function 2

The document discusses various Python functions such as lambda functions, map(), filter(), reduce(), and nested functions. It provides examples of using lambda functions to find the square of a number, sum of two numbers, and largest of two numbers. Examples of using map() to double values in a list and filter() to extract even numbers from a list are shown. The reduce() function is demonstrated to sum the elements of a list and multiply the elements of a list. Finally, an example of a nested function that calls an inner function is provided.

Uploaded by

aasthaa1805
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/ 4

In [1]: #Anonymous Functions:

'''Sometimes we can declare a function without any name, such type of nameless
functions are called anonymous functions or lambda functions.'''
#Syntax of lambda Function:
#lambda argument_list : expression

Out[1]: 'Sometimes we can declare a function without any name, such type of nameless \nfun
ctions are called anonymous functions or lambda functions.'

In [2]: #Anonymous Functions:


'''Write a program to create a lambda function to find square of given number?'''
#####################
s=lambda n:n*n
print(f'The square of 4 is: {s(4)}')
print(f'The square of 4 is: {s(6)}')

The square of 4 is: 16


The square of 4 is: 36

In [3]: #Lambda function to find sum of 2 given numbers


s=lambda a,b:a+b
print(f'The sum of 11,13 is: {s(11,13)}')
print(f'The sum of 21,13 is: {s(21,13)}')

The sum of 11,13 is: 24


The sum of 21,13 is: 34

In [4]: def larger_no(arg1,arg2):


if arg1>arg2:
return arg1
else: return arg2
##############
print(larger_no(11,121))

121

In [5]: #Lambda Function to find biggest of given values.


s=lambda a,b:a if a>b else b
print(f'The larger no. in 11,13 is: {s(11,13)}')
print(f'The larger no. in 21,121 is: {s(21,121)}')

The larger no. in 11,13 is: 13


The larger no. in 21,121 is: 121

In [6]: '''Find even numbers in a given list and save them in a new list'''
def isEven(x):
if x%2==0:
return True
else:
return False
###########
list1=[0,5,10,15,20,25,31]
list2=[]
for i in list1:
if isEven(i)==True:
list2.append(i)
print(list2)

[0, 10, 20]

In [7]: #filter() function:


'''We can use filter() function to filter values from the given sequence based
on some condition.'''
#filter(function,sequence)
#####################
def isEven(x):
if x%2==0:
return True
else:
return False
###########
list1=[0,5,10,15,20,25,31]
list2=list(filter(isEven,list1))
print(list2)

[0, 10, 20]

In [8]: '''using lambda and filter function'''


list1=[0,5,10,15,20,25,31]
list2=list(filter(lambda x:x%2==0,list1))
print(list2)

[0, 10, 20]

In [9]: #map() function:


'''For every element present in the given sequence, apply some functionality
and generate new element with the required modification.'''
###############
#Syntax:
#map(function, sequence)
######################

Out[9]: 'For every element present in the given sequence, apply some functionality \nand g
enerate new element with the required modification.'

In [10]: #map() function:


'''For every element present in the list perform double and generate new list
of doubles.'''
################
def double_it(x):
x=2*x
return x
###############
list1=[1,3,5,7,9,11]
list2=list(map(double_it,list1))
print(list2)

[2, 6, 10, 14, 18, 22]

In [11]: ###############using lambda and map function


list1=[1,3,5,7,9,11]
list2=list(map(lambda x:2*x,list1))
print(list2)

[2, 6, 10, 14, 18, 22]

In [12]: '''To find square of given numbers'''


###########
list1=[1,2,3,4,5,6]
list2=list(map(lambda x:x*x,list1))
print(list2)

[1, 4, 9, 16, 25, 36]

In [13]: list1=[1,2,3,4,5,6]
list2=list(filter(lambda x:x*x,list1))
print(list2)

[1, 2, 3, 4, 5, 6]

In [14]: ##map() function:


'''We can apply map() function on multiple lists also. But make sure all
list should have same length.'''
#Syntax:
#map(lambda x,y:x*y,l1,l2))
#x is from l1 and y is from l2
#############
list1=[1,2,3,4]
list2=[5,6,7,8]
list3=list(map(lambda x,y:x*y,list1,list2))
print(list3)

[5, 12, 21, 32]

In [15]: #reduce() function:


'''reduce() function reduces sequence of elements into a single element by the
applying specified function.
#Syntax:
#reduce(function,sequence)
reduce() function present in functools module and hence we should write import
statement.'''

Out[15]: 'reduce() function reduces sequence of elements into a single element by the\napp
lying specified function.\n#Syntax:\n#reduce(function,sequence)\nreduce() function
present in functools module and hence we should write import\nstatement.'

In [16]: from functools import reduce


#reduce() function:
list1=[1,2,3,4,5,6]
result=reduce(lambda x,y:x+y,list1)
print(result)

21

In [17]: #reduce() function:


list1=[1,2,3,4,5,6]
result=reduce(lambda x,y:x*y,list1)
print(result)
720

In [18]: #reduce() function:


result=reduce(lambda x,y:x+y,range(1,10))
print(result)

45

In [19]: #Nested Functions:


'''We can declare a function inside another function, such type of functions
are called Nested functions.'''
####################
def outer():
print('outer function started')
def inner():
print('inner function execution')
print('outer function calling inner function')
inner()
###########
outer()

outer function started


outer function calling inner function
inner function execution

In [20]: ####################
def outer():
print('outer function started')
def inner():
print('inner function execution')
print('outer function calling inner function')
return inner
###########
f1=outer()
f1()
###############
#f1()

outer function started


outer function calling inner function
inner function execution

In [ ]:

You might also like