Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Python code showing a reverse range loop with a downward arrow, illustrating descending iteration
Python

Python Reverse Range: How to Iterate Backward

python reverse range: Learn how to create and use reverse ranges in Python with range() and reversed(), including practical examples and common pitfalls.

Pythonrangeiterationreversed
Illustration of Python negative range showing a descending sequence from 5 to 1 with a downward arrow
Python

Python Negative Range: Descending Sequences with range()

Learn how to use Python range() with a negative step to generate descending sequences, handle edge cases, and avoid off-by-one errors.

pythonrangenegative steploops
Illustration of Python range syntax with start, stop, and step parameters shown as a number line.
Python

Python range syntax: start, stop, step

Understand the python range syntax for start, stop, and step, including lazy evaluation, negative steps, and practical loop patterns.

pythonrangeiterationfor-loop
Illustration of Python range object generating a sequence of numbers lazily
Python

Python range: How It Works and When to Use It

python range: Understand Python's range object: how it generates sequences lazily, its memory efficiency, and when to convert it to a list.

rangeiteratorsloopslazy evaluation
Illustration of a Python dictionary with custom objects as keys, highlighting hashability.
Python

Python Hashable Dictionary Keys: How to Use Them Correctly

Learn what makes Python objects hashable, how to use custom classes as dictionary keys, and how to avoid mutable-key pitfalls.

PythonDictionariesHashableCustom Objects
A stylized dictionary with a key and lock icon, representing the requirements for Python dictionary keys.
Python

Python Dictionary Key Requirements

Python dictionary keys must be hashable and unique. This article explains why stable, effectively immutable keys are the safe choice and how to use custom objects correctly.

hashableimmutabilitydictionary keysPython data structures
Illustration of a Python dictionary with numbered entries showing insertion order preservation.
Python

Python Dict Order Preservation

Understand Python's dict order preservation guarantee, its history, practical implications, and when OrderedDict is still worth using for compatibility.

pythondictordereddictinsertion order
Illustration comparing a plain Python dict and a defaultdict with a default factory for missing keys.
Python

Python dict vs defaultdict: Choosing the Right Mapping

Compare Python's dict and defaultdict: understand missing-key behavior, performance trade-offs, and decide which mapping fits your data-processing needs.

dictdefaultdictPython collectionsmissing keys
A Python dictionary with a missing key highlighted, showing a KeyError being handled safely with a fallback value.
Python

Python KeyError Handling: Safe Dictionary Access

Learn practical ways to handle Python KeyError when accessing dictionaries: use .get(), setdefault, defaultdict, and try/except, with trade-offs for each approach.

KeyErrordictionaryexception handlingdefaultdict
Illustration of a Python dictionary with a missing key highlighted, showing the safe access and default value approaches
Python

Python Missing Dictionary Key: Safe Access and Defaults

Practical guide to handling missing dictionary keys in Python: KeyError, get(), setdefault(), defaultdict, and when to use each approach.

KeyErrordefaultdictdict.getexception handling