More On Function 2
More On Function 2
'''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.'
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)
Out[9]: 'For every element present in the given sequence, apply some functionality \nand g
enerate new element with the required modification.'
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]
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.'
21
45
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()
In [ ]: