Python Reference Guide — Basics to Expert

← Hub · SQL Reference Guide

Python 3–oriented. Good for data engineering / backend screens. Verify version-specific details (typing, match) before the interview.

Basics — read and write small programs

Indentation defines blocks (no braces). Convention: 4 spaces.

name = "Ada"
scores = [10, 20, 30]
user = {"id": 1, "active": True}


if user["active"]:
    total = sum(scores)
    print(f"{name}: {total}")

Core building blocks

Intermediate — shape data without spaghetti

List & dict comprehensions

squares = [x * x for x in range(10) if x % 2 == 0]
by_id = {u["id"]: u for u in users if u.get("active")}

Context managers (with)

Guarantees cleanup (close files, release locks)—even if an error happens.

with open("data.csv", encoding="utf-8") as f:
    first = f.readline()

Exceptions

try:
    risky()
except ValueError as e:
    log.warning("bad value: %s", e)
finally:
    cleanup()

*args and **kwargs

Variable positional and keyword arguments—common in wrappers and APIs.

Modules & scripts

if __name__ == "__main__": runs only when the file is executed directly, not when imported—clean pattern for CLI tools.

Expert — patterns senior interviews probe

Decorators

A function that takes a function and returns a new function (often adds logging, timing, auth). Know the idea and @decorator sugar even if you don’t write one from scratch cold.

Generators & yield

Lazy iteration—one item at a time, low memory. Streaming large files/lines is a classic win.

def lines(path):
    with open(path, encoding="utf-8") as f:
        for line in f:
            yield line.rstrip("\n")

Type hints (PEP 484 style)

def total(xs: list[int]) -> int:
    return sum(xs)

Hints are optional at runtime (unless you use a checker); they document contracts and help IDEs.

Concurrency snapshot

Data & tooling (real jobs)

TopicWhy it shows up
venv + pipReproducible environments; “works on my machine” antidote.
pytestSmall unit tests for transforms (pure functions) save pipelines.
pandas (if DE role)Vectorized transforms vs row loops—know when to push work to SQL/Snowflake instead.
logging not printStructured logs in jobs running in Airflow / containers.
Interview line: “I keep heavy aggregation in the warehouse; Python orchestrates, validates, and handles exceptions around the edges.”

Interview angles — Python edition

Top Python interview questions (click to expand)

Model answers for rapid recall—deep dives depend on role.

What is mutable vs immutable? Give examples.

Immutable: int, float, str, tuple, frozenset—new objects on “change”. Mutable: list, dict, set—in-place updates affect all references to that object.

What is the difference between == and is?

== compares value (calls __eq__). is compares object identity (same memory). Use is for singletons like None, True, False—never == None in idiomatic Python.

Explain *args and **kwargs.

*args collects extra positional arguments into a tuple. **kwargs collects keyword arguments into a dict. Used in decorators and flexible APIs.

What is a decorator?

A callable that wraps a function to add behavior (logging, caching, auth). @timer above def f() is rewritten to f = timer(f) (conceptually).

What is a generator? Why use yield?

A generator function uses yield to produce a stream of values lazily—saves memory vs building a giant list and enables pipeline-style processing.

What is the GIL?

The Global Interpreter Lock allows one thread to execute Python bytecode at a time in CPython—threads don’t parallelize CPU-bound Python work; use multiprocessing or native extensions for parallel CPU, or async for I/O-bound concurrency.

Shallow copy vs deep copy?

Shallow: new outer container, inner objects shared. Deep: recursive copy of nested structures—use copy.deepcopy when nested mutables must be independent.

What does with open(...) as f do under the hood?

Uses a context manager (__enter__ / __exit__) so the file is closed reliably, including on exceptions.

What keys can a dict have?

Keys must be hashable (immutable types like str, int, tuple of immutables). Lists and dicts cannot be keys.

How does Python pass arguments?

“Pass by object reference”: names bind to objects; assigning inside a function rebinds the local name—mutating a mutable object passed in is visible outside, rebinding the parameter is not.

List comprehension vs map/filter?

Comprehensions are idiomatic and often clearer; map/filter with lambdas can be harder to read. Performance is usually comparable for small data.

What is if __name__ == "__main__" for?

Code that runs when the script is executed directly, but not when imported as a module—lets you mix library + CLI in one file.

What are list / dict comprehensions?

Compact syntax to build lists/dicts from iterables with optional filtering—readable when kept short; nested comprehensions can harm readability.

How do you handle errors in production pipelines?

Specific exceptions, retries with backoff for transient failures, dead-letter paths, structured logging, and idempotent stages so replays are safe.

Pair with SQL Reference Guide and SnowPro study for warehouse-side questions.