Backend6 min read

Python Fundamentals

Master Python from first principles - data types, functions, OOP, comprehensions, decorators, and the standard library for real-world development.

PythonProgrammingScriptingOOPData Structures

skill: Python Fundamentals category: Backend tags: [Python, Programming, Scripting, OOP, Data Structures] readingTime: 6

Python's strength is readability - code that reads like English is easier to maintain than clever one-liners. Follow the Zen of Python: explicit is better than implicit; simple is better than complex. Prefer the standard library before reaching for a package.

Workflow

  1. Master data structures and comprehensionsLists, dicts, sets, and tuples are the foundation of every Python program. Comprehensions express transformations in one line without sacrificing readability.
  2. Write clean functions with type hintsType hints are not enforced at runtime but enable IDE autocomplete, mypy static analysis, and self-documenting code.
  3. Use classes and dataclassesDataclasses eliminate boilerplate (__init__, __repr__, __eq__) for data containers while keeping full OOP capability.
  4. Apply decorators and context managersDecorators add cross-cutting concerns (logging, timing, caching) without modifying function bodies. Context managers guarantee cleanup.
  5. Use the standard library effectivelyPython ships with batteries included - itertools, collections, pathlib, json, logging, and datetime cover 80% of common needs without extra dependencies.

Output Format

Python code: type hints, docstrings, dataclasses for data, pathlib for files, standard library first.

from dataclasses import dataclass
from typing import Optional
from pathlib import Path

@dataclass
class MyClass:
    field: str

    def method(self) -> Optional[str]:
        """Docstring explains what and why."""
        ...

Examples

Input

Output

Edge Cases & Gotchas

  • is vs == - "is" checks identity (same object in memory); "==" checks equality. Never use "is" to compare values - only for "is None", "is True", "is False".
  • Late binding in closures - Lambda in a loop captures the variable by reference: [lambda: i for i in range(3)] all return 2. Fix: lambda i=i: i (default argument).
  • GIL and threading - Python's GIL prevents true multi-core parallelism for CPU-bound code. Use multiprocessing for CPU-bound; asyncio or threading for I/O-bound tasks.
  • Circular imports - Importing module A from module B and B from A causes ImportError. Restructure code or use lazy imports (import inside the function).

When to Break These Rules

  • Use Rust/Go/C extensions for performance-critical inner loops - Python is 10–100x slower than compiled languages.
  • Use TypeScript instead of Python when building a web frontend - JS/TS is the native language of the browser ecosystem.

Reference Files

  • https://docs.python.org/3/
  • https://www.pythonmorsels.com/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.