ADVANCED FUNCTIONS ================== FUNCTION ALIASING ================= DEFINING THE SAME FUNCTION WITH ANOTHER NAME def fun(): jlasksfk fun() name = fun name() fun() # function aliasing def printMessage(name): print("Hey",name,"Good Morning.") printMessage('Kumar') # function aliasing greets = printMessage greets('Rakesh') printMessage('Rahul') wishes = greets wishes('Ashwini') ============================================ Banking Application xyz abc bank functionality-1 ANONYMOUS FUNCTION ================== def function-name(): function-body return function-name() ==> anonymous function is also called as "nameless function" keyword: lambda Syntax: identifier = lambda argument_list : expression ==> lambda function can always return a value by default (without the return statement). # anonymous function # def square(n): # return n * n # # print(square(7)) square = lambda n : n ** 2 print("The Square of {} is = {}".format(9,square(9))) why lambda function: ===================== 1) just for instant use. 2) when we want to send a function as an argument to the function, lambda function is the best option. map(), filter(), reduce() filter(): ======== is an inbuilt function, which we can use to filter from the collection based on some condition. syntax: filter(function, sequence/collection) Here: function can use to define the condition to perform the filtering # filter() # WAP TO FILTER ONLY EVEN NUMBERS FROM THE LIST BY USING filter() # def filterFunction(ld): # rl = list() # for i in ld: # if i % 2 == 0: # rl.append(i) # return rl # # listData = eval(input("Enter a list:")) # # print("The List Even elements = ",filterFunction(listData)) ============================================================== # filter() without lambda function def isEven(ld): if ld % 2 == 0: return True else: return False ld = [1,10,0,101,11,100,21,20,30,31] result = list(filter(isEven,ld)) print(result) ==================================== # filter() with lambda function l = [0,5,10,15,20,25,30,35,40] result = tuple(filter(lambda x : x % 2 == 0,l)) print(result) ======================================= map() function: =============== map() is an inbuilt method, which we can use to accept a collection/sequence based on the condition it can modify the mapped element from the collection Syntax: map(function, collection) # map() l = [1,3,5,7,9,11] result =list(map(lambda x : x * x, l)) print(result) ==================================== reduce() ======== reduce() can accept a collection and reduce to the single element based on the condition. Syntax: reduce(function, sequence/collection) ==> to use the reduce(), we should import "functools". Syntax: from functools import * from functools import * l = [1,3,5,7,9,11,13,15,17,19,21,23,25] result = reduce(lambda x,y : x + y,l) print(result)