Python class Keyword
The Python class Keyword is a case-sensitive. It is used to define a class. The ClassName is followed by class keyword. The classname should be in Pascal case. It is used in OOP's[Object Oriented Programming] concept.
class is a user-defined blue print from which objects are created. For implementation of class we need to create an object.
Syntax
Following is the syntax of Python class keyword −
class ClassName:
statement1
statement2
Example
Following is the basic example of the Python class Keyword −
#class is defined
class TutorialsPoint:
print("Welcome to Tutorials point")
#Object is defined
var1=TutorialsPoint()
Output
Following is the output of the above code −
Welcome to Tutorials point
Empty class
In Python, We can also created an empty class. The class without implementation is known as Empty class. It can be obtained using pass keyword.
Example
Following is an example for empty class −
class Animal:
pass
object1=Animal()
Output
As we have created an empty class there will be no output.
Using 'class' with attributes
We can also pass an attributes in a class using special constructor called __init__. This method is executed when object is been created.
Example
Here, is an example of class with attributes −
class Marks:
def __init__(self,Name,RollNo,Marks):
self.Name = Name
self.RollNo = RollNo
self.Marks = Marks
def Results(self):
if self.Marks>75:
print(self.Name,"with rollno",self.RollNo,"passed with a percentage of ", self.Marks)
Student1=Marks('Sai',297,89)
Student1.Results()
Output
Following is the output of the above code −
Sai with rollno 297 passed with a percentage of 89
Using 'class' with methods
The functions which are defined inside the class is known as methods. These methods can be called by object.method-name().
Example
Following is an example of a class with methods −
class TutorialsPoint:
def Python(self):
print("Welcome to Python Tutorials")
def Java(self):
print("Welcome to Java Tutorials")
object1=TutorialsPoint()
object1.Python()
object1.Java()
Output
Following is the output of the above code −
Welcome to Python Tutorials Welcome to Java Tutorials