๐Ÿ Lesson 19: Python Classes & Object-Oriented Programming (OOP)

Welcome to Lesson 19! Today we’ll dive into Object-Oriented Programming (OOP) in Python. OOP helps you create structured, reusable, and organized code using classes and objects.


⭐ What You Will Learn in This Lesson

  • Understand the fundamentals of Object-Oriented Programming (OOP)
  • Learn to define and use classes and objects
  • Understand the importance of the __init__ method
  • Learn about inheritance in Python
  • Apply OOP principles to create reusable, modular code

๐Ÿ‘ฅ Who Is This Lesson For?

  • Beginners and intermediate Python developers looking to learn OOP
  • Anyone interested in building scalable and modular Python applications
  • Developers planning to work with frameworks like Django or Flask

๐Ÿ“ฆ What Is a Class?

A class is like a blueprint for creating objects. It defines attributes (variables) and methods (functions) that the objects will have.


๐Ÿงฉ 1. Creating a Simple Class


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Create an object
person1 = Person("Alice", 25)
person1.greet()

๐Ÿงฉ 2. Understanding __init__

The __init__ method is called automatically when a new object is created. It initializes the object’s attributes.


๐Ÿงฉ 3. Adding Methods

Methods are functions inside a class that define behaviors of objects.


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

    def area(self):
        return 3.14159 * self.radius**2

c1 = Circle(5)
print("Area:", c1.area())

๐Ÿงฉ 4. Inheritance

Classes can inherit attributes and methods from another class:


class Animal:
    def sound(self):
        print("Some sound")

class Dog(Animal):
    def sound(self):
        print("Bark")

d = Dog()
d.sound()  # Output: Bark

๐Ÿงฉ 5. Why OOP Matters

  • Organizes code using real-world objects
  • Reusable and modular code
  • Helps manage complex programs and applications
  • Foundation for frameworks like Django, Flask, and game development

๐Ÿงช Practice

  1. Create a class Car with attributes brand and model, and a method display.
  2. Create a class Student with attributes name and marks, and a method to calculate the percentage.
  3. Create a parent class Shape and a child class Rectangle with area calculation.
  4. Try creating multiple objects from the same class and call their methods.

❌ Common Mistakes

  • Not using self in methods
  • Incorrectly calling methods or accessing attributes
  • Confusing inheritance and method overriding

❓ Frequently Asked Questions (FAQ)

1. What is the purpose of the self keyword?

The self keyword is used to refer to the current instance of the class and access its attributes and methods.

2. Can I have multiple classes in the same file?

Yes, you can define multiple classes in the same Python file. Just make sure to organize them logically.

3. How does inheritance work in Python?

Inheritance allows a class (child class) to inherit attributes and methods from another class (parent class). This promotes reusability and modularity.


๐Ÿš€ What’s Next?

In the next lesson, you’ll learn about:

  • Advanced OOP concepts like encapsulation, polymorphism, and abstraction
  • How to design and implement complex class structures
  • Working with objects in real-world applications

➡ Next Lesson

Go to Lesson 20 →

Comments

Popular posts from this blog

How to Install Geany 2.1 on Windows 10/11 (2026) | Step-by-Step Guide

How to Uninstall Bluefish 2.2.19 on Windows 10/11 (2026) | Step-by-Step Guide

How to Install Visual Studio 2026 on Windows 10/11 | Step-by-Step Guide