CONSTRUCTOR: ============ 1) The constructor can define inside the class only. 2) Keyword: def 3) __init__() 4) The Constructor can accept the parameters/arguments 5) Constructor can be invoked automatically at the time of object creation. 6) In Python, there is only one constructor is allowed in the class. 7) Constructor can be used in python only for initialization. 8) The attributes of the class (Class variables) can always allowed to access in the constructor using "self" keyword. Syntax: self.var-name # Class with constructor class myClass: a = 100 b = 200 # Constructor without any parameters def __init__(self): print("Hi") print("Hello") print("Good Morning.") mc = myClass() print(mc.a);print(mc.b) ========================================== # Constructor with parameters class myClass: def __init__(self,a,b): sum_num = a + b print("The Sum of two numbers = ",sum_num) mc = myClass(100,200) ======================================== # Constructor with parameters class myClass: def __init__(self,a,b): sum_num = a + b # return sum_num print("The sum = ",sum_num) mc = myClass(100,200) ==================================================== Method Vs Constructor: ======================= 1) Name of the method can be any identifier. But the constructor name should always be "__init__" 2) Method need to be invoked to make the execute the method body. But the constructor can be invoked automatically at the time of object creation. 3) Per object, we can call method for any number of times. Whereas, per object the constructor can be called for only one time. 4) Method can allow to define with any business logic, whereas the constructor for only the initialization. class myClass: a = 100 b = 200 def __init__(self): sum_num = self.a + self.b print(sum_num) mc = myClass() ==================================== # Constructor can be for initialization # class myClass: # a = 100 # b = 200 # def __init__(self): # sum_num = self.a + self.b # print(sum_num) # # mc = myClass() class myClass: def __init__(self,a,b,c): self.a = a self.b = b self.c = c def met1(self): print("The Values of Constructor are:") print(self.a,self.b,self.c) mc = myClass(10,20,30) mc.met1() ============================================= TYPES OF VARIABLES ================== three types: 1) Instance Variables (Object Level Variables) 2) Static Variables (Class Level Variables) 3) Local Variables (Method Level Variables) Instance Variables: =================== ==> Also called as "Object Level Variables". ==> The values of variables can be varied from object to object, those variables are called as "Instance Variables". Where we can declare Instance Variables: ======================================== 1) Instance Variables can declare inside the constructor using "self" keyword. # Instance Variables class Employee: def __init__(self): self.eid = 1221 self.ename = "Krishna" self.edesignation = "Analyst" self.esal = "100000" def printData(self): print("The Employee Id = ",self.eid) print("The Employee Name = ",self.ename) print("The Designation = ",self.edesignation) print("The Salary = ",self.esal) emp = Employee() emp.printData() =============================================================== 2) Instance Variables can also declare inside the Instance Methods using "self" keyword. # Instance Variables inside the Instance Method class Test: def __init__(self): self.a = 10 self.b = 20 def met1(self): # Instance Method self.c = self.a + self.b def met2(self): print("The Instance Variables are = ") print(self.a,self.b,self.c) t = Test() t.met1() t.met2() =================================================== 3) These can define in outside of the class using object reference variable. # Instance Variables in outside the class. class myClass: def __init__(self): self.a = 100 self.b = 200 def m1(self): self.c = 300 mc = myClass() mc.m1() mc.d = 400 print(mc.__dict__) ============================== year = int(input("Enter a year:")) if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: print("This is a leap year.") else: print("This is not a leap year.")