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.
SyntaxFollowing is the syntax of Python class keyword −
class ClassName: statement1 statement2Example
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 pointEmpty 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.
ExampleFollowing 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 attributesWe can also pass an attributes in a class using special constructor called __init__. This method is executed when object is been created.
ExampleHere, 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 89Using 'class' with methods
The functions which are defined inside the class is known as methods. These methods can be called by object.method-name().
ExampleFollowing 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4