Classes in Python

Classes in Python programming is a building block that leads to OOP. It is user-defined data type which combines data and methods for manipulating that data into one package.

Class Program

Program

class cls: #'cls' is name of Class.
    def fun(self): #'fun()' is name of function.
        print("This is Function")
    def fun2(self): #'fun2()' is name of function.
        print("This is Another Function")
obj = cls() #'obj' is object of class 'cls'.
obj.fun() #with the help of object 'obj' calling 'fun()' function.
obj.fun2() #with the help of object 'obj' calling 'fun2()' function.
 #Note: U also Write like 'class cls():',
 #      And 'self' is not Keyword in Functions.
Output
This is Function
This is Another Function

Program Class Another Method

Program

class cls: #'cls' is name of Class.
    def fun(self): #'fun()' is name of function.
        print("This is Function")
    def fun2(self): #'fun2()' is name of function.
        print("This is Another Function")
obj = cls() #'obj' is object of class 'cls'.
obj2 = cls() #'obj2' is another object of class 'cls'.
obj.fun() #with the help of object 'obj' calling 'fun()' function.
obj2.fun2() #with the help of object 'obj2' calling 'fun2()' function.
Output
This is Function
This is Another Function

Program Calling Functions of Class

Program

class cls: #'cls' is name of Class.
    def set(self): #this is function.
        self.a = input("Enter value for a: ") #assigning values to 'a'
        self.b = input("Enter value for b: ") #and 'b'.
    def put(self): #this is another function.
        print("a is: ",self.a) #printing 'a' and
        print("b is: ",self.b) #'b' value.
obj = cls() #'obj' is object of class 'cls'.
obj.set() #calling 'set()' function.
obj.put() #calling 'put()' function.
Output
Enter value for a: Bilal
Enter value for b: Khan
a is: Bilal
b is: Khan