A Complete Guide on OOPs Concepts in Python

Object-Oriented Programming or OOP has been an integral part of the software world for a while now. It is an excellent programming paradigm that offers a certain level of freedom and enhances the programming accomplishments for the developer. 

There are certain basic concepts and principles that every developer should know. They drive the premise for developing an application using this model.

Here, we are going to take a look at some of the crucial yet basic OOPs concepts in Python. These concepts drive the programmers towards achieving better results as well as to model their apps in an enriching fashion.

Learn Data Science Courses online at upGrad

What Is Object-Oriented Programming?

For the uninitiated, this is where we are going to begin our induction. Let’s understand OOPs as a layman would, to get a deeper understanding of how we can work it out. 

In this programming structure, you can easily combine things with similar properties or behaviors into a single object. If you are talking about demographics, then that becomes a single object for this programming model. Similarly, if you are talking about actions to be taken, then that becomes another object.

They are all objects that contain raw data. Your software solution will program these objects in a way that they fulfill the end goal together. These objects will perform a few actions together, as programmed by you, and deliver the result.

An excellent example to help understand this would be an email program. 

Let’s say you have one object that contains the email contents, such as the recipient’s name and the subject. There is a second object that details the attachment and sending the email. You will design a program that will automatically combine these objects, ensure that a fully-written email is ready with the desired attachments, and sent it to the recipient. 

Explore our Popular Data Science Certifications

This is how OOP works. However, some concepts drive this into action. Let’s take a quick look at these concepts and how they function.

Read: Is Python an Object-Oriented Language?

How to Define a Class?

The class is what defines every object, and is an important aspect of OOPs concepts in Python. Let’s say you have four objects, such as eyes, ears, mouth, and nose. These are parts of a face, which is the class. 

Let’s get started with how to define a class.

Let’s say you are talking about a class called email.

Class Email:


This is how the class is defined. Use the “pass” so that when you run the code in a compiler, there are no bugs issued. 

Only defining the class may not help you much. You need to add some properties to make it attractive and the code helpful. So, you need to add objects to your class.

The ._init_() method is what would prove to be useful when defining the properties within a class.

This will ensure that every single time you create a new object for the class, this method will set the parameters to the initial state, thus, initializing a new object every single time. 

The first parameter for this method would always be self, even when you assign multiple parameters to it. 

So, what happens in this case? Let’s say you created a new class. The instance is transferred to the self parameter. As a result, the ._init_() can assign fresh properties to the defined object. 

If you are still pondering on how to use it, let’s understand it via an example.

Class Email:

                  def ._init_(self, name, recipient, address):

                           = name

                                    self.recipient = recipient

                                    self.address = address

Let’s break this code down for better understanding. 

The indentation expressed in the code above is critical. You should match the same when writing your program using OOPs in Python. Let’s understand the self variable in the above code.

The produces an attribute that is called name. The value of the name parameter is assigned to this attribute. Similarly, attributes and values are assigned for the other-self variables too.

These attributes are known as instance. You will need to specify the value of every attribute mentioned within a particular instance. Let’s say that there are two types of emails – welcome and nurture. The email recipient would differ for both instances (welcome emails and nurture emails).

Class attributes, on the other hand, are different; they will contain the same value for all class instances. For instance, these are all inbound emails. This is the class attribute that you can define for them.

Class Email:

                  #Class Attribute  

                  Email: “Inbound Emails”

                  def ._init_(self, name, recipient, address):

                           = name

                                    self.recipient = recipient

                                    self.address = address

Learn about: How to Code, Compile and Run Java Projects

Top Data Science Skills to Learn

Understanding Class and Instance Variables

Let’s take the above example where we have created a class attribute as well as two instance variables to understand OOPs in Python better. 

Class Email:           

                  Email: “Inbound Emails”

                  def ._init_(self, name, recipient, address):

                           = name

                                    self.recipient = recipient

                                    self.address = address

Let’s talk about instantiating the objects within this class. You need to set values for each of these objects. This will be the initial value for every object, as discussed in the earlier part of the guide. In case you don’t assign values to these objects, then you will receive a TypeError. 

>>> Email()

Traceback (most recent call last):

     File “<pyshell#6>”, line 1, in <module>

          Email ()

TypeError: __init__() missing 4 positional arguments: ‘name’, ‘recipient’, and ‘address’

We can add value immediately after the class name.

Nurture Email = Email (Nurture, David,

Welcome Email = Email (Welcome, Daisy,

With the above code, we have generated two instances, for a nurture email and a welcome email. Let’s check the __init__() defined after the class instance for the above code. We see four parameters, including self. However, no variable has been mentioned for self. Let’s understand why?

So, whenever you create a new object within a class, you are instantiating the class. You will be assigning a memory address while doing this, where the variable will be stored. As soon as you instantiate a new object, Python will automatically create an instance, and it will be passed to the first variable in the .__init__() method. As a result, the self is removed in this method. 

With the class variable, you can ensure that every instance you have created has a variable or value associated with it. This will ensure quicker and easier results, and thus, safer conclusions for your programming. 

Also Read: Must Read 47 OOPS Interview Questions & Answers For Freshers & Experienced

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

Summing Up

We have understood the various aspects of creating a class, object, and method using the Python OOPs programming model here. Apart from this, we have also understood concepts such as instances and instantiating, which are core to OOPs programming in Python. Next, you will need to understand the principles that govern these instances and classes such as inheritance, polymorphism, abstraction, and encapsulation. They are core OOPs concepts in Python that drive your application and its results.

Check out all trending Python tutorial concepts in 2024.

To move further in the Python funnel, you need to have your basics clear and rooted. So, dive into classes and understand how it works. Play around a bit with these variables, and understand their outcomes before you can set-up your application. 

If you are curious about learning data science to be in the front of fast-paced technological advancements, check out upGrad & IIIT-B’s PG Diploma in Data Science and upskill yourself for the future.

Why do we use OOPs?

Object-Oriented Programming (OOP) is a programming framework that relies on the development of reusable 'objects' with their attributes and behavior that can be acted on, manipulated, and packaged. Python supports both OOP and other frameworks. In Python, you achieve OOP by using classes. Python has all of the usual characteristics of object-oriented programming. Developers utilize OOP in their Python applications because it makes codes more reusable and working with larger programs easier. Because a class can only be defined once and utilized multiple times, OOP systems prevent you from duplicating code. As a result, OOP makes it simple to follow the 'Don't Repeat Yourself' (DRY) concept.

What is a Class?

A Python class is similar to a blueprint for generating a new object. An object is anything that you want to modify or change while coding. When we instantiate a class object, which happens when we define a variable, a new object is created from scratch. Class objects can be reused as many times as needed. A class is used to construct each item. Consider this to be the blueprint for a certain sort of item. Classes list the attributes that are required for that type of object but do not assign a value to them. Classes also specify methods that are shared by all objects of the same type.

What is an instance variable?

A class instance owns instance variables in Python. An instance variable's value might change based on the instance with which it is linked. This is different from a class variable, which can only have one value assigned to it. Instance variables are declared within a class method. When we declare a class, we may assign values to instance variables. When we declare the class, we provide the values we wish to assign as arguments.

Want to share this article?

Become a Full Stack Developer

Enroll Today

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks