Object-Oriented Programming in Python

Shrish Last Updated : 21 Mar, 2025
10 min read

Object-Oriented Programming (OOP) is a way of writing code that organizes software design around objects, which are like real-world things. In Python, OOP helps you create reusable and structured programs. Instead of writing everything step-by-step, you group related data and actions into objects. For example, a “Car” object could have data like color and speed, and actions like “drive” or “stop.” Python makes OOP easy with features like classes, which act as blueprints for objects. By using OOP, you can write cleaner, more efficient code that’s easier to understand, update, and share with others.In this blog, we will be learning everything that we need to know for object-oriented programming in Python. At the same time, we will also be implementing those object-oriented concepts in Python as well.

This article was published as a part of the Data Science Blogathon.

What is Object-Oriented Programming in Python?

Object-Oriented Programming (OOP) in Python is a way of organizing code by creating reusable “objects” that bundle data (attributes) and actions (methods) together. It focuses on concepts like classes (blueprints for objects), inheritance (reusing code), encapsulation (hiding data), and polymorphism (using objects in different ways).

Basics of Object-Oriented Programming(OOP) in Python

For learning the crux of Object-Oriented Programming in Python, We will be learning the following topics in this article both the theory part and implementation part.

Topics to be covered:-

  1. Classes
  2. Objects
  3. Pre-requisites
  4. Attributes
  5. Methods

Let’s embark on our journey of learning OOP in Python!

Pre-requisites

In this article, since we are going to implement those concepts as well here are the pre-requisites:-

  1. Anaconda Distribution should be installed on the computer
  2. Jupyter notebook(will be installed automatically when anaconda distribution is installed)
  3. Either Windows OS or Linux-based distribution ex. Ubuntu

Here I will be implementing all those concepts in the Jupyter notebook in Ubuntu 20.04.2 LTS. Again a selection of OS does not affect as it is a personal preference. All the concepts that we are going to learn will be similar irrespective of the operating system used.

Class in OOP

What are classes in OOP? Let’s understand this concept easily with the help of a picture.

  • If we analyze the above image, we are seeing class, object, attributes, methods, and a Pikachu there :). You might be thinking about what all of this has to do with OOP.  Well, the above image is exactly what we need to understand OOP concepts easily.
  • So moving to the first topic. What is meant by class in OOP? The technical definition of class is “Class is a collection of an object of the same type“.Let’s relate this to our example above.
  • Here in this image, we can see there is a rectangle name “Pokemon”. This is basically the name of our class. In the simplest term class is nothing but a collection of objects. So all the Pokemon in the Pokemon universe will be under the “Pokemon” class.

Object in OOP

In OOP, an object is basically a constituent or member of a class. Taking into consideration our pokemon example, each Pokemon is an object of the Pokemon class. For example. Pikachu is an object of Pokemon class.

Attributes in OOP

Back to our Pokemon example. Let’s focus on Pikachu in the image. Just observed it for a moment. What are you seeing?

  • Pikachu has the following visible features:
  1. Two ears
  2. Two hands
  3. Two legs
  4. Two small red-colored dots on its cheeks
  5. A distinguishing yellow color
  • Additional information from the adjoining table in the image includes:
  • Name = “Pikachu”
  • Type = “Electric”
  • Health = 70
  • These features and details are referred to as attributes.
  • Attributes are elements that provide extra information about classes or objects.
  • In this case, the attributes give additional information about the Pikachu object, such as:
  1. Color
  2. Health
  3. Type

Read this article about the Methods in Python of Object Oriented Programming

Methods in OOP

In our example, under the bottom three lines, we are seeing something like attack(), dodge(), and evolve(). Those are known as methods. To explain it more simply, methods are basically actions that are associated with a particular object. In this case, those actions as associated with our Pikachu object.

Now till this point, we have a theoretical understanding of various object-oriented concepts in Python. Now let’s implement those concepts in python using the jupyter notebook.

Here I am going to implement those concepts using Jupyter notebook in Ubuntu 20.04.2 LTS but. Again the choice of OS is a personal preference as all the concepts that we are seeing here will be similar across any platform.

Practical Implementation of OOP  in Python

Let’s start implementing OOP concepts in Python. Here we will be coding using a jupyter notebook. Again the choice of IDE is a personal preference. Being a data scientist I have spent most of my time using jupyter notebook and hence I am using the same here for OOP as well.

Creating class in Python

 Let’s create our class named “Pokemon” in python.

Code For Class Creation :

#Creating class named "Pokemon"
class Pokemon():

As you can see from the code above, there is a certain structure that needs to be followed while creating a class in python. The convention is to start with the keyword named “class”. In this way, python can recognize that whatever we are trying to create is going to be considered a class. The keyword class is then followed by the class name which is Pokemon in this case. Eventually, it is followed ( ) which then again followed by “:” character.

Now you might have a question – “is our class creation completed ?”.Well not yet. There are still some lines of code that we need to write down in our class to complete its creation.

Creating __init__ method for the class

Continuing with our class creation task, the next step involved the creation of a very special method named “__init__”. You might ask “What is this __init__ method and why it is called so?”. Well to understand this concept easily, consider the init method similar to the main method in Java.

If we analyze any java code, we can that with every class there is the main method associated. The same is the concept in Python as well. Here instead of calling it the “main” method, it is known as the “__init__” method. Let’s add this code to our class.

Code For Creating __init__( ) method for the class:

#Creating class named "Pokemon"
class Pokemon():
             def __init__():

As you can see from the code above, here we have created our __init__ method in Python. Here we are noticing one thing which is the keyword named “def”.What is the purpose of this def keyword here? Basically def is a keyword in python that we use specifically which defining method for an object.

Also here you are noticing one thing. By default, python is aligning some indentation in the code that we are writing. We are witnessing what is known as “Syntax Engine” in python. This syntax engine operates on its own. Its main purpose is to make our code look neat and clean.

Since we have defined our __init__( ) method for our class, it’s still not completely defined. Still, certain arguments need to be passed in the __init__( ) method to complete its definition.Let’s add those arguments in our __init__( ) method.

Code for Defining attributes associated with class:

#Creating class named "Pokemon"
class Pokemon():
           def __init__(self,name,Type,health):
                    self.name = name
                    self.Type = Type
                    self.health = health

As you can see from the code above, we have passed lots of arguments in our __init__( ) method. Now let’s backtrack a little to understand what parameters are being passed here.

Remember a little while ago, we discussed that our Pikachu is having yellow colour, two red dots on cheeks, etc. Well, those are basically attributes associated with an object and in this __init__ ( ) method we are simply passing defining those attributes.

You might be having another question now – “What is the use of keyword self and what is the below self. line of codes doing?”. To explain this in simple terms, “self” is basically a keyword that is responsible for connecting those attributes to our class which is pokemon in this case.

Below 3 lines of codes “self…” is responsible for connecting those attributes to our class. Now let’s proceed to further part

Creating Methods For Class

Till this point, we have declared our class, special method named __init__ ( ) and attributes for the class. Now let’s proceed with our final part which is method creation.

Code for Method creation in Python:

def attack(self):
           print("Electric attack!!.Zhoop!!!")

As you can see from the code above, we are creating a method named attack() here. If you observed this piece of code carefully, you can see that for this method we have passed an argument named “self” here. The reason for this is the same again. Here we are connecting this method to our class using the self keyword.

If you see our example image above, with our Pokemon class there are three methods associated – attack(), Dodge(), Evolve(). Here since we have already created our attack() method, let’s incorporate the remaining two methods in our class.

Once we incorporate all those changes, our final Pokemon class will look like this

Pokemon Class with attributes and methods incorporated:

#Creating class named "Pokemon"
class Pokemon():
          def __init__(self,name,Type,health):
                self.name = name
                self.Type = Type 
                self.health = health

           def attack(self):
                print("Electric attack!!.Zhoop!!!")


           def dodge(self):
                print("Pikachu Dodge!")

          def evolve(self):
               print("Evolving to Raichu!!!!")

Now the only thing remaining is to execute our line of code and we are done with class creation. Once this line of code is executed, you will see something like this in a jupyter notebook.

oop in python pokemon

The next part of our OOP journey is to create an instance of a class that is basically an object.

Object Creation in Python

Now let’s continue with the creation of an object in Python.

Code For Object Creation in Python:

#Creating object of "Pokemon" class
pk1 = Pokemon("Pikachu","Electric",120)

Here you can see that we have created an object named pk1.

Accessing attributes associated with the object

Attributes associated with an object can be accessed by using. (Dot) operators. Let’s access all attributes associated with our pk1 object in the jupyter notebook.

Code For Accessing name attribute of pk1 Object:

#Creating class named "Pokemon"
class Pokemon():
    def __init__(self,name,Type,health):
          self.name = name
          self.Type = Type 
          self.health = health
  
    def attack(self):
          print("Electric attack!!.Zhoop!!!")
  
  
    def dodge(self):
          print("Pikachu Dodge!")
  
    def evolve(self):
         print("Evolving to Raichu!!!!")

#Creating object of "Pokemon" class
pk1 = Pokemon("Pikachu","Electric",120)
print(pk1.name)
          

In the above code, we can see that we have accessed the name attribute associated with our pk1 object. In the same manner, let’s access all attributes associated with our object.

Code For Accessing type attribute of pk1 Object:

#Accessing Type attribute of pk1 object
pk1.Type

Output:

'Electric'

Accessing Methods associated with the object

We can also access methods associated with our object using the dot operator. So let’s quickly access all methods associated with our pk1 object.

Code For Accessing attack() method of pk1 Object:

pk1.attack()

Output:

Electric attack!!.Zhoop!!!

Code For Accessing evolve() method of pk1 Object:

#Accessing evolve() method of pk1 object
pk1.evolve()

Output:

Evolving to Raichu!!!!

Code For Accessing dodge() method of pk1 Object:

#Accessing dodge() method of pk1 object
pk1.dodge()

Output:

Pikachu Dodge!

Here one important thing to note is that while accessing methods associated with our object, we need to put () after the method name.

Inheritance in OOP

Inheritance is one of the key concepts in OOP. Let’s understand this concept easily with the help of a simple example.

Let’s consider an example of a supercar ex. McLaren 720S spyder. It’s basically a supercar. For those of you interested in cars, this is what I am talking about.

  • The car shares common characteristics with most other cars, such as doors, a windshield, 4 wheels, headlights, and taillights.
  • As a supercar, it also has unique custom features, including:
  1. A 720 Horsepower V8 turbocharged engine.
  2. A custom-made gearbox developed by McLaren.
  3. A customized All-Wheel Drive (AWD) system.
  • In Object-Oriented Programming (OOP) terms, this car can be seen as inheriting features from its parent class, which in this case is the “car” class.
  • This demonstrates the concept of Inheritance, where an element (like the McLaren 720S Spyder) acquires characteristics from its parent class.
  • The McLaren 720S Spyder inherits all common features from the “car” class while also possessing its own special features.

Superclass and Subclass

The class from which our element is inheriting (Car class in this case) is known as Superclass and it is generic in nature. While the class which is inheriting those characteristics is known as sub-class which is specific in nature.

Let’s implement those concepts in Python now. In this case m, we will create two classes – One is going to be a class named “Car” and is going to be our superclass. Another class is going to be a class named “McLaren” and is going to be our subclass. This “McLaren” class will inherit all properties of the “Car” class.

Creation of Car class in Python:

class Car():
         def __init__(self,ndoors,nwheels,tailLight,headLight):
               self.ndoors = ndoors
               self.nwheels = nwheels
               self.tailLight = tailLight
               self.headLight = headLight

 

Here we have created our first class named “Car”. Now we are going to create our new class named “McLaren” which is going to inherit all attributes of this class

You can also check the article about the 30 MCQ on Python OOPs Cocnepts

Creation of McLaren class in Python:

class McLaren(Car):
         def __init__(self,ndoors,nWheels,tailLight,headLight,Engine,Wheel_Drive):
                 Car.__init__(self,ndoors,nWheels,tailLight,headLight)
                 self.Engine = Engine
                 self.Wheel_Drive = Wheel_Drive

       def Drive(self):

                 print("I am driving McLaren 720S Spyder")

As you can see in this code above, since our McLaren class is inheriting the Car class, we have mentioned Car as one of the parameters while creating our McLaren class and this basically represents this inheritance mechanism in Python.

Now it’s time to test our theory about whether our McLaren class is inheriting features of the Car class or not.

Code For Creating Object of McLaren Class:

#Creating an object of McLaren Class
mk1 = McLaren("4","4","Yes","Yes","V8","AWD")

Now let’s try to access attribute which is specific to McLaren class

Code For Accessing engine attribute of McLaren class:

#Aceesing Engine attribute for McLaren Class
mk1.Engine

Output:

'V8'

Code For Accessing Wheel Drive attribute of McLaren class:

#Accessing Wheel_Drive attribute for McLaren Class
mk1.Wheel_Drive

Output:

'AWD'

Here we can see that we are able to access attributes that are specific to the McLaren object. Since here we are inheriting our base class which is Car class in this case, let’s see if those attributes from the base class have been inherited or not in our McLaren class.

Code For Accessing inherited attributes:

headLight inherited attributes:

mk1.headLight

Output:

Yes'
Accessing ndoors inherited attribute:

mk1.ndoors

Output:

'4'

Accessing nwheels inherited attribute:

mk1.nwheels

Output:

'4'

Accessing tailLight attribute:

mk1.tailLight

Output:

'Yes'

As we can see here, our newly created object of McLaren class is inheriting all features of the Car base class indicating that inheritance is being implemented correctly in this context.

Conclusion

Object-Oriented Programming (OOP) in Python is a way to structure code by creating reusable components called classes. These classes act as blueprints for creating objects, which are instances of the class. Each object has attributes (data) and methods (functions) that define its behavior. To use OOP, you first create a class using the class keyword, define an __init__ method to initialize attributes, and add methods to perform actions. Objects are created by calling the class, and their methods are accessed using dot notation. OOP makes code organized, reusable, and easier to manage, especially for larger projects. It’s a powerful tool for solving real-world problems efficiently.

My name is Shrish. I have been working as a data scientist at EY. I love technology and during my free time, I try to use my skills to create something awesome in python so that it can be shared on the analyticsvidhya platform

Login to continue reading and enjoy expert-curated content.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details