LEARN COMPLETE PYTHON IN 24 HOURS

🟦 Table of Contents – Python OOP

🔹 1. Introduction to Object-Oriented Programming (OOP)

  • 1.1 What is OOP and Why Learn It?

  • 1.2 Real-World Examples – Class vs Object

  • 1.3 The 4 Pillars of OOP in Python

  • 1.4 Procedural vs Object-Oriented Programming

🔹 2. Classes and Objects – Basic Building Blocks

  • 2.1 Creating Classes

  • 2.2 Creating Objects

  • 2.3 The init Method and self Parameter

  • 2.4 Instance Variables vs Class Variables

  • 2.5 str and repr

🔹 3. Encapsulation – Data Hiding and Protection

  • 3.1 Public, Protected and Private Members

  • 3.2 Name Mangling

  • 3.3 @property, @setter, @deleter

🔹 4. Inheritance – Reusing Code

  • 4.1 Single Inheritance

  • 4.2 Using super()

  • 4.3 Method Overriding and Polymorphism

  • 4.4 Multiple Inheritance and MRO

🔹 5. Polymorphism – One Name, Different Behavior

  • 5.1 Method Overriding

  • 5.2 Operator Overloading

  • 5.3 Duck Typing

  • 5.4 Abstract Base Classes

🔹 6. Class Methods, Static Methods and Decorators

  • 6.1 @classmethod

  • 6.2 @staticmethod

  • 6.3 Alternative Constructors

  • 6.4 @property

🔹 7. Advanced OOP Concepts

  • 7.1 Composition vs Inheritance

  • 7.2 Data Classes (@dataclass)

  • 7.3 Magic Methods / Dunder Methods

  • 7.4 Metaclasses

🔹 8. Real-World OOP Projects & Best Practices

  • 8.1 Bank Account System

  • 8.2 Library Management System

  • 8.3 Employee Payroll System

  • 8.4 OOP Best Practices

🔹 9. Common Mistakes & Interview Preparation

  • 9.1 Common OOP Mistakes

  • 9.2 Python OOP Interview Questions

  • 9.3 Debugging OOP Code

🔹 10. Next Steps After Mastering OOP

  • 10.1 Design Patterns

  • 10.2 Decorators and Context Managers

  • 10.3 OOP in FastAPI / Django

  • 10.4 Resources

2. Classes and Objects – Basic Building Blocks

Classes and objects are the foundation of Object-Oriented Programming in Python. A class is like a blueprint, and an object is the actual thing built from that blueprint.

2.1 Creating Classes (class keyword)

A class is defined using the class keyword followed by the class name (use CamelCase by convention).

Basic syntax:

Python

class ClassName: # class body (attributes and methods go here) pass

Simple example – Empty class

Python

class Car: pass # empty class (placeholder)

With a comment (good practice)

Python

class Student: """This class represents a student in school.""" pass

Naming convention (PEP 8):

  • Use CamelCase (CapitalizeEachWord)

  • Meaningful names: BankAccount, LibraryBook, EmployeeProfile

2.2 Creating Objects (Instances)

An object (or instance) is created by calling the class name like a function.

Syntax:

Python

object_name = ClassName(arguments_if_any)

Example

Python

class Car: pass # Creating objects car1 = Car() # object 1 car2 = Car() # object 2 print(car1) # <__main__.Car object at 0x...> print(car2) # different memory address print(car1 == car2) # False – different objects

Analogy: Car is the blueprint (class). car1 and car2 are two actual cars built from the same blueprint.

2.3 The init Method and self Parameter

init is a special method (constructor) that runs automatically when an object is created. It is used to initialize (set up) the object’s attributes.

self is a reference to the current object (instance). Python automatically passes it — you don’t need to send it.

Example – Initialize attributes

Python

class Car: def init(self, brand, color, year): self.brand = brand # instance attribute self.color = color self.year = year def drive(self): return f"{self.color} {self.brand} ({self.year}) is driving!" # Create objects with data car1 = Car("Maruti", "Red", 2025) car2 = Car("Honda", "Blue", 2024) print(car1.drive()) # Red Maruti (2025) is driving! print(car2.drive()) # Blue Honda (2024) is driving!

Key points about self:

  • self is just a convention — you can name it anything (but never do!)

  • First parameter of every instance method must be self

  • Use self.attribute to access or set instance-specific data

2.4 Instance Variables vs Class Variables

TypeDefined where?Shared by all objects?Example in codeUse caseInstance VariableInside init (with self)No – unique per objectself.brand = "Maruti"Data specific to each object (color, year)Class VariableDirectly inside class bodyYes – shared by allwheels = 4 (outside any method)Common properties for all instances

Code Example

Python

class Car: wheels = 4 # class variable (shared) def init(self, brand, color): self.brand = brand # instance variable (unique) self.color = color car1 = Car("Maruti", "Red") car2 = Car("Honda", "Blue") print(car1.wheels) # 4 print(car2.wheels) # 4 print(Car.wheels) # 4 (access via class) Car.wheels = 6 # changes for all objects print(car1.wheels) # 6 print(car2.wheels) # 6 car1.wheels = 8 # creates instance variable for car1 only print(car1.wheels) # 8 (car1 has its own) print(car2.wheels) # 6 (car2 still uses class variable)

Rule: Use class variables for constants/shared data. Use instance variables for data unique to each object.

2.5 str and repr – Beautifully Printing Objects

By default, printing an object shows memory address — not useful.

Use special methods to control how objects are printed:

  • str → human-readable string (used by print())

  • repr → developer-friendly, unambiguous (used in REPL/debugger)

Example

Python

class Car: def init(self, brand, color, year): self.brand = brand self.color = color self.year = year def str(self): return f"{self.color} {self.brand} ({self.year})" def repr(self): return f"Car(brand='{self.brand}', color='{self.color}', year={self.year})" car = Car("Maruti", "Red", 2025) print(car) # Red Maruti (2025) ← str print(repr(car)) # Car(brand='Maruti', color='Red', year=2025) ← repr

Best practice:

  • Always implement both

  • str for users/readability

  • repr for debugging (should ideally allow recreating the object)

Mini Summary Project – Student Class

Python

class Student: school = "XYZ International School" # class variable def init(self, name, roll_no, marks): self.name = name self.roll_no = roll_no self.marks = marks def str(self): return f"{self.name} (Roll: {self.roll_no}) - Marks: {self.marks}%" def get_grade(self): if self.marks >= 90: return "A+" elif self.marks >= 80: return "A" else: return "B or below" s1 = Student("Anshuman", 101, 92) s2 = Student("Rahul", 102, 85) print(s1) # Anshuman (Roll: 101) - Marks: 92% print(s1.school) # XYZ International School print(s1.get_grade()) # A+

This completes the full Classes and Objects – Basic Building Blocks section — the heart of OOP in Python!

📚 Amazon Book Library

All my books are FREE on Amazon Kindle Unlimited🌍 Exclusive Country-Wise Amazon Book Library – Only Here!

On GlobalCodeMaster.com you’ll find complete, ready-to-use lists of my books with direct Amazon links for every country.
Belong to India, Australia, USA, UK, Canada or any other country? Just click your country’s link and enjoy:
Any eBook FREE on Kindle Unlimited ✅ Or buy at incredibly low prices
400+ fresh books written in 2025-2026 with today’s latest AI, Python, Machine Learning & tech trends – nowhere else will you find this complete country-wise collection on one platform!
Choose your country below and start reading instantly 🚀
BOOK LIBRARY USA 2026 LINK
BOOK LIBRARY INDIA 2026 LINK
BOOK LIBRARY AUSTRALIA 2026 LINK
BOOK LIBRARY CANADA 2026 LINK
BOOK LIBRARY UNITED KINGDOM 2026 LINK
BOOK LIBRARY GERMANY 2026 LINK
BOOK LIBRARY FRANCE 2026 LINK
BOOK LIBRARY ITALY 2026 LINK
BOOK LIBRARY SPAIN 2026 LINK
BOOK LIBRARY NETHERLANDS 2026 LINK
BOOK LIBRARY BRAZIL 2026 LINK
BOOK LIBRARY MEXICO 2026 LINK
BOOK LIBRARY JAPAN 2026 LINK
BOOK LIBRARY POLAND 2026 LINK
BOOK LIBRARY IRELAND 2026 LINK
BOOK LIBRARY SWEDEN 2026 LINK
BOOK LIBRARY BELGIUM 2026 LINK