Let’s explore the basics of Python together: PART-2

Ashimabha Bose
4 min readMay 18, 2023

Introduction to Object-Oriented Programming (OOP) in Python

Python is a versatile and powerful programming language known for its simplicity and readability. One of the key features that sets Python apart is its support for Object-Oriented Programming (OOP). OOP is a programming paradigm that enables you to organize your code into reusable objects, making it easier to manage and maintain complex projects.

In this beginner’s guide to OOP in Python, we will cover the fundamental concepts and principles of OOP. Each section will provide a clear explanation of the concept, along with practical examples in Python code.

Section 1: Introduction to Objects and Classes

In this section, we will explore the basics of objects and classes — the building blocks of OOP.

Objects: Objects are instances of a class and represent entities with properties and behaviors. They encapsulate data and the methods that operate on that data.

Classes: Classes are blueprints or templates for creating objects. They define the structure and behavior of objects.

In the code example, we define a class named “Car” that represents a car object. The class has attributes like brand, model, and year. We also define a method called “start_engine” that prints a message. We then create an object of the Car class and access its attributes and methods.

# Define a class
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year

def start_engine(self):
print("Engine started.")

# Create an object of the Car class
my_car = Car("Tesla", "Model S", 2022)
print(my_car.brand) # Tesla
my_car.start_engine() # Engine started.

Section 2: Encapsulation and Data Hiding

Encapsulation is the practice of bundling data and methods within a class. It allows us to control access to data and protect it from direct modification.

Data Hiding: Data hiding is a mechanism to restrict access to certain attributes or methods of a class. By using private variables (denoted by double underscores), we can prevent direct modification of the data.

In the code example, we define a class named “BankAccount” that represents a bank account. The class has attributes like account_number and balance. The balance attribute is hidden by prefixing it with double underscores. We provide methods to deposit money and retrieve the balance.

# Encapsulation example
class BankAccount:
def __init__(self, account_number, balance):
self.account_number = account_number
self.__balance = balance

def deposit(self, amount):
self.__balance += amount

def get_balance(self):
return self.__balance

# Create a BankAccount object
account = BankAccount("1234567890", 1000)
account.deposit(500)
print(account.get_balance()) # 1500

Section 3: Inheritance

Inheritance allows us to create new classes based on existing classes, inheriting their attributes and methods. It promotes code reusability and helps create a hierarchical structure of classes.

Superclass and Subclass: The existing class is called the superclass or base class, while the new class is called the subclass or derived class. The subclass inherits attributes and methods from the superclass.

In the code example, we define a superclass called “Animal” with a method named “sound.” We then create two subclasses, “Dog” and “Cat,” which inherit the “sound” method from the Animal class but provide their own implementation.

# Inheritance example
class Animal:
def __init__(self, name):
self.name = name

def sound(self):
pass

class Dog(Animal):
def sound(self):
return "Woof!"

class Cat(Animal):
def sound(self):
return "Meow!"

# Create objects of the Dog and Cat classes
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.sound()) # Woof!
print(cat.sound()) # Meow!

Section 4: Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables us to write flexible and adaptable code that can work with objects of various types.

Method Overriding: In polymorphism, method overriding occurs when a subclass provides its own implementation of a method inherited from the superclass.

In the code example, we define a superclass called “Shape” with a method named “area.” We then create two subclasses, “Rectangle” and “Circle,” which override the “area” method with their own implementation.

# Polymorphism example
class Shape:
def area(self):
pass

class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width

def area(self):
return self.length * self.width

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius * self.radius

# Create objects of the Rectangle and Circle classes
rectangle = Rectangle(5, 10)
circle = Circle(7)
print(rectangle.area()) # 50
print(circle.area()) # 153.86

Conclusion

Understanding the core concepts of Object-Oriented Programming is essential for building robust and maintainable Python applications. In this blog, we covered the basics of objects and classes, encapsulation and data hiding, inheritance, and polymorphism. By mastering these concepts and practicing them through code examples, you will be well on your way to becoming a proficient Python programmer.

Stay tuned for the next blog post in this series, where we will dive deeper into advanced OOP concepts and techniques.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ashimabha Bose
Ashimabha Bose

Written by Ashimabha Bose

Senior Business Analyst | Power BI | Digital Marketer | Data Analyst | AI Enthusiast

No responses yet

Write a response