class keyword in Python

Python class keyword is used to create classes in Python. Class is a OOPS concept, which is used to create objects. We will read about Python class in details in coming posts. Here we will study about class keyword

 

Syntax of class

class class_name :

attributes

 

  • In Python, classes can be created by using class keyword.
  • Class_name is name of class.
  • attributes are methods or variables of class_name

 

Example of class:

Let’s understand class by taking an example:

#Python class

class student :
    rollNo=1
    name="Rocky"
    std="5th"
    
print(student.name)

 

Output

Rocky

 

Explanation:

  • Here student is class.
  • rollNo, name and std are its attributes or variable of class.
  • Class variable can be accessed by using dot operator. When we wrote student.name its print Rocky as it has assign name Rocky.
Please follow and like us:
READ  How to use Python while loop | while loop in Python