Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconA Complete Guide on OOPs Concepts in Python

A Complete Guide on OOPs Concepts in Python

Last updated:
22nd Oct, 2020
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
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.


Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1Why 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.

2What 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.

3What 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.

Explore Free Courses

Suggested Blogs

Data Mining Techniques &#038; Tools: Types of Data, Methods, Applications [With Examples]
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno
Read More

by Rohit Sharma

07 Jul 2024

An Overview of Association Rule Mining &#038; its Applications
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or
Read More

by Abhinav Rai

07 Jul 2024

What is Decision Tree in Data Mining? Types, Real World Examples &#038; Applications
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on
Read More

by Rohit Sharma

04 Jul 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

04 Jul 2024

Most Common Binary Tree Interview Questions &#038; Answers [For Freshers &#038; Experienced]
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par
Read More

by Rohit Sharma

03 Jul 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right
Read More

by Rohit Sharma

02 Jul 2024

Graphs in Data Structure: Types, Storing &#038; Traversal
In my experience with Data Science, I’ve found that choosing the right data structure is crucial for organizing information effectively. Graphs
Read More

by Rohit Sharma

01 Jul 2024

Python Banking Project [With Source Code] in 2024
The banking sector has many applications for programming and IT solutions. If you’re interested in working on a project for the banking sector,
Read More

by Rohit Sharma

25 Jun 2024

Linear Search vs Binary Search: Difference Between Linear Search &#038; Binary Search
In my journey through data structures, I’ve navigated the nuances of linear search vs binary search in data structure, especially when dealing w
Read More

by Rohit Sharma

23 Jun 2024

Schedule 1:1 free counsellingTalk to Career Expert
footer sticky close icon