Welcome to the ultimate Python reference repository. This document provides a quick syntax overview of every topic covered in this directory, mapped to the respective runnable files.
- Description: Basic output, input, and python design history/pros/cons.
- Syntax:
print("Hello World") # input() takes string console input: name = input("Enter name: ")
- Description: Comments and code documentation.
- Syntax:
# Single line comment """ Docstring used to document a function, accessible via func.__doc__ """
- Description: Overview of core primitive, non-primitive, and user-defined datatypes.
- Syntax:
x: int = 10 y: float = 3.14 name: str = "Alice" is_true: bool = True empty: None = None
- Description: Variables, memory references (
id()), and basic string transformations. - Syntax:
x = 10 print(id(x)) # Unique memory address name = "Abbas Ali" print(name.upper()) # "ABBAS ALI"
- Description: Variables, assignments, type casting, error handling, and multi-inputs.
- Syntax:
x = y = z = 0 # Multiple assignment a, b = 1, 2 # Unpacking assignment # Type casting num = int("42") val = float(5) string = str(100) boolean = bool("hi") # True # Multi-input mapping num1, num2 = map(int, input().split())
- Description: Mathematical operations, PEMDAS order, logical/comparison operators, and the walrus operator.
- Syntax:
# Division and Floor Division print(10 / 3) # 3.3333333333333335 (float) print(10 // 3) # 3 (drops decimal) print(10 % 3) # 1 (remainder) print(2 ** 3) # 8 (exponent) # Walrus operator (assigns inside expressions) if (n := len("Hello")) > 3: print(n)
- Description: if-statements, ternary operators, loops, and loop control statements.
- Syntax:
# Ternary result = "Adult" if age >= 18 else "Minor" # Loops for i in range(0, 5, 2): # start, stop, step print(i) # 0 2 4 # Loop Else Clause (runs if loop finished without 'break') for x in range(3): pass else: print("No break encountered!") # Simulated Do-While Loop while True: # code runs once if not condition: break
- Description: Function declarations, default values, args, kwargs, nested functions, lambdas, and recursion.
- Syntax:
def complex_func(a, b=2, *args, **kwargs): # args packed as tuple, kwargs packed as dict return a + b + sum(args) # Lambda/Anonymous function square = lambda x: x * x # Recursion def factorial(n): return 1 if n == 1 else n * factorial(n - 1)
- Description: Lists, tuples, dictionaries, sets, slicing, unpacking, and set operations.
- Syntax:
# Slicing: list[start:stop:step] nums = [1, 2, 3, 4, 5] print(nums[1:4]) # [2, 3, 4] print(nums[::-1]) # [5, 4, 3, 2, 1] (reversed) # Tuple Unpacking and Swapping x, y = 10, 20 x, y = y, x # Swap variables # Set operations a = {1, 2, 3} b = {3, 4, 5} print(a | b) # Union {1, 2, 3, 4, 5} print(a & b) # Intersection {3} print(a - b) # Difference {1, 2}
- Description: f-strings, common string methods, and comprehensive Regular Expressions (
re). - Syntax:
# Formatting floats/percentages in f-strings percentage = 0.8756 print(f"{percentage:.2%}") # "87.56%" # Common string methods " hello ".strip() "apple banana".split() ",".join(["a", "b", "c"]) # Regex functions import re re.findall(r"\d+", "Price 100, Qty 50") # ['100', '50'] re.sub(r"\d", "X", "Room 404") # "Room XXX"
- Description: Comprehensions across lists, dictionaries, sets, and generators.
- Syntax:
# List Comprehension squares = [x**2 for x in range(5) if x % 2 == 0] # Dictionary Comprehension square_dict = {x: x**2 for x in range(3)} # Set Comprehension unique_set = {x for x in [1, 1, 2, 3]} # Generator Expression (calculates on-the-fly) gen_exp = (x**2 for x in range(10000))
- Description: Built-in libraries (
math,random,os,sys,datetime), custom imports, and package directory initialization. - Syntax:
import math as m from datetime import datetime # Package structure requires a folder containing a __init__.py file: # my_package/ # ├── __init__.py # └── module.py
- Description: File modes,
withstatements, JSON reading/writing, cursor seek/tell operations, and modernpathlib. - Syntax:
# Pathlib Paths from pathlib import Path path = Path("test.txt") path.write_text("Hello!") content = path.read_text() # JSON serialization import json with open("data.json", "w") as f: json.dump({"name": "Alice"}, f)
- Description: Catching multiple exceptions, raising custom error classes,
else, andfinallyconstructs. - Syntax:
class CustomError(Exception): pass try: x = 10 / int(input()) except (ValueError, ZeroDivisionError) as e: print(f"Handled error: {e}") else: print("No error occurred!") finally: print("Always executes!")
- Description: Custom
__iter__classes, generatoryieldpauses, custom function decorators, anditertoolslibrary functions. - Syntax:
# Generator def count_up(): yield 1 yield 2 # Decorator from functools import wraps def my_decorator(func): @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper # Itertools import itertools itertools.count(10, 2) # 10, 12, 14...
- Description: Closure scope caching, lambdas, and mapping/filtering/reducing lists.
- Syntax:
# Closure def outer(factor): return lambda val: val * factor double = outer(2) # map, filter, reduce list(map(lambda x: x*2, [1, 2])) list(filter(lambda x: x > 5, [3, 8])) from functools import reduce reduce(lambda x, y: x + y, [1, 2, 3]) # 6
- Description: PEP 484 type hints, annotations, union/optional types, and static analysis checkers.
- Syntax:
from typing import List, Dict, Union, Optional def greet(name: str, aliases: List[str]) -> Optional[str]: return f"Hello {name}" if name else None
- Description: Building custom context managers with classes (
__enter__/__exit__) or generators (@contextmanager). - Syntax:
# Class-based class CustomManager: def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): pass # Generator-based from contextlib import contextmanager @contextmanager def resource(): # setup yield # cleanup
- Description: OOP principles, inheritance structures, constructor overrides, and custom arithmetic operators.
- Syntax:
class Child(Parent1, Parent2): # Multiple inheritance def __init__(self, name): super().__init__(name) def __add__(self, other): # Operator Overloading (+) return self.value + other.value
- Description: Class instantiation via
__new__, custom metaclasses (typeinheritance), and descriptor property validation protocols. - Syntax:
# Metaclass class Meta(type): def __new__(cls, name, bases, dct): return super().__new__(cls, name, bases, dct) # Descriptor Protocol class Descriptor: def __get__(self, instance, owner): pass def __set__(self, instance, value): pass
- Description: Auto-generating class methods, field attributes customization, mutable default values, and slots performance.
- Syntax:
from dataclasses import dataclass, field @dataclass(slots=True) class Point: x: float y: float history: list = field(default_factory=list)
- Description: Coroutines, async event loops, gathering async functions, and concurrent task execution.
- Syntax:
import asyncio async def worker(): await asyncio.sleep(1) async def main(): await asyncio.gather(worker(), worker()) asyncio.run(main())
- Description: Threads, GIL constraints, Locks/Semaphores, OS processes, and process pool data distribution.
- Syntax:
import threading lock = threading.Lock() with lock: # Critical section # Multiprocessing Pool import multiprocessing with multiprocessing.Pool() as pool: results = pool.map(func, data_list)
- Description: Interacting with native C libraries using ctypes, Cython, and cffi.
- Syntax:
import ctypes libc = ctypes.CDLL("libc.so.6") libc.puts(b"Hello standard C library puts!")
- Description: Reference count diagnostics, cyclical reference cleanup (
gc), and Python 3.13 free-threaded no-GIL architecture. - Syntax:
import sys, gc sys.getrefcount(obj) # Get references count gc.collect() # Force cyclical garbage collection
- Environments_Packaging.py: Commands for
venv,poetry,uv,pyenv,pyproject.tomlconfiguration, and uploading packages to PyPI. - Testing.py: Unit testing structure with
pytesttesting checks, fixtures, and mock clients. - Code_Quality.py: Configuration guidelines for code linters and formatters (Black, Ruff, pre-commit).
- Logging_Debugging.py: Logger severity structures and entering interactive debugging using
breakpoint(). - Profiling.py: Performance measurement using
timeit, program run diagnostics usingcProfile, and line-by-line memory tracking. - Scripting_Automation.py: Invoking shell scripts using
subprocessand developing command line programs withclickortyper. - Ecosystem_Frameworks.py: Quick templates for web APIs (FastAPI, Flask, Django), Database ORMs (SQLAlchemy, Alembic migrations), asynchronous job task queues (Celery, Redis), data analysis (NumPy arrays, Pandas dataframes), and machine/deep learning libraries (scikit-learn, PyTorch).
- Design_Patterns.py: Implementing Factory class creation, Observer state subscriptions, and Singleton instances.