1. Classes & Objects
1.1 Basic Class Definition
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): return f"Hello, I'm {self.name}"
Usage:
p = Person("Vivek", 28) print(p.greet())
š¹ Important: self
selfrepresents the instance
- Similar to
thisin C#
- Must be explicitly written in method definition
2. Instance vs Class Variables
2.1 Instance Variables
class Person: def __init__(self, name): self.name = name
Each object gets its own copy.
2.2 Class Variables
class Person: species = "Human" # class variable
Shared across all instances.
3. Dunder (Magic) Methods
Special methods surrounded by
__.3.1 str vs repr
class Person: def __init__(self, name): self.name = name def __str__(self): return f"Person({self.name})"
__str__ā readable
__repr__ā developer-focused representation
3.2 eq (Equality)
def __eq__(self, other): return self.name == other.name
3.3 len
def __len__(self): return len(self.items)
4. Inheritance
class Animal: def speak(self): return "Sound" class Dog(Animal): def speak(self): return "Bark"
4.1 super()
class Dog(Animal): def __init__(self, name): super().__init__() self.name = name
5. Multiple Inheritance
Python supports it.
class A: pass class B: pass class C(A, B): pass
Method Resolution Order (MRO):
C.__mro__
Interview note:
Python uses C3 linearization for method resolution.
6. Dataclasses (Very Important)
Cleaner way to create data objects.
from dataclasses import dataclass @dataclass class Person: name: str age: int
Auto-generates:
- init
- repr
- eq
Very useful in APIs & AI pipelines.
7. Exception Handling
7.1 Try / Except
try: x = 1 / 0 except ZeroDivisionError: print("Cannot divide by zero")
7.2 Multiple Exceptions
except (ValueError, TypeError): pass
7.3 Finally
finally: print("Always executes")
7.4 Custom Exception
class MyError(Exception): pass
8. Modules & Packages
8.1 Importing
import math from math import sqrt
8.2 name == "main"
if __name__ == "__main__": main()
Prevents execution when imported.
9. Virtual Environments
Create:
python -m venv venv
Activate:
Mac/Linux:
source venv/bin/activate
Windows:
venv\Scripts\activate
Install packages:
pip install requests
10. File Handling
10.1 Reading File
with open("file.txt", "r") as f: content = f.read()
10.2 Writing File
with open("file.txt", "w") as f: f.write("Hello")
11. Context Managers
Used with
with.Built-in example:
with open("file.txt") as f: data = f.read()
11.1 Custom Context Manager
class MyContext: def __enter__(self): print("Enter") return self def __exit__(self, exc_type, exc_value, traceback): print("Exit")
12. Decorators (Beginner Level)
Decorators wrap functions.
def logger(func): def wrapper(): print("Before") func() print("After") return wrapper @logger def say_hello(): print("Hello")
13. Closures
Function inside function remembering outer variable.
def outer(): x = 10 def inner(): return x return inner
14. Memory Model & GIL (Interview Critical)
14.1 Reference Counting
Python tracks number of references to objects.
When count reaches zero ā garbage collected.
14.2 Garbage Collection
Handles cyclic references.
14.3 Global Interpreter Lock (GIL)
- Only one thread executes Python bytecode at a time.
- Limits CPU-bound multithreading.
- Does NOT affect I/O-bound tasks.
Interview-ready answer:
Python threads are good for I/O-bound tasks but not ideal for CPU-bound tasks due to GIL.
š„ Quick C# vs Python Mental Comparison
Concept | C# | Python |
Access modifiers | public/private | Convention-based (_var) |
Interfaces | Explicit | Duck typing |
Overloading | Yes | Not in same way |
Threading | True parallel | GIL limits CPU parallelism |
ć
¤ | ć
¤ | ć
¤ |
Ā
