Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/Python/Tips & tricks
Py

Python

Readable, batteries-included, and the default language of AI.

Paradigm
Multi-paradigm, OOP, functional
Typing
Dynamic, strong, optional type hints
Memory
Reference counting + cycle GC
Runs
CPython bytecode interpreter
Concurrency
asyncio, threads (GIL), multiprocessing

Tips & tricks

Small idioms with a big payoff.

Comprehensions, enumerate and zip

Build collections and loop with indexes cleanly.

squares = {n: n * n for n in range(10) if n % 2 == 0}   # dict comprehension
for i, (name, score) in enumerate(zip(names, scores), start=1):
    print(f"{i}. {name}: {score}")

Dataclasses

Boilerplate-free value objects.

from dataclasses import dataclass, field

@dataclass(frozen=True, slots=True)
class Point:
    x: float
    y: float
    tags: tuple[str, ...] = field(default_factory=tuple)

# __init__, __repr__, __eq__ and __hash__ are generated for you

Walrus operator and context managers

Assign in expressions; guarantee clean-up.

if (n := len(items)) > 100:          # walrus: assign inside an expression
    print(f"Too many items: {n}")

with open("journal.txt") as f:       # context manager closes the file for you
    lines = [line.strip() for line in f]

Saved in this browser only. Part of Clean code foundations (0/5).