Understanding Object-Oriented Programming in Python
Learn the basics of object-oriented programming in Python, including classes, objects, inheritance, and polymorphism.
Introduction to OOP
Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects and classes.
Classes and Objects
A class is a blueprint for creating objects, and an object is an instance of a class.
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
my_car = Car('Toyota', 'Corolla')
print(my_car.brand)
Inheritance
Inheritance is the process of creating a new class based on an existing class.
class ElectricCar(Car):
def __init__(self, brand, model, battery_size):
super().__init__(brand, model)
self.battery_size = battery_size
electric_car = ElectricCar('Tesla', 'Model S', 75)
print(electric_car.brand)
Polymorphism
Polymorphism is the ability of an object to take on multiple forms.
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
rectangle = Rectangle(4, 5)
circle = Circle(3)
print(rectangle.area())
print(circle.area())
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based python video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked python books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.