Introduction to OOPs in Python. Topic – 39

Introduction to OOPs In Python

Introduction to OOPs in Python

In this post, we will give an introduction to OOPs in Python. Object Oriented Programming is the concept of writing clean, maintainable, and secure code. OOP increased the modularity, maintainability, and scalability of the code. Most of the programmer write their code using the OOPs concept. The main concept of object-oriented Programming (OOPs) or oops concepts in Python is to bind the data and the functions that work together as a single unit so that no other part of the code can access this data.

In programming languages, mainly there are two approaches that are used to write programs or code.

  •     Procedural Programming
  •     Object-Oriented Programming

The procedure we are following till now is the “Procedural Programming” approach. So, now we will learn about Object Oriented Programming (OOP). The basic idea of object-oriented programming (OOP) in Python is to use classes and objects to represent real-world concepts and entities.

What is Class?

A class is a blueprint or template for creating objects. It defines the properties and methods that an object of that class will have. Properties are the data or state of an object, and methods are the actions or behaviors that an object can perform.

Just imagine a railway ticket form. In a form, everything is predefined. We take the form and input values into it. A class is also the same thing.

Example:

Python
class RailwayTickit:
    pass

What Is Objects?

Objects are instances of a class. They represent real-world entities and encapsulate both data (attributes) and behavior (methods). An object is created using the class keyword followed by the class name and parentheses.

For example, you could create a class called “RailwayTickit” that has properties such as name and age, and methods such as speak() and walk(). Each instance of the Person class would be a unique object with its own name and age, but they would all have the same methods to speak and walk.

Python
obj = RailwayTickit()

Creating Class and Object:

Python
class Dog:

    # class attribute
    attr1 = "mammal"

    # Instance attribute
    def __init__(self, name):
        self.name = name
        
    def speak(self):
        print("My name is {}".format(self.name))

# Driver code
# Object instantiation
Rodger = Dog("Rodger")
Tommy = Dog("Tommy")

# Accessing class methods
Rodger.speak()
Tommy.speak()

#output
# My name is Rodger
# My name is Tommy

Main features of OPPs:

Encapsulation:

Encapsulation is the bundling of data and methods related to an object within a single unit, hiding the internal details from the outside world. It promotes data protection and controlled access to object properties.

In Python, encapsulation is achieved using access modifiers like public, private, and protected to control the visibility of attributes and methods.

If we think of a medicine capsule, there are many small medicines inside the capsule, but we cannot normally access them. There is a cover that protects all the medicines inside. Encapsulation in Python is the same.

Inheritance:

Inheritance is the ability of a class to inherit attributes and methods from another class, known as the parent class. This allows for code reuse and promotes a hierarchical relationship between classes.

Suppose we have a cycle. So, we added an engine to it and made it a bike. So, we had a cycle and we added new features to it to make a bike. So, we inherited from the cycle to make a bike.

Polymorphism:

Polymorphism is the ability of objects of different classes to respond to the same method call in different ways. It allows for flexible and dynamic behavior based on the type of object.

‘is’ vs ‘==’ Operators In Python. Topic – 54

‘is’ vs ‘==’ Operators In Python

In Python programming, there are two more useful operators. The first one is ‘=='(double equals to) and another one is ‘is’….

I hope you understand “Introduction to OOPs in Python. Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

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

ABOUT ME
Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog