Advanced Functions: =================== Function Aliasing ================= for the data, we can define two name a = 10 b = 10 id(a) is id(b) ==> True a = 20 b = 10 a = [1,2,3,4] b = [1,2,3,4] id(a) is id(b) ==> True a[0] = 11 a = [11,2,3,4] b = [11,2,3,4] function aliasing ==> giving/providing the alternative name to the function is called as "function aliasing" Syntax: new-name = function-name # Function Aliasing def sumDigits(num): n = num s_dig = 0 while n != 0: ind_dig = n % 10 # 1 2 s_dig = s_dig + ind_dig # 1 3 n = n // 10 # 98765432 9876543 return s_dig print(sumDigits(987654321)) # function aliasing sIndDig = sumDigits print(sIndDig(97799779)) print(sumDigits(102030405060708090)) =============================================== ANONYMOUS FUNCTION ================== NAMELESS FUNCTION KEYWORD: lambda Syntax: identifier = lambda parameter-list : expression ==> lambda function can return a value by default (without return statement) ==> Two reasons for anonymous functions: 1) For Instant use 2) to send a function as an argument to the function, lambda function/anonymous function is preferred. # LAMBDA FUNCTION # sum of three numbers without the lambda function # def sumNumbers(a,b,c): # s_nums = a + b + c # print("The SUm of three numbers = ",s_nums) # # sumNumbers(11,21,31) result = lambda a,b,c : a+b+c print("The sum of three numbers = ",result(11,21,31)) ===============\ # message = lambda name : ("Hello",name,"Good Night") message = lambda name : "Hello %s,Good Night."%(name) print(message("Rahul")) ========================== to send a function as an argument to the function, lambda function/anonymous function is preferred. =================================================================================================== there are three function tools are required. function tools ==> functools ==> in functools, there are three inbuilt methods: filter() map() reduce() filter() ======== 1) filter() can always accept a sequence/collection 2) filter() can perform filtering operation on the given collection/sequence based on the condition Syntax: identifier = filter(condition, sequence/collection) ==> like lambda function, filer() can also return an iterable object to access the returned object, we can perform the type casting to the filter() here: condition ==> can be passed with other function # filter() # WAP TO ACCEPT A LIST AND PRINT THE LIST WITH ONLY EVEN ELEMENTS. # WITHOUT LAMBDA FUNCTION def filterEvens(x): if x % 2 == 0: return True else: return False ld = eval(input("Enter a list:")) res_list = list(filter(filterEvens,ld)) print(res_list) =================================== # with lambda function ld = eval(input("Enter a list:")) res_list = list(filter(lambda x : x % 2 == 0,ld)) print(res_list) ========================================= map(): ====== 1) map() can accept a sequence/collection 2) return an object need to type cast the map() to get the traceable object. 3) map() will perform the specified operation on each element of the collection/sequence based on the condition. [1,2,3,4] [2, 4, 6, 8] Syntax: identifier = map(condition, collection/sequence) # WAP TO PRINT THE DOUBLE THE ELEMENTS OF THE LIST. ld = eval(input("Enter the list data:")) result = list(map(lambda x : 2 * x,ld)) print(result) =============================================================== reduce(): ========= 1) to use the reduce(), we should import this from "functools" Syntax: from functools import * 2) reduce() can accept a collection/sequence. 3) based on the condition, reduce() will generate a single value from the sequence/collection. [1,3,5,7,9] ==> 945 Syntax: identifier = reduce(condition, sequence/collection) # reduce() from functools import * td = eval(input("Enter a tuple data:")) result = reduce(lambda x,y : x * y,td) print(result)