Read Python Interview Questions and Answers for Beginners and Experienced

Python classs keyword

Python class keyword  is used to create the class in Python. class is like blueprint to create objects and methods. we will read about Python classes in other posts. In this post of Python Keywords, we will study about class keyword

 

Syntax of class keyword

class className:

.....

.....

 

  • class is keyword.
  • className is name of the class.

 

Example of class keyword

Let’s see some examples of class keyword


# Python class keyword

class Employee:
    id=1
    name="Joey"
    

print(Employee.name)

 

Output

Joey

 

  • In above example, Employee is class, which is created by using class keyword.
  • Employee class has two attributes id and name having some value.
  • we have printed Employee name value by using className.attributeName which is Employee.name.

 

Another Example of class with method


# Python class keyword

class Employee:
    id=1
    name="Chandler"
  
    def getName(self):
      return self.name

emp =Employee()
print (emp.getName())

 

Output

Chandler
  • In above example of Employee class, we have created one method getName().
  • Now we have created one emp object of class Employee.
  • After creating Employee class object we are calling method getName() through object.