Types of Variables: =================== 1) Instance Variables ===================== The Instance Variables can allow to access with only object reference. ==> To Access the Instance variable, the syntax is: object-reference.instance-variable-name ==> To delete the data/value of instance variable: Syntax: del object-reference.instance-variable-name # Accessing of Instance Variables: class Test: def __init__(self): self.a = 100 self.b = 200 def met(self): self.c = 300 self.d = 400 t = Test() print(t.a) print(t.b) t.met() print(t.c) print(t.d) # deletion on instance variable del t.a print(t.a) 2) Static Variables 3) Method Variables 2) Static Variables ==================== the value of the variable, which is not varied from object to object is called as "static variable". the static variables can be have only one copy for the entire class and that copy can be shared among multiple objects of the class. static variables can be accessed with either class name or with object reference. Syntax: (using class name) class-name.static-variable-name Syntax: (using object reference) object-reference.static-variable-name Instance Variable Vs Static variable: ====================================== Static variables can keep the same copy among different objects of the same class. Whereas, the instance variable can keep the different copies among multiple objects of the same class. # Instance Variable Vs Static Variable class Test: p = 1221 # Static Variable def __init__(self): self.x = 100 # Instance Variable t = Test() t1 = Test() # Before the Change: print("t:",t.x,t.p) print("t1:",t1.x,t1.p) # Changing the static variable # Test.p = 1234 Test.p = 1234 t.x = 200 # After the change print("t:",t.x,t.p) print("t1:",t1.x,t1.p) ============================== Static variables can define: 1) inside the class directly but outside the from the method. 2) inside the constructor using "class name". 3) inside the method using class name. 1) inside the class directly but outside the from the method. ============================================================= # Defining the static variable in class class test: x = 1221 y = 2345 t = test() print(test.x) print(t.y) 2) inside the constructor using "class name". ============================================= # Defining the static variable in constructor using class name class test: def __init__(self): test.a = 9797 test.b = 9779 t = test() print(test.a) print(t.b) 3) inside the method using class name. ====================================== class Test: def met(self): Test.p = 1221 Test.q = 9779 t = Test() t.met() ========================================== Local Variables: ================ also called as "method variables" ==> which are having the scope within the method. ==> method variables can always define inside the method without "self" keyword and "class name". # Method Variables class test: def __init__(self): self.a = 100 self.b = 200 def met1(self): self.c = 300 test.d = 400 p = 12 q = 13 print(self.c) print(p,q) def met2(self): print(self.a,self.b) print(self.c) print(test.d) # print(p,q) t = test() t.met1() t.met2() t.met1() ======================================= Types of Methods: ================= three types: 1) Instance Methods 2) Static Methods 3) Class methods 1) Instance Methods =================== Method with instance variables. # Instance Methods class Student: def __init__(self,name,marks): self.name = name self.marks = marks def display(self): print("Name of the student = ",self.name) print("Marks of the student = ",self.marks) def grade(self): if self.marks >= 60: print("You Got First Grade.") elif self.marks >= 50: print("You Got Second Grade.") elif self.marks >= 40: print("You Got Third Grade.") else: print("You are Failed.") stu1 = Student("Ramya",78) stu1.display() stu1.grade()