FUNCTIONS: ========== Function is a named block can be used to perform the block of action. ex: Ecommerce Application search option cart payment etc. ==> to define the functions in python: keyword: def ==> the functions can have three things: 1) function definition 1) Function header 2) function body 2) function call ==> function can accept the data with respect to parameters and perform the action on that data and return a value. Syntax: (for function definition) def function-name/identifier(parameters/arguments-name): statement-1 statement-2 return value Syntax: (for function calling/invoking) function-name(values for the parameters) ==> functions can be defined in 4-ways: 1) function without parameters and no return type ================================================= # function without parameters and no return type def ecart_total(): cart_items = [199,399,348,1999,999,899,789,648,123] # group/collection sum_items = 0 index = 0 for item in cart_items: sum_items = sum_items + item print("The product",index+1,"is = ",cart_items[index]) index = index + 1 print("The Total Amount = ",sum_items) ecart_total() 2) function without parameters and return type ============================================== # function without parameters and return type # WAP TO CREATE A FUNCTION IN PYTHON TO REVERSE THE STRING. # string[::-1] def reverseString(): str_data = input("Enter a string:") str_reverse = "" for ch in str_data: str_reverse = ch + str_reverse return str_reverse result = reverseString() print("The Reverse of the string = ",result) ================================================================ 3) function with parameters and no return type ============================================== # FUNCTION WITH PARAMETERS AND NO RETURN TYPE def ecart_total(list_products): sum_product = 0 index = 0 for item in list_products: sum_product += item print("Product",(index+1),"is = ",list_products[index]) index += 1 print("The Total Amount = ",sum_product) cart_items = [199,399,349,1999,599,899,1299,1099,999] ecart_total(cart_items) ======================================================= 4) function with parameters an return type =========================================== # Function With Parameters and return type def greets(name): wish = "Good Evening {}".format(name) # "good evening",name return wish print(greets("Ravi")) ==================================================== Function Parameters: ==================== ==> four ways to define parameters in function. 1) Positional arguments 2) Keyword arguments # positional Arguments def addingNumbers(a,b,c): s_numbers = a+b+c print("The Sum of three numbers = ",s_numbers) addingNumbers(121,131,1021) # positional arguments # Keyword Arguments x = 1021 y = 1321 z = 1993 addingNumbers(c = x,a = z,b = y) addingNumbers(a = 100,b = 200,c = 300) 3) Default Arguments ===================== ef SumOfNumbers(a = 0,b = 1,c = 2,d = 3): print("The Sum = ",a+b+c+d) SumOfNumbers() SumOfNumbers(10) SumOfNumbers(10,20) SumOfNumbers(100,200,300) SumOfNumbers(100,200,300,400) 4) Variable Length Arguments ============================= # Variable Length Function def SumOfNumbers(*args):# args = (10,30,50) s_num = 0 for i in args: s_num += i print("The Sum = ",s_num) SumOfNumbers() SumOfNumbers(10,30,50) SumOfNumbers(199,299,399,499,599,699,799,899) =========================================================== 1) WAP TO CREATE THE FUNCTION IN PYTHON TO PRINT ALL ARMSTRONG NUMBERS FROM 1000 TO 10000. 2) WAP TO CREATE THE FUNCTION TO FIND THE FACTORIAL OF THE NUMBER. 3) WAP TO CREATE THE FUNCTION TO PRINT ALL PRIME NUMBERS FROM 100 TO 200. 4) WAP TO CREATE THE FUNCTION TO PRINT MULTIPLICATION TABLE FOR THE GIVEN NUMBER.