0% found this document useful (0 votes)
27 views32 pages

Python Basics to Advanced Concepts Guide

The document provides a comprehensive overview of Python programming, covering core concepts, functions, object-oriented programming, modules, file handling, advanced topics, multithreading, and database interactions. It includes key features, data types, decorators, generators, context managers, and metaclasses, along with practical examples and explanations. Additionally, it addresses interview scenarios and best practices for Python development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views32 pages

Python Basics to Advanced Concepts Guide

The document provides a comprehensive overview of Python programming, covering core concepts, functions, object-oriented programming, modules, file handling, advanced topics, multithreading, and database interactions. It includes key features, data types, decorators, generators, context managers, and metaclasses, along with practical examples and explanations. Additionally, it addresses interview scenarios and best practices for Python development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Core Python (Basics & Intermediate)

Q1. What are Python’s key features?

• Interpreted, high-level, dynamically typed, object-oriented, portable, large standard


library, supports multiple paradigms (OOP, functional, procedural).

Q2. What are Python’s data types?

• Numeric (int, float, complex), Sequence (list, tuple, range), Text (str), Set (set,
frozenset), Mapping (dict), Boolean (bool), None (NoneType).

Q3. What is the difference between list, tuple, and set?

• List: Ordered, mutable, allows duplicates.

• Tuple: Ordered, immutable, allows duplicates.

• Set: Unordered, mutable, unique values only, no duplicates.

Q4. Explain mutable vs immutable objects.

• Mutable → can be changed (list, dict, set).

• Immutable → cannot be changed (str, tuple, frozenset).

Q5. Difference between is and == in Python?

• == checks value equality.

• is checks object identity (memory address).

Q6. What is Python’s None?

• It represents the absence of a value (like null in other languages).

Q7. What are Python’s namespaces?

• System used to ensure names are unique. Types: Built-in, Global, Enclosing, Local
(LEGB rule).

Q8. Explain Python’s memory management.

• Uses reference counting and garbage collection (via gc module).

Q9. What is Python’s GIL (Global Interpreter Lock)?

• GIL allows only one thread to execute Python bytecode at a time, limiting true
parallelism in multi-threading.

Q10. What are Python decorators?

• Functions that modify the behavior of other functions or classes (using @decorator).
2. Functions & OOP

Q11. What are Python’s function arguments types?

• Positional, Keyword, Default, Variable (*args, **kwargs).

Q12. What is the difference between *args and **kwargs?

• *args: variable number of positional arguments (tuple).

• **kwargs: variable number of keyword arguments (dict).

Q13. What is recursion in Python?

• Function calling itself until a base case is met.

Q14. What are Python’s OOP concepts?

• Class, Object, Inheritance, Encapsulation, Polymorphism, Abstraction.

Q15. Explain Python inheritance types.

• Single, Multiple, Multilevel, Hierarchical, Hybrid.

Q16. What are Python’s @staticmethod, @classmethod, and instance methods?

• Instance method: self access instance data.

• Class method: @classmethod uses cls, works with class-level data.

• Static method: @staticmethod no self or cls, works independently.

Q17. What are Python’s magic methods?

• Special methods with __ (dunder). Example: __init__, __str__, __len__, __getitem__.

Q18. What is operator overloading?

• Defining custom behavior for operators (+, -, etc.) using magic methods like __add__.

Q19. Difference between shallow copy vs deep copy?

• Shallow copy → copies references (changes in nested objects affect original).

• Deep copy → creates independent copies of nested objects ([Link]).

Q20. What is Python’s __new__ vs __init__?

• __new__ creates object; __init__ initializes it.

3. Modules & Libraries


Q21. How to import modules in Python?

• import module, from module import function, import module as alias.

Q22. What is Python’s __name__ == "__main__"?

• Ensures code runs only when the file is executed directly, not when imported.

Q23. What are Python’s virtual environments?

• Isolated environments for dependencies (venv, virtualenv, conda).

Q24. Commonly used Python libraries?

• Web: Django, Flask.

• Data: NumPy, Pandas.

• Visualization: Matplotlib, Seaborn.

• Testing: Pytest, Unittest.

Q25. What is pip?

• Python package manager for installing and managing libraries.

4. File Handling & Exceptions

Q26. How do you open and read files in Python?

with open("[Link]", "r") as f:

data = [Link]()

Q27. Explain file modes in Python.

• "r" read, "w" write, "a" append, "rb" read binary, "wb" write binary.

Q28. What is exception handling in Python?

• Using try-except-finally to handle runtime errors.

Q29. Difference between Exception and Error?

• Errors → serious issues (syntax errors).

• Exceptions → runtime issues that can be handled.

Q30. How to raise custom exceptions?

class CustomError(Exception): pass

raise CustomError("Something went wrong")


5. Advanced Python

Q31. What is Python’s iterators and generators?

• Iterator: Object with __iter__() and __next__().

• Generator: Uses yield to produce values lazily.

Q32. Difference between list comprehension and generator expression?

• List comprehension returns full list in memory.

• Generator expression returns values lazily (saves memory).

Q33. Explain Python’s with statement and context managers.

• Ensures resources are cleaned up properly. Example: file handling.

with open("[Link]") as f:

print([Link]())

Q34. How do you handle JSON in Python?

import json

data = [Link]('{"name":"Alice"}')

json_str = [Link](data)

Q35. What is monkey patching in Python?

• Modifying a class or module at runtime.

Q36. Difference between deepcopy and copy module?

• [Link]() → shallow copy.

• [Link]() → recursive copy.

Q37. Explain Python’s lambda functions.

• Anonymous functions: lambda x: x*2.

Q38. What is map(), filter(), reduce()?

• map() → applies function to iterable.

• filter() → filters items based on condition.

• reduce() → reduces iterable to single value (from functools).

Q39. Explain Python’s zip() and enumerate().


• zip() → pairs elements from multiple iterables.

• enumerate() → returns index + value.

Q40. What are Python metaclasses?

• A class of a class that defines how classes behave.

6. Multithreading & Multiprocessing

Q41. Difference between multithreading and multiprocessing in Python?

• Multithreading: Multiple threads, but limited by GIL (good for I/O).

• Multiprocessing: True parallelism using multiple processes (good for CPU-bound).

Q42. How to create threads in Python?

import threading

def func(): print("Hello")

t = [Link](target=func)

[Link]()

Q43. How to create processes in Python?

from multiprocessing import Process

def func(): print("Hello")

p = Process(target=func)

[Link]()

Q44. What is asyncio in Python?

• Library for asynchronous programming (event loop, coroutines).

Q45. Difference between concurrency and parallelism?

• Concurrency = tasks managed together (not necessarily simultaneous).

• Parallelism = tasks executed simultaneously (multi-core).

7. Database & Django/Flask Basics

Q46. How do you connect Python with a database?

• Using sqlite3, psycopg2 (Postgres), MySQLdb, or ORM (Django ORM, SQLAlchemy).


Q47. What is an ORM?

• Object Relational Mapper → maps Python objects to database tables.

Q48. How do you execute SQL in Python?

import sqlite3

conn = [Link]("db.sqlite3")

cursor = [Link]()

[Link]("SELECT * FROM users")

Q49. Difference between Flask and Django?

• Django → full-stack, batteries included.

• Flask → lightweight, microframework.

Q50. How to handle migrations in Django?

• python [Link] makemigrations → create migration.

• python [Link] migrate → apply changes.

8. Interview Scenario-Based

Q51. How would you reverse a string in Python?

s[::-1]

Q52. How to remove duplicates from a list?

list(set(mylist))

Q53. Find the second largest number in a list.

sorted(set(mylist))[-2]

Q54. How do you merge two dictionaries in Python?

d3 = {**d1, **d2}

Q55. How to sort a dictionary by value?

sorted([Link](), key=lambda x: x[1])

Q56. Explain difference between @property and normal methods.

• @property allows methods to be accessed like attributes.

Q57. How do you handle memory leaks in Python?


• Use gc module, weak references, and avoid circular references.

Q58. How do you test Python code?

• unittest, pytest, mock objects.

Q59. How do you secure Python code?

• Input validation, using virtual environments, avoiding eval(), proper exception


handling.

Q60. Explain Python’s logging module.

• Provides different log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).

Python Advanced Topics — Detailed Guide (English with Hindi summaries)

1) Decorators

English — Explanation

A decorator is a callable that takes another function (or class) and returns a new function (or
class) with extended or modified behavior. Decorators let you apply cross-cutting concerns
(logging, caching, auth checks, timing) without changing the target function’s body.

Key ideas:

• Use [Link] to preserve metadata (__name__, __doc__).

• Decorator can be simple (no args), or parameterized (accepts arguments).

• Can decorate functions, methods, and classes.

• Support stacking (multiple decorators applied in order).

Simple decorator example:

from functools import wraps

import time

def timer(fn):

@wraps(fn)

def wrapper(*args, **kwargs):


start = time.perf_counter()

result = fn(*args, **kwargs)

end = time.perf_counter()

print(f"{fn.__name__} took {end - start:.6f}s")

return result

return wrapper

@timer

def compute(n):

return sum(i*i for i in range(n))

compute(10_000)

Parameterized decorator example (decorator with arguments):

def repeat(times):

def decorator(fn):

@wraps(fn)

def wrapper(*args, **kwargs):

for _ in range(times):

value = fn(*args, **kwargs)

return value

return wrapper

return decorator

@repeat(3)

def greet(name):

print("Hello", name)

greet("Alice")
Class decorator example:

def add_repr(cls):

cls.__repr__ = lambda self: f"<{cls.__name__} {self.__dict__}>"

return cls

@add_repr

class Point:

def __init__(self, x, y):

self.x = x; self.y = y

p = Point(1,2)

print(p) # uses added __repr__

Common use-cases: logging, retrying network calls, caching results, rate-limiting, permission
checks.

Pitfalls / Best practices

• Always use [Link] to keep metadata.

• Avoid heavy logic in decorators that makes debugging hard.

• Be careful when decorating methods vs functions because of self and descriptors.

Hindi (संक्षेप)

डे कोरे टर एक ऐसा फ़ंक्शन/कॉलैबल है जो ककसी दू सरे फ़ंक्शन या क्लास को ले कर उसे बदलकर नया
फ़ंक्शन/क्लास लौटाता है। यह लॉकग़ंग, कैक ़ंग, ऑथेन्टिके न जैसी चीजें मॉड्यूलर तरीके से जोड़ने के
कलये उपयोगी है।
उदाहरण: @timer समय मापेगा; @repeat(3) फ़ंक्शन को 3 बार चलाएगा।

2) Generators and Iterators

English — Explanation

• Iterator protocol: An object is an iterator if it implements __iter__() (returns self)


and __next__() (returns next item or raises StopIteration).
• Iterable: An object that implements __iter__() returning an iterator (e.g., lists,
strings).

• Generator: A special kind of iterator created using a function that contains yield.
Generators produce items lazily, which is memory-efficient for large sequences.

Iterator class example:

class CountUpTo:

def __init__(self, limit):

[Link] = limit

[Link] = 0

def __iter__(self):

return self

def __next__(self):

if [Link] >= [Link]:

raise StopIteration

[Link] += 1

return [Link]

for i in CountUpTo(3):

print(i) # 1 2 3

Generator function example:

def count_up_to(limit):

current = 1

while current <= limit:

yield current

current += 1

for i in count_up_to(3):

print(i)

Advanced generator features


• send(value): injects value to generator (useful for coroutines).

• throw(exc): raise exception inside generator.

• close(): stop generator.

Use-cases

• Streaming large files line-by-line.

• Pipelines (chain generators).

• Implementing lazy sequences (e.g., Fibonacci).

Memory/performance

• Generators save memory because they yield one item at a time.

• They can also simplify stateful iteration logic.

Hindi (संक्षेप)

इटरे टर वह ऑब्जेक्ट है कजसमें __iter__() और __next__() होते हैं। जनरे टर yield का उपयोग करने
वाला फ़ंक्शन है जो एक-एक करके मान दे ता है —यानी मेमोरी बचती है। उपयोग: बड़ी फाइल पढ़ना,
lazy pipelines बनाना।

3) Context Managers

English — Explanation

A context manager is an object that defines setup and teardown behavior using __enter__()
and __exit__(). The with statement uses this protocol to ensure resources (files, locks, DB
transactions) are cleaned up even when exceptions occur.

Class-based context manager:

class ManagedFile:

def __init__(self, filename, mode='r'):

[Link] = filename

[Link] = mode

def __enter__(self):

[Link] = open([Link], [Link])

return [Link]
def __exit__(self, exc_type, exc, tb):

[Link]()

# Returning False re-raises exceptions (default)

return False

with ManagedFile("[Link]", "w") as f:

[Link]("hello")

[Link] decorator (generator-based):

from contextlib import contextmanager

@contextmanager

def managed_db_connection(conn):

try:

[Link]()

yield conn

finally:

[Link]()

# usage

# with managed_db_connection(conn) as c:

# [Link](...)

Use-cases

• Files, locks, DB sessions/transactions, network connections.

• Ensures deterministic release even on exceptions.

Pitfalls

• __exit__ parameters can suppress exceptions if it returns True. Only do that


intentionally.
Hindi (संक्षेप)

Context manager (with-statement) resource को सुरकित तरीके से खोलने और ब़ंद करने का


तरीका है। __enter__() setup करता है और __exit__() cleanup। [Link] से
generator-based context managers बनते हैं।

4) Metaclasses

English — Explanation

A metaclass is the “class of a class” — it controls how classes are created. By default, Python
classes are instances of type. You can subclass type to customize class creation, enforce
patterns, register subclasses, or auto-add methods/attributes.

Simple metaclass example — auto-registering subclasses:

class AutoRegister(type):

registry = {}

def __init__(cls, name, bases, namespace):

if name != 'Base':

[Link][name] = cls

super().__init__(name, bases, namespace)

class Base(metaclass=AutoRegister):

pass

class A(Base): pass

class B(Base): pass

print([Link]) # {'A': <class '__main__.A'>, 'B': <class '__main__.B'>}

Singleton via metaclass:

class SingletonMeta(type):

_instances = {}

def __call__(cls, *args, **kwargs):


if cls not in cls._instances:

cls._instances[cls] = super().__call__(*args, **kwargs)

return cls._instances[cls]

class Logger(metaclass=SingletonMeta):

pass

a = Logger(); b = Logger()

assert a is b

Use-cases

• Frameworks (Django ORM uses metaclasses for Model creation).

• Auto-registration, validation at class creation time, injecting methods/properties.

Caution

• Metaclasses increase complexity and can make code hard to follow; use only when
needed.

Hindi (संक्षेप)

Metaclass क्लास का क्लास होती है — यानी यह कनर्ााररत करती है कक एक क्लास कैसे बनेगी। यह
type को subclass कर के बनाया जाता है। उपयोग: क्लास auto-register करना, Singleton, फ्रेमवका-
लेवल कनयम लागू करना। पर सावर्ानी से प्रयोग करें क्ो़ंकक यह कोड जकटल कर दे ता है।

5) Descriptors

English — Explanation

A descriptor is an object attribute with binding behavior, defined by implementing any of:
__get__(self, obj, type), __set__(self, obj, value), __delete__(self, obj). The property builtin is
implemented using descriptors. Descriptors are used to manage attributes with reusable
logic (type checking, lazy evaluation, computed properties).

Descriptor example — typed attribute:

class Typed:

def __init__(self, name, expected_type):


[Link] = name

self.expected_type = expected_type

def __get__(self, instance, owner):

if instance is None:

return self

return instance.__dict__.get([Link])

def __set__(self, instance, value):

if not isinstance(value, self.expected_type):

raise TypeError(f"{[Link]} must be {self.expected_type}")

instance.__dict__[[Link]] = value

def __delete__(self, instance):

del instance.__dict__[[Link]]

class Person:

name = Typed('name', str)

age = Typed('age', int)

p = Person()

[Link] = "Alice"

[Link] = 30

# [Link] = "thirty" # raises TypeError

Use-cases

• Enforcing types, lazy attributes, computed caching, attribute-level validation across


many classes.

Note

• Descriptors are used heavily in frameworks (Django ORM field descriptors, property,
cached_property).
Hindi (संक्षेप)

Descriptor एक ऑब्जेक्ट है जो attribute access को कनय़ंकित करता है (__get__, __set__,


__delete__)। इससे आप attribute-level validation, type checking और lazy evaluation जैसा
common logic centralize कर सकते हैं। property भी descriptor पर आर्ाररत है।

6) DUNDER (Magic) Methods

English — Explanation

Dunder (double-underscore) or magic methods are special methods Python looks for to
enable built-in behavior. Examples:

• Construction/representation: __init__, __new__, __repr__, __str__

• Container protocol: __len__, __getitem__, __setitem__, __contains__

• Iteration: __iter__, __next__

• Arithmetic: __add__, __sub__, __mul__, __radd__

• Comparison & hashing: __eq__, __lt__, __hash__

• Callable objects: __call__

• Context manager: __enter__, __exit__

Example — simple Vector supporting addition & repr

class Vector:

def __init__(self, x, y):

self.x, self.y = x, y

def __add__(self, other):

return Vector(self.x + other.x, self.y + other.y)

def __repr__(self):

return f"Vector({self.x}, {self.y})"

def __eq__(self, other):

return isinstance(other, Vector) and self.x == other.x and self.y == other.y

v1 = Vector(1,2)

v2 = Vector(3,4)
print(v1 + v2) # Vector(4,6)

Best practices

• Implement __repr__ to be unambiguous and __str__ for readability.

• If you implement __eq__, also implement __hash__ (or set __hash__ = None) to
keep hashable semantics consistent.

• Keep magic methods simple and efficient.

Hindi (संक्षेप)

Dunder methods (जैसे __init__, __add__, __repr__) Python की कबल्ट-इन सुकवर्ाओ़ं को सिम
करते हैं — operator overloading, iteration, representation आकद। जब आप इन्हें पररभाकित करते
हैं तो objects Python से “प्राकृकतक” तरीके से behave करते हैं।

7) Asynchronous Programming (asyncio)

English — Explanation

asyncio is Python’s standard library for writing concurrent code using the async/await syntax.
It is ideal for I/O-bound concurrency (networking, file I/O) — not for CPU-bound parallelism
(use multiprocessing for that).

Key concepts:

• Coroutine: function defined with async def.

• Event loop: schedules and runs coroutines.

• Task: wrapper to schedule coroutine concurrently.

• await: yields control until awaited coroutine completes.

• async context managers / iterators: async with, async for.

Basic example (concurrent I/O simulation):

import asyncio

import random

import time

async def fetch(id):


delay = [Link](0.1, 1.0)

await [Link](delay) # simulate I/O

print(f"Fetched {id} after {delay:.2f}s")

return id

async def main():

tasks = [asyncio.create_task(fetch(i)) for i in range(5)]

results = await [Link](*tasks)

print("Results:", results)

[Link](main())

aiohttp for real HTTP I/O (external lib):

# pip install aiohttp

import aiohttp, asyncio

async def fetch(session, url):

async with [Link](url) as resp:

return await [Link]()

async def main():

async with [Link]() as session:

htmls = await [Link](*(fetch(session, "[Link] for _ in range(5)))

[Link](main())

When to use asyncio

• High-concurrency network servers/clients.

• Web scraping with concurrency.

• Real-time apps.
Pitfalls

• Don't block the event loop (avoid CPU-heavy loops); use run_in_executor to run
blocking calls on thread pool.

• Proper cancellation handling (tasks should handle [Link]).

• Mixing blocking code and async code can cause deadlocks.

Hindi (संक्षेप)

asyncio asynchronous code कलखने के कलये है — खासकर I/O-bound tasks (नेटवका, फाइल)।
async def coroutine बनाते हैं, await से control yield होता है। [Link]() से event loop चलता
है। CPU-bound काम के कलये multiprocessing बेहतर है।

8) Concurrency and Parallelism

English — Explanation

• Concurrency = many tasks are in-progress (conceptual simultaneity).

• Parallelism = actual simultaneous execution on multiple CPU cores.

In Python

• Threading: [Link] — good for I/O-bound tasks; limited by GIL for CPU-
bound Python bytecode.

• Multiprocessing: [Link] or [Link]


— true parallelism for CPU-bound tasks.

• Asyncio: concurrency for I/O using single-threaded event loop (cooperative


multitasking).

• C extensions / native code: bypass GIL for CPU work (e.g., numpy).

Examples

ThreadPool for I/O:

from [Link] import ThreadPoolExecutor

import requests # blocking I/O

def fetch(url):

return [Link](url).status_code
urls = ["[Link] * 10

with ThreadPoolExecutor(max_workers=5) as ex:

results = list([Link](fetch, urls))

ProcessPool for CPU-bound:

from [Link] import ProcessPoolExecutor

import math

def heavy(n):

return sum([Link](i) for i in range(n))

with ProcessPoolExecutor(max_workers=4) as ex:

results = list([Link](heavy, [10_000_00]*4))

Choosing approach

• I/O-bound → threads or asyncio.

• CPU-bound → processes or native extensions (NumPy, Cython).

• Mixed → combine: use asyncio for I/O and offload CPU tasks with run_in_executor or
process pool.

Pitfalls

• Thread-safety for shared mutable state.

• Inter-process communication overhead.

• Debugging concurrent issues (race conditions, deadlocks).

Hindi (संक्षेप)

Concurrency अनेक कायों को एक साथ manage करना है ; Parallelism वास्तव में एक साथ multiple
cores पर चलना है। I/O-bound → threading/asyncio; CPU-bound → multiprocessing या native
C-extensions। चुनते समय GIL का ध्यान रखें।

9) Advanced OOP
English — Explanation

Advanced OOP in Python covers design patterns, composition vs inheritance, multiple


inheritance and MRO, abstract base classes (ABC), mixins, encapsulation, properties, and
careful API design.

Abstract base class example

from abc import ABC, abstractmethod

class Shape(ABC):

@abstractmethod

def area(self):

pass

class Circle(Shape):

def __init__(self, r): self.r = r

def area(self): return 3.1416 * self.r * self.r

Mixin pattern (composition)

class JsonSerializableMixin:

def to_json(self):

import json

return [Link](self.__dict__)

class User(JsonSerializableMixin):

def __init__(self, name): [Link] = name

u = User("Bob")

print(u.to_json())

Multiple inheritance & MRO

class A: pass

class B(A): pass


class C(A): pass

class D(B, C): pass

# Method resolution order: D, B, C, A

print(D.__mro__)

Best practices

• Prefer composition over inheritance when appropriate.

• Keep classes small and focused (single responsibility).

• Use ABCs for enforcing interface contracts.

• Use mixins only for orthogonal functionality (e.g., serialization).

Hindi (संक्षेप)

Advanced OOP में abstract classes, mixins, multiple inheritance और composition बनाम
inheritance ाकमल हैं। Mixins को छोटे , orthogonal features के कलए प्रयोग करें ; interface enforce
करने के कलए abc का उपयोग करें ।Inheritance misuse से जकटलता बढ़ सकती है — composition
अक्सर बेहतर है।

10) Packaging and Distribution

English — Explanation

Packaging prepares your code for distribution (PyPI, internal repos). Modern Python
packaging uses [Link]. Typical steps:

1. Project structure and metadata ([Link], [Link], or [Link]).

2. Build sdist and wheel (python -m build).

3. Upload to PyPI using twine upload dist/*.

Minimal example ([Link]):

[build-system]

requires = ["setuptools>=61", "wheel"]

build-backend = "setuptools.build_meta"

[project]
name = "mypackage"

version = "0.1.0"

description = "Example package"

authors = [{name="You", email="you@[Link]"}]

readme = "[Link]"

requires-python = ">=3.8"

dependencies = ["requests>=2.0"]

Build and upload

pip install build twine

python -m build # creates dist/[Link] and .whl

twine upload dist/* # requires PyPI account (or [Link])

Best practices

• Use semantic versioning.

• Include README, LICENSE, and long_description in metadata.

• Test install from [Link] before real PyPI.

• Pin dependencies minimally and avoid overly broad requirements.

Hindi (संक्षेप)

पैकेकज़ंग में [Link] में metadata डालकर python -m build से wheel और sdist बनाते हैं ,
कफर twine upload से PyPI पर भेजते हैं। [Link] पर पहले परीिण करें ; README और
LICENSE ज़रूरी हैं।

11) Performance Optimization

English — Explanation

Performance optimization covers algorithmic improvements, memory usage, I/O patterns,


caching, and using the right tools (profilers). Focus on measuring first, then optimize
hotspots.

Steps

1. Measure with timeit, cProfile, pyinstrument, line_profiler.


2. Algorithmic improvements: use better algorithmic complexity (e.g., dict lookups O(1)
vs list O(n)).

3. Use efficient data structures: deque for pops from left, set for membership, array for
numeric arrays, numpy for vectorized math.

4. Avoid unnecessary allocations: reuse lists, use generator expressions when possible.

5. Minimize Python-level loops: prefer built-in functions and comprehensions.

6. Caching: functools.lru_cache, memoization.

7. I/O optimizations: use buffered I/O, asyncio for many concurrent connections,
streaming for large data.

8. Parallelism: use multiprocessing for CPU-bound tasks.

9. Native extensions: use C libraries (NumPy), Cython, Numba for critical loops.

Examples

• lru_cache:

from functools import lru_cache

@lru_cache(maxsize=128)

def fib(n):

if n < 2: return n

return fib(n-1) + fib(n-2)

• List comprehension vs loop (speed & memory):

# faster and often clearer

squares = [i*i for i in range(10_000)]

# generator expression uses less memory:

squares_gen = (i*i for i in range(10_000))

• Profiling with cProfile:

python -m cProfile -o [Link] [Link]

python -m pstats [Link] # inspect hotspot

Micro-optimizations
• Bind methods/attributes to local variable in tight loops to avoid attribute lookup
overhead.

# slower

for item in items:

process(item)

# faster (local lookup)

proc = process

for item in items:

proc(item)

• Use ''.join([...]) for strings rather than repeated +=.

Memory optimizations

• Use generators for streaming data.

• memoryview for slicing large bytes without copy.

• Use __slots__ in classes with many instances to reduce memory footprint.

When to stop optimizing

• Avoid premature optimization. Optimize only the parts that matter (hot paths from
profiling).

Hindi (संक्षेप)

Performance के कलये पहले मापना ज़रूरी है (cProfile, timeit)। उसके बाद algorithm सुर्ारें (O(n)
→ O(1) जहााँ स़ंभव), सही data structure चुनें (set, deque), caching (lru_cache) और vectorized
libraries (NumPy) का उपयोग करें । I/O-bound काम के कलये async/threading; CPU-bound के
कलये multiprocessing या native extensions।

Final Tips & References

• Measure before optimizing. Use profilers and benchmark real scenarios.

• Write readable code first — optimize later.

• Use tests to ensure changes don’t break behavior.


• Prefer composition and small functions/classes for maintainability.

• Use modern packaging ([Link]) and automation (CI) for reliability.

Python Advanced Cheat-Sheets

Decorators Cheat-Sheet

What: Functions that wrap other functions to add functionality.


Syntax: @decorator above a function.

def decorator(func):

def wrapper(*args, **kwargs):

print("Before function")

result = func(*args, **kwargs)

print("After function")

return result

return wrapper

@decorator

def greet(name):

print(f"Hello {name}")

greet("Amit")

✔ Use Cases: Logging, Authentication, Caching, Rate-limiting.

Generators & Iterators Cheat-Sheet

Iterator: Object with __iter__() & __next__() methods.


Generator: Function using yield, auto-creates iterator.

def gen():
yield 1

yield 2

yield 3

for i in gen():

print(i)

✔ Use Cases: Large data streams, memory-efficient loops.

Context Managers Cheat-Sheet

What: Manage resources with with statement.


Example:

with open("[Link]", "w") as f:

[Link]("Hello")

Custom Context Manager:

class MyContext:

def __enter__(self):

print("Enter")

def __exit__(self, exc_type, exc_value, traceback):

print("Exit")

with MyContext():

print("Inside")

✔ Use Cases: File I/O, DB connections, Locks.

Metaclasses Cheat-Sheet

What: Class of a class. Controls how classes are created.

class Meta(type):

def __new__(cls, name, bases, dct):


print(f"Creating class {name}")

return super().__new__(cls, name, bases, dct)

class MyClass(metaclass=Meta):

pass

✔ Use Cases: ORM frameworks (Django Models), Singleton pattern.

Descriptors Cheat-Sheet

What: Objects that control attribute access via


__get__, __set__, __delete__.

class Descriptor:

def __get__(self, obj, objtype=None):

return "Got value"

def __set__(self, obj, value):

print(f"Set value {value}")

class MyClass:

x = Descriptor()

m = MyClass()

m.x = 10

print(m.x)

✔ Use Cases: Property management, Validation, ORM fields.

Dunder Methods Cheat-Sheet

Special methods (start + end with __).

class Vector:

def __init__(self, x, y): self.x, self.y = x, y


def __add__(self, other): return Vector(self.x+other.x, self.y+other.y)

def __str__(self): return f"({self.x}, {self.y})"

print(Vector(1,2) + Vector(3,4)) # (4,6)

✔ Common: __init__, __str__, __len__, __getitem__, __call__.

Asynchronous Programming (asyncio) Cheat-Sheet

What: Concurrency with async & await.

import asyncio

async def task1():

await [Link](1)

print("Task 1 done")

async def main():

await [Link](task1(), task1())

[Link](main())

✔ Use Cases: I/O-bound tasks (web scraping, APIs, DB calls).

Concurrency & Parallelism Cheat-Sheet

• Concurrency: Multiple tasks (time-sliced).

• Parallelism: Multiple tasks truly at once (multi-core).

import threading, multiprocessing

def work(): print("Working")

# Thread
t = [Link](target=work)

[Link]()

# Process

p = [Link](target=work)

[Link]()

✔ Use Cases: Web servers (concurrency), Data crunching (parallelism).

Advanced OOP Cheat-Sheet

• Inheritance

• Polymorphism

• Abstraction

• Encapsulation

from abc import ABC, abstractmethod

class Animal(ABC):

@abstractmethod

def speak(self): pass

class Dog(Animal):

def speak(self): return "Woof"

print(Dog().speak())

✔ Use Cases: Framework design, APIs, Large-scale systems.

Packaging & Distribution Cheat-Sheet

Steps to package Python project:

project/
├── mylib/

│ └── __init__.py

├── [Link]

[Link]:

from setuptools import setup, find_packages

setup(

name="mylib",

version="0.1",

packages=find_packages(),

Build & Install:

python [Link] sdist bdist_wheel

pip install dist/[Link]

✔ Use Cases: Share libraries on PyPI.

Performance Optimization Cheat-Sheet

Techniques:

• Use list comprehension instead of loops.

• Use set/dict for faster lookups.

• Use multiprocessing for CPU-bound tasks.

• Use asyncio for I/O tasks.

• Profile code with cProfile.

import cProfile

def calc():

return sum([i*i for i in range(10**6)])

[Link]("calc()")
✔ Use Cases: Speeding up APIs, Big data processing.

You might also like