Python
Python Basics
- Introduction to Python and Its History
- Python Syntax and Indentation
- Python Variables and Data Types
- Dynamic and Strong Typing
- Comments and Docstrings
- Taking User Input (input())
- Printing Output (print())
- Python Operators (Arithmetic, Logical, Comparison)
- Type Conversion and Casting
- Escape Characters and Raw Strings
Data Structures in Python
- Lists
- Dictionaries
- Dictionary Comprehensions
- Strings and String Manipulation
- Tuples
- Python Sets: Unordered Collections
- List Comprehensions and Generator Expressions
- Set Comprehensions
- String Formatting
- Indexing and Slicing
Control Flow and Loops
- Conditional Statements: if, elif, and else
- Loops and Iteration
- While Loops
- Nested Loops
- Loop Control Statements
- Iterators and Iterables
- List, Dictionary, and Set Iterations
Functions and Scope
- Defining and Calling Functions (`def`)
- Function Arguments (`*args`, `**kwargs`)
- Default Arguments and Keyword Arguments
- Lambda Functions
- Global and Local Scope
- Function Return Values
- Recursion in Python
Object-Oriented Programming (OOP)
- Object-Oriented Programming
- Classes and Objects
- the `__init__()` Constructor
- Instance Variables and Methods
- Class Variables and `@classmethod`
- Encapsulation and Data Hiding
- Inheritance and Subclasses
- Method Overriding and super()
- Polymorphism
- Magic Methods and Operator Overloading
- Static Methods
- Abstract Classes and Interfaces
Python Programs
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions
- Program: Calculate Pi in Python
- String Formatting in Python
🧱 Object-Oriented Programming (OOP) in Python
Object-Oriented Programming (OOP) is a programming paradigm that allows you to structure your code in a way that models real-world entities. Rather than writing code as a sequence of procedures, OOP focuses on objects—bundles of data and functionality.
Python is a versatile language that supports multiple programming paradigms, and OOP is one of its most powerful and widely used approaches.
In this article, we’ll explore what OOP is, why it matters, and how to implement it in Python using real-world examples.
🧠 Why is OOP Important?
Object-Oriented Programming brings several key benefits to coding:
- Modularity: Code is divided into self-contained objects.
- Reusability: You can reuse and extend classes without rewriting them.
- Scalability: Easier to manage large applications.
- Real-World Modeling: Objects map closely to real-world concepts.
- Maintainability: Cleaner structure means easier debugging and updates.
OOP is used in countless Python frameworks, like Django and Flask, and is essential for building apps, games, and systems.
📌 Prerequisites
Before learning OOP, you should be familiar with:
- Basic Python syntax
- Functions and variables
- Data types (strings, lists, dictionaries)
- Conditional statements and loops
🔑 Must-Know OOP Concepts in Python
1. Class
A class is like a blueprint for creating objects. It defines properties (attributes) and behaviors (methods).
class Car: def __init__(self, brand, color): self.brand = brand self.color = color
2. Object
An object is an instance of a class. You can create multiple objects from one class.
car1 = Car("Toyota", "Red")car2 = Car("BMW", "Black")
🛠️ Building Blocks of OOP in Python
✅ 1. Creating a Class and Object
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed
def bark(self): return f"{self.name} says woof!"
my_dog = Dog("Buddy", "Golden Retriever")print(my_dog.bark())
Output:
Buddy says woof!
✅ 2. The __init__()
Method
- Acts as a constructor
- Automatically called when a new object is created
- Initializes the object with values
def __init__(self, name): self.name = name
✅ 3. The self
Keyword
- Refers to the current instance of the class
- Used to access variables and methods
class Student: def __init__(self, name): self.name = name
🧬 Advanced OOP Concepts
🔁 4. Inheritance
Inheritance allows a class (child) to inherit attributes and methods from another class (parent).
class Animal: def speak(self): return "Makes a sound"
class Cat(Animal): def speak(self): return "Meow"
cat = Cat()print(cat.speak()) # Output: Meow
Inheritance promotes code reusability and logical hierarchy.
🛡️ 5. Encapsulation
Encapsulation means hiding internal details and exposing only necessary parts.
Use a single underscore _
or double __
to make variables private or protected.
class BankAccount: def __init__(self, balance): self.__balance = balance
def get_balance(self): return self.__balance
account = BankAccount(1000)print(account.get_balance()) # Output: 1000
🔄 6. Polymorphism
Polymorphism allows different classes to implement methods with the same name, behaving differently.
class Bird: def sound(self): return "Chirp"
class Duck(Bird): def sound(self): return "Quack"
def make_sound(animal): print(animal.sound())
duck = Duck()bird = Bird()
make_sound(duck) # Output: Quackmake_sound(bird) # Output: Chirp
🧩 Real-World Example: Employee Management System
class Employee: def __init__(self, name, role): self.name = name self.role = role
def get_info(self): return f"{self.name} works as a {self.role}"
class Manager(Employee): def get_info(self): return f"{self.name} is a manager"
emp1 = Employee("Alice", "Developer")mgr1 = Manager("Bob", "Manager")
print(emp1.get_info()) # Output: Alice works as a Developerprint(mgr1.get_info()) # Output: Bob is a manager
⚠️ Common Mistakes to Avoid
- Forgetting
self
in method definitions - Using global variables instead of encapsulated data
- Overcomplicating with inheritance when not needed
- Not initializing attributes in
__init__()
🛡️ Best Practices for OOP in Python
- Name classes with
CamelCase
and methods/variables withsnake_case
- Keep methods focused on one responsibility
- Use inheritance wisely
- Document your classes and methods with docstrings
- Use
super()
to call parent class methods in inheritance
📘 Summary
Object-Oriented Programming is a core concept in Python that helps you write organized, reusable, and scalable code. By learning about classes, objects, inheritance, and encapsulation, you’re preparing yourself to build real-world applications efficiently.
Here’s what you should take away from this guide:
- Classes define the blueprint, and objects are actual instances.
- OOP promotes better structure and maintenance.
- Concepts like inheritance and polymorphism make your code smarter and more reusable.
Next > Classes and Objects