Q: If local variable and global variable with the same name? ============================================================= Ex: a = 100 # global variable def fun(): a = 500 # local variable print(a) # 500 ==> to access the global variable within the function when the local variable already defined with same name: "globals()" Syntax: globals()['variable-name'] a = 100 # global variable def fun1(): # print("a = ",a) # global variable a = 1000 # local variable print("a as local variable = ",a) print("a as global variable = ",globals()['a']) fun1() ======================================================== Recursion: ========== Function: function definition function header + function body function call Ex: fun1() { ---- ----- fun1() } Recursion is the procedure used to call itself until the completion of the task. ==> the function which can be used for recursion is called as "Recursive function". Advantages: =========== 1) Length of the code can be reduced. 2) Readability increased. 3) to solve the complex programs, recursion can be used. # WAP TO FIND THE FACTORIAL OF THE NUMBER WITHOUT RECURSION. """ 5! ==> 5 X 4 X 3 X 2 X 1 ==> 120 1) 0! == 1 2) factorial for negative numbers ==> not possible 3) factorial is always possible for positive numbers only. """ number = int(input("Enter a value:")) num = number factorial = 1 if number == 0: print("The Factorial of the number {} is = {}".format(num,factorial)) elif number < 0: print("Factorial is not possible for negative numbers.") else: while number != 0: factorial = factorial * number number -= 1 print("The Factorial of the number {} is = {}".format(num, factorial)) ============================================================ # WAP TO FIND THE FACTORIAL OF THE NUMBER WITH RECURSION def findFactorial(number): if number == 0: result = 1 elif number < 0: result = "Factorial is not possible for negative numbers." else: result = number * findFactorial(number-1) return result number = int(input("Enter a value:")) print("Factorial of number = ",findFactorial(number)) ================================================================= Anonymous Functions: ==================== ==> functions without any name are called as "anonymous functions". ==> anonymous functions are also called as "nameless functions". ==> also called as "lambda functions". ==> use just for instant use (one time usage). Lambda Function: ================ def function-name(parameters): function-body Syntax: lambda argument-list:expression # lambda Functions # WAP PROGRAM TO FIND THE SUM OF TWO NUMBERS USING LAMBDA FUNCTION # def sumNUmbers(a,b): # return a+b # print(sumNUmbers(100,200)) result = lambda a,b : a+b print("The Sum = ",result(100,200)) ================================================= # WAP TO FIND THE BIGGEST VALUE AMONG TWO VALUES USING LAMBDA FUNCTION. biggest_number = lambda x,y : x if x > y else y p = int(input("Enter a value:")) q = int(input("Enter a value:")) print("The Biggest Number = ",biggest_number(p,q)) ============================================================= filter() function: ================== [10,20,30,40,50,60] [10,30,50] ==> filter() can be used to filter the values from the sequence based on the condition. Syntax: filter(function_for_defining_condition, sequence) # WAP TO FILTER ONLY EVEN NUMBERS FROM THE LIST BY USING filter(). def isEven(p): if p % 2 == 0: return True else: return False ld = [0,3,5,10,2,-24,96,79,97,20,70,100] resultant_list = list(filter(isEven,ld)) print(resultant_list) ================================================== map() function: =============== [1,2,3,4] map(square) ==> [1, 4, 9, 16] ==> used to perform the functionality on each element of the sequence and apply that modification into the sequence. Syntax: map(function, sequence) # map() def Triple(x): return x ** 3 ld = [1,3,5,7,9] result = list(map(Triple, ld)) print(result) Assignment: =========== 1) WAP TO FIND THE SQUARE AND CUBE OF A NUMBER USING LAMBDA FUNCTION.