Code blocks and scope in Python
What a code block is in Python, why if, for, while and try don't create a new scope, which blocks do (functions, classes, comprehensions), and the bugs that come from mixing them up.
23 entries
What a code block is in Python, why if, for, while and try don't create a new scope, which blocks do (functions, classes, comprehensions), and the bugs that come from mixing them up.
A dict stores values by key instead of by position. Creating one, reading safely with get, adding and removing, looping over keys and values, counting, nesting, and the keys you can use.
What happens when Python hits an error at runtime: reading a traceback, catching errors with try/except, else and finally, raising your own exceptions, and why a bare except is a trap.
def, parameters and return values, default and keyword arguments, the mutable default trap, docstrings and type hints, and passing a function as a value.
How Python decides which code to run: if, else and elif, the comparison operators, truthy and falsy values, the conditional expression (x if C else y), and a first look at match.
A list holds many values in order. Creating one, indexing and slicing, adding and removing items, sorting, copying without surprises, and a word on tuples.
How and, or and not combine conditions in Python: truth tables, short-circuit evaluation, why and/or return one of their operands, precedence, and any() and all().
Running code again and again: for over a list or a range, enumerate and zip, while, break and continue, the else clause on a loop, and the loop bugs everyone writes once.
What % does in Python and where it's useful: even or odd, every Nth item, wrapping around a list or a clock, splitting seconds with divmod, digits, FizzBuzz, and how it treats negative numbers.
How to work with dates in Python using datetime.date: creating dates, reading parts of a date, formatting with strftime and isoformat, date arithmetic with timedelta, and invalid dates.
How f-strings work in Python: putting expressions in braces, format specs for width, alignment, precision, thousands separators and percentages, the = debug specifier, and !r conversions.
How Python uses indentation to group code, the four IndentationError messages you'll meet, why tabs and spaces can trigger a TabError, and how to find and fix them.
How input() reads a line from the user in Python: the prompt, why the result is always a str, converting it with int() and float(), the ValueError on bad input, and EOFError.
The arithmetic operators in Python: + - * / // % and **, the difference between true and floor division, round, the math module, and why floats aren't exact.
How pip installs third-party packages from PyPI: install, pin versions, list, show, freeze into requirements.txt, uninstall, and why to use a virtual environment with python -m pip.
How print() works in Python 3: printing several values, the sep and end arguments, printing to stderr, and the common mistake of printing None.
Where a variable can be seen and changed in Python: local, global and nonlocal scope, the global and nonlocal keywords, shadowing, and how to choose clear names that follow PEP 8.
Python strings: quotes and escapes, raw and triple-quoted strings, indexing and slicing, immutability, the common str methods for changing, splitting and searching text.
How to convert between Python types with int(), float(), str(), bool(), list(), tuple() and set(): what each conversion does, truncation, bases, truthiness, and the errors when a conversion fails.
The common TypeError messages in Python and what they mean, checking types with type() and isinstance(), why type hints are not enforced, and when to check types versus catching the error.
What uv is and how to use it: the project workflow with uv init, uv add and uv run, the .venv, pyproject.toml and uv.lock, plus uv venv and uv pip for pip-style work.
How variables work in Python: assignment, names, the basic types int, float, str, bool and None, type(), and the NameError.
What Python is, what it's good for, and how it differs from languages like C and Java: interpreted, dynamically typed, indentation-based and batteries included.