Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Key Takeaways
Last Updated: Mar 27, 2024

Objects and Classes in Python

Author Malay Gain
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


In object-oriented programming, the concept of class and object is very essential. In Python, we also find such object-oriented characteristics. A class is a description of a set of objects that share the same properties (attributes), behaviour (operations), kind of relationships, and semantics. An object is an instance of a class. Here we will be discussing different properties of a class and object as well as how a class is defined, and an object is created.

Also see, Python Round Function.

Recommended topics, Floor Division in Python, and Convert String to List Python


A class can be considered a user-defined blueprint from which objects are created. Classes are used to bundle data and functionalities together so that their objects can be created or classified under different classes based on their functionalities. Objects of each class can have their attributes to maintain the state, and their state values can be modified using the member functions.

Let’s understand this concept through an example.

If we consider student as a class, the student class has nameageenrollment_id as its attributes, and studyeatsleep as its behaviour.

Defining class

#a class to denote student of CSE department
class Student:
  #calss attribute

  #constructor in Python
  def __init__(self,name,enr_id):
    #instance variable

  def get_name(self):
  def get_id(self):
    return self.enr_id

#object of Student class
student1= Student("Dhruv",5105133)

Data members(attributes)

Attributes are variables of a class. Attributes are public and can be accessed using the dot(.) operator. For example, to access the department attribute of the Student class we can use Student.department.

Self parameter

All class methods have self as their first parameter. We don’t provide any value to this parameter when we call a method. It is similar to this reference in C++, Java.

Constructor in Python: __init__( ) method

init method works as a constructor in Python. It is used to initialize objects of a class. This method is executed at the time of object creation.

# constructor in Python for Student class
def __init__(self,name,enr_id):
    #instance variable

Also See, Intersection in Python

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


Object is an instance of a class. Class is just a concept but an object is a real entity. All objects of a class have their own unique state that is determined by the values of their attributes. All instances of a class share its attributes and methods. When an object is created, a class is said to be instantiated. 

Creating object

#objects of Student class
student1= Student("Dhruv",5105133)

student2= Student("Kriti",5105134)

Two objects of student class are instantiated with different values. So, we can say both objects are in a unique state. 

It is said that “Everything in Python is an object”. It can be justified as every entity in Python has its attributes and associated functionality called methods that can be accessed by the dot(.) operator.

Let’s say if we consider list type.

l =[5,6,7,8]
l.__len__() ## returns length of the list = 4
dir(l) ## to get list of all attributes and methods of list object 




You can practice by yourself with the help of online python compiler.

Know What is Object in OOPs here in detail.

Check out Escape Sequence in Python


Q1. What is an instance variable?

Variables whose values are assigned inside the init method(constructor) or method with self, are called instance variables.

Q2. Is there any access modifier in the Python class?

Yes, there are three types of access modifiers. Such as Public, Private, Protected.

By default, members of a class are public.

Key Takeaways

This article covered concepts of class and object in Python with its code implementation.

We recommend you to check out the Coding Ninjas Studio Guided Path on Python programming for learning Python language from basics.

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here. 

Happy learning!

Next article
How everything in Python is an Object?
Live masterclass