Python is one of the most readable programming languages, but readability does not happen automatically. Here are a few simple techniques to make your code better.
1. Use f-strings instead of concatenation
f-strings were introduced in Python 3.6 and quickly became the de facto standard for string formatting. They are clearer and faster than older approaches.
# Bad
name = "World"
greeting = "Hello, " + name + "!"
# Good
greeting = f"Hello, {name}!"
2. Prefer enumerate() over manual counters
If you need an index while iterating over a list, use enumerate() instead of managing a counter yourself.
# Bad
i = 0
for item in items:
print(i, item)
i += 1
# Good
for i, item in enumerate(items):
print(i, item)
3. Use dataclasses for data structures
Instead of regular classes with __init__ and a set of attributes, use @dataclass — it reduces boilerplate and provides repr/eq for free.
4. Write small functions
A function should do one thing and do it well. If a function is longer than 20–30 lines, consider splitting it.
5. Document your public API
Add docstrings to public functions and classes. This helps your teammates — and your future self six months later.
Following these simple rules will make your code easier to maintain and nicer to read.