OOPs Day_02: ============ Methods with parameters: ======================== class class-name: def met1(self, par1, par2, par3, ..): implementation # methods with parameters class Student: def stuData(self,name, gender, section): self.name = name self.gender = gender self.section = section def printData(self): print(self.name) print(self.gender) print(self.section) stu = Student() stu.stuData("Ashok","Male","IX-C") stu.printData() =================================== Types of Variables: =================== two types: 1) Local Variables 2) Global Variables 1) Local Variables ================== the local variables are always allow to define within the particular block. ==> scope: limit to that particular block. # Local Variables def function(): name = "Ashok" age = 36 gender = "Male" print("Name = ",name) print("Age = ",age) print("Gender = ",gender) # def fun(): # print("Name = ",name) function() # print(name) # fun() ============================= Global Variable: ================ Global variables can always define at the beginning of the program We can access in anywhere of the program. # Global Variables name = "Ashok" age = 36 gender = "Male" def function(): print("Name = ",name) print("Age = ",age) print("Gender = ",gender) def fun1(): print("The Person {} is {} year's of old".format(name,age)) function() print(name) print(age) print(gender) fun1() ===================================== # Global Variables def fun1(a,b): print("Value1 = ",a) print("Value2 = ",b) def fun2(p,q): if p > q: big = p else: big = q return big """ when the function or method of the class is not having any implementation to define, we can write those with pass statement. without implementation of function/method if we need to execute without error, we can use pass statement. """ a = int(input("Enter a:")) b = int(input("Enter b:")) # global variables fun1(a = b,b = a) print("Biggest value = ",fun2(a,b)) print(a) print(b) ==================================== # Local Variable Vs Global variables class localGlobal: a = 100 b = 200 def m1(self): p = 100 print("Value1 = ",self.a) print("Value2 = ",self.b) def m2(self): print("Result = ",self.a + self.b) # print("p = ",p) print("Values of class = ",a,b) lg = localGlobal() lg.m1() lg.m2() print(lg.a) print(lg.b) ========================================== Changing of global variables: ============================= # changing of global variables a = 100 b = 200 def f1(): print("Using Function:") print("a = ",a) # 100 print("b = ",b) # 200 def f2(): global a,b a = 200 b = 400 # f2() print("Outside the function:") print("a = ",a) # 100 print("b = ",b) # 200 f1() # 100 200 f2() print("Outside the function:") print("a = ",a) # 200 print("b = ",b) # 400 f1() ============================== Method with Return Statement: ============================== # Method with return statement class Calculator: def m1(self,a,b): if a > b: big = a else: big = b return big cobj = Calculator() bigger = cobj.m1(100,200) print("The Biggest number = ",bigger) ======================================= Constructor: ============ ==> a special method in python class ==> always named with: "__init__" ==> In Python, the constructor can always use for the data definition only, not for any implementation. ==> to convert the local variables into class variables, we can use "constructor". Syntax: def __init__(self, par1, par2, ...): definition ==> The constructor need not be invoked extensively. ==> The constructor can be invoked automatically at the time of object creation. # sending of data to the class. # constructor can be used. class Employee: def __init__(self,ename, erole, esal): # local data converting to the class data self.ename = ename self.erole = erole self.esal = esal def employeeData(self): print("Employee name = ",self.ename) print("Employee Role = ",self.erole) print("Employee salary = ",self.esal) emp = Employee("Amisha", "Software Developer",100000) emp.employeeData()