Python | Introduction to Object Oriented Programming

Python | Introduction to Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that has been used widely in software development. In Python, OOP is a popular approach used by developers to create robust, scalable, and maintainable software applications. In this article, we will explore the concept of OOP in Python, its benefits, and some examples.

What is Object-Oriented Programming?

OOP is a programming paradigm that focuses on creating objects that have attributes (data) and behaviors (methods). The objects are instances of classes, which are blueprints or templates for creating objects. A class defines the properties and methods that an object of that class will have. In other words, a class is a user-defined data type that encapsulates data and methods into a single unit.

Benefits of Object-Oriented Programming:

OOP has several benefits that make it a popular programming paradigm. Some of these benefits include:

1) Reusability: With OOP, you can reuse code by creating classes and objects that can be used in multiple programs.

2) Modularity: OOP promotes modular programming, which makes it easier to maintain and update code.

3) Encapsulation: OOP encapsulates data and methods within a class, which makes it easier to manage and control access to data.

4) Polymorphism: OOP allows you to use the same method name for different classes, which makes it easier to write code that works with multiple classes.

Let’s look at some examples of how OOP is used in Python.

Example 1: Creating a Class and Object.

The below code creates a simple class called “Person” with two attributes (name and age) and a method called “printInfo” that prints the name and age of the person.

# creating a Person class
class Person:
    # define a constructor
    def __init__(self, name, age):
        # define attributes = name
        self.age = age

    # define a method    
    def printInfo(self):
        print("Age:", self.age)

To create an object of the “Person” class, you can use the below code:

person1 = Person("John", 25)

To call the “printInfo” method of the person1 object, you can use the below code:



Name: John
Age: 25

Example 2: Inheritance

Inheritance is a concept in OOP that allows you to create a new class from an existing class. The new class inherits all the properties and methods of the existing class and can add new properties and methods.

The below code creates a “Student” class that inherits from the “Person” class and adds a new attribute called “studentId” and a new method called “printStudentInfo”.

# creating a Student class
# which inherits Person class
class Student(Person):

    # define a constructor
    def __init__(self, name, age, studentId):
        # calling constructor of a parent class
        super().__init__(name, age)
        self.studentId = studentId
    # define a method    
    def printStudentInfo(self):
        print("Student ID:", self.studentId)

To create an object of the “Student” class, you can use the below code:

student1 = Student("Mary", 20, 1234)

To call the “printStudentInfo” method of the student1 object, you can use the below code:



Name: Mary
Age: 20
Student ID: 1234

OOP is a powerful programming paradigm that provides several benefits, such as reusability, modularity, encapsulation, polymorphism, and abstraction. Python, with its simple syntax and dynamic typing, is an excellent language for implementing OOP concepts.

In this article, we explored two examples of OOP in Python. The first example demonstrated how to create a simple class with attributes and methods, while the second example demonstrated inheritance, which is a powerful feature of OOP.

When designing software, it’s important to consider whether OOP is the right approach for your project. While OOP has several benefits, it’s not always the best approach for every situation. Understanding the strengths and weaknesses of different programming paradigms is an essential skill for any software developer.

Leave a Reply

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

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call

If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds