OOP, decorators, generators, GIL, data structures and more — everything you need for your Python interview.
Python is a high-level, interpreted, general-purpose programming language. Known for its simple, readable syntax. Supports multiple programming paradigms: procedural, object-oriented, and functional. Used in web dev, data science, AI/ML, automation.
Lists are mutable (can be changed) and use square brackets []. Tuples are immutable (cannot be changed) and use parentheses (). Tuples are faster and used for fixed data. Lists are for collections that change.
A decorator is a function that takes another function and extends its behavior without modifying it. Uses @syntax. Common examples: @staticmethod, @classmethod, @property, @login_required in Django.
A generator is a function that yields values one at a time using the yield keyword instead of return. It is memory efficient for large data sets because it produces items only when needed, not all at once.
Shallow copy creates a new object but references the same nested objects (copy.copy()). Deep copy creates a completely independent clone including nested objects (copy.deepcopy()). Modifying nested objects in shallow copy affects the original.
*args allows passing a variable number of positional arguments to a function as a tuple. **kwargs allows passing a variable number of keyword arguments as a dictionary. Useful for flexible function signatures.
List comprehensions provide a concise way to create lists. Syntax: [expression for item in iterable if condition]. Example: [x*2 for x in range(10) if x%2==0]. Faster and more Pythonic than for loops.
The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecode simultaneously. It limits true parallelism in CPU-bound tasks. Use multiprocessing for CPU-bound, threading for I/O-bound tasks.
== checks value equality (do they have the same value?). is checks identity equality (are they the same object in memory?). Example: a = [1,2]; b = [1,2]; a==b is True but a is b is False.
Built-in types: int, float, complex (numbers), str (string), bool, list, tuple, dict, set, frozenset, None. Everything in Python is an object.
Object-Oriented Programming in Python uses classes and objects. Four pillars: Encapsulation (bundling data and methods), Inheritance (child class inherits from parent), Polymorphism (same interface, different behavior), Abstraction (hiding implementation).
@staticmethod does not receive any implicit first argument — it's just a regular function inside a class. @classmethod receives the class (cls) as first argument and can access/modify class state.
Lambda functions are anonymous, single-expression functions. Syntax: lambda arguments: expression. Example: square = lambda x: x**2. Used in map(), filter(), sorted() as short, throwaway functions.
Use try/except/finally blocks. try contains code that might fail. except catches specific exceptions. else runs if no exception occurred. finally always runs. Raise custom exceptions by inheriting from Exception class.
A dictionary is an unordered (ordered in Python 3.7+) collection of key-value pairs. Keys must be unique and immutable. Operations: dict[key] to access, dict.get(key) for safe access, dict.items() to iterate.
__init__ is the constructor method in Python classes. It is called automatically when a new instance is created. Used to initialize the object's attributes. self refers to the current instance.
Mutable objects can be changed after creation: list, dict, set. Immutable objects cannot be changed: int, float, str, tuple, frozenset. Immutable objects are hashable and can be used as dictionary keys.
map(function, iterable) applies a function to every item in an iterable and returns a map object. filter(function, iterable) filters items where function returns True. Both are lazy — use list() to evaluate.
PEP 8 is the style guide for Python code. Key rules: 4 spaces for indentation, max 79 characters per line, two blank lines between top-level functions/classes, one blank line between methods, lowercase with underscores for variable names.
A module is a single .py file containing Python code. A package is a directory containing multiple modules and an __init__.py file. Use import to access them. Example: import os, from datetime import datetime.
Sign up free to access complete notes — DSA, System Design, HR and more.
🚀 Access Full Notes Free →Build a professional resume to complement your preparation.
Build Free Resume →