Home »
Python
Python Classes and Objects
Last Updated : May 03, 2025
In Python, classes and objects are used to implement object-oriented programming. A class is a blueprint for creating objects, and an object is an instance of a class.
In this chapter, we will learn how to define classes and create objects with the help of examples.
Creating a Class in Python
To define a class in Python, use the class
keyword followed by the class name and a colon. The body of the class contains attributes and methods.
Example
In this example, we create a class named Student
with an attribute and a method.
# Defining a class
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def show_details(self):
print(f"Name: {self.name}, Age: {self.age}")
Creating Objects from a Class
Once a class is defined, you can create objects (instances) of that class and access its attributes and methods.
Example
In this example, we create two Student
objects and call their method.
# Creating objects
student1 = Student("Aarav", 20)
student2 = Student("Bhavya", 22)
# Calling the method
student1.show_details()
student2.show_details()
The output of the above code would be:
Name: Aarav, Age: 20
Name: Bhavya, Age: 22
Understanding the __init__
Method
The __init__
method is the constructor method in Python. It is called automatically when a new object is created.
Example
In this example, the constructor initializes the values for each student.
# Class with constructor
class Student:
def __init__(self, name):
self.name = name
print(f"Student {self.name} has been created.")
# Creating an object
s = Student("Charu")
The output of the above code would be:
Student Charu has been created.
Accessing Attributes and Methods
You can access attributes and methods of an object using the dot (.
) operator followed by the object_name.
Example
In this example, we access and print the values of object attributes directly.
# Creating a class
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
# Creating an object
car1 = Car("Tata", "Nexon")
# Accessing attributes
print(car1.brand)
print(car1.model)
The output of the above code would be:
Tata
Nexon
Adding Methods to a Class
Methods are functions defined inside a class that operate on instances of that class.
Example
In this example, the method display_info()
prints the details of the car.
# Creating a class
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"Car Brand: {self.brand}, Model: {self.model}")
# Creating an object
car2 = Car("Mahindra", "XUV700")
# Calling the method
car2.display_info()
The output of the above code would be:
Car Brand: Mahindra, Model: XUV700
Python Classes and Objects Exercise
Select the correct option to complete each statement about classes and objects in Python.
- The ___ keyword is used to define a class in Python.
- In Python,
self
refers to the ___.
- To create an object from a class named
Car
, use the syntax ___.
Advertisement
Advertisement