Advanced Functions: =================== Q: Why the data/function declaration is not mandatory in Python? ================================================================= Ans: Because the Python dynamically typed/Instant Typed programming language x = 123 # integer x = 1.23 # float x = "Python" # String Syntax for function definition: =============================== Note: ==== Function declaration is not required in Python def function-name(parameters with comma separation): statement-1 statement-2 statement-3 return value ===================================================== Types of Variables ================== ==> Three types of variables: 1) Local Variables 2) Global Variables 3) Non-Local Variables 1) Local Variables ================== ==> always define within the function body and we can access within the same function. ==> the scope of the local variables is function scope. # Local Variables def localVariables(): a = 100 b = 200 print("Local Variables are:") print("a = ",a) print("b = ",b) localVariables() # print("Local Variables are:") # print("a = ",a) # print("b = ",b) =========================================== def LocalVariables(a,b,c): # the function parameters are also act as local variables print("The Local Variables are:") print("a = ",a) print("b = ",b) print("c = ",c) LocalVariables(10,20,30) # print("a = ",a) # print("b = ",b) # print("c = ",c) ========================================= a = -10 b = 20 if a > 5 and b != 0: c = 30 print("a = ",a) print("b = ",b) print("c = ",c) else: print("a = ", a) print("b = ", b) # print("c = ", c) ============================================== 2) Global Variables =================== ==> can always define in outside the function. ==> Global scope ==> we can access the global variable in anywhere of the program Ex: program ==> 3-functions a variable have defined in outside of all 3 functions ==> that variable we can access in fun1, fun2 and fun3 also. a = 100 b = 200 c = 300 def function1(): # pass print("The Given Global Variables are:") print("a = ",a) print("b = ",b) print("c = ",c) # pass ==> keyword in python # to define an empty block or empty function or empty class, we can use pass in the implementation section. # to make execute the function without implementation as normal function, # we can use "pass" statement. def function2(): # pass # when local variables and global variables with same name, function can # prioritize to local variables first. # print("The Given Global Variables are:") # print("a = ", a) # print("b = ", b) # print("c = ", c) # global keyword can help to define the global variables within the function # also can be used to modify the global variables # using the global keyword we should only declare the variable but we can't assign/initialize the value. global a global b global c a = 111 b = 222 c = 333 print("The Given Global Variables after the modification are:") print("a = ", a) print("b = ", b) print("c = ", c) def function3(): # pass print("The Given Global Variables are:") print("a = ", a) print("b = ", b) print("c = ", c) function1() function2() function3() print("The Given Global Variables are:") print("a = ",a) print("b = ",b) print("c = ",c) ================================================== 3) Non-Local Variables ====================== keyword ==> nonlocal ==> a variables are not defined in either local or global scope are called as "non-local variables". ==> use in nested functions ==> function in function ==> nested function def outerFunction(): a = 15 b = 30 # a and b ==> local variables for outerFunction but these are global variables # in innerFunction print("a = ", a) # 15 print("b = ", b) # 30 def innerFunction(): nonlocal a nonlocal b a = 10 b = 20 print("a = ",a) print("b = ",b) return a+b print(innerFunction()) print("a = ",a) print("b = ",b) outerFunction() # print("a = ",a) # print("b = ",b) Function Aliasing ================== List ==> List Aliasing Set ==> Aliasing ==> function aliasing means assigning alterative name to the function. def greetings(name): print("Hi",name) print("Good Evening") greetings("Ravi") wishes = greetings # function aliasing wishes("Keerthi") greetings("Karthik")