Python Tips
Welcome to Python Tips. This section collects short, practical lessons that help you write better Python, avoid common mistakes, and discover useful parts of the language and standard library.
Code structure
Tips for organizing Python code so it stays easier to read, test, and maintain.
Small, focused modules
Separate configuration
Separate logic from I/O
Run code only in main
Type hints for interfaces
Dataclasses for data
Composition vs inheritance
Pathlib for file paths
Unexpected Python behavior
Python has a few behaviors that are correct but surprising. These are the ones that cause real bugs most often.
Closures in loops
Shallow vs deep copies
Mutable defaults
dict.get() - missing vs None
Exception handler order
Float comparisons
Avoid import *
Identity vs equality
Mutating while iterating
Repeated nested lists
sort() vs sorted()
Truthiness and missing values
Unexpected useful patterns
Some Python features look obscure at first, but become very useful once you know when to use them.
Boolean checks with any() and all()
Counter and defaultdict
Context managers
Loop else blocks
enumerate() for indexes
Generator expressions
Match/case
Sentinel objects
Tuple unpacking
zip() for parallel iteration
Standard library gems
Python's standard library solves many everyday problems well, but a lot of its best tools stay underused.
Bisect and heapq
Collections
Contextlib
CSV
Dataclasses
Decimal
Difflib
Fractions
Functools
Itertools
Pathlib
Pprint
Secrets
Shutil
SQLite
Statistics
Subprocess
Tempfile
Timeit
Traceback
Typing
UUID
Zoneinfo
If you are still learning the basics, start with the Python Guides. If you need exact syntax or API details, use the Python Language Reference.