▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Writing readable code
Names, docstrings and the PEP 8 style guide.
🎬 Video version coming soon — read it now
Code is read far more often than it's written: by your teammates, your interviewer, and you in six months. Readable code is a professional skill, not decoration.
Names that explain
Good names remove the need for most comments.
Docstrings
A docstring is a string right under def that says what the function does:
help(bmi) shows it, and editors display it when you hover. Comments elsewhere should explain why, not repeat what.
Small functions, one job each
If a function scrolls off the screen, split it. A function called process_data that reads a file, cleans it, calculates and prints a report should be four functions.
No magic numbers
PEP 8: Python's style guide
The community agrees on one style, called PEP 8. The main points:
- 4 spaces per indent, never tabs
snake_casefor variables and functions,CapWordsfor classes,CAPSfor constants- Spaces around operators:
x = a + b, notx=a+b - Lines under about 80–100 characters
- Two blank lines between top-level functions
You don't have to memorise it. Tools like ruff and black check and fix style automatically; you'll set them up in the Production Python course.
Simpler is better
import this in Colab to read "The Zen of Python". "Readability counts" is one of its lines, and Python developers really do live by it.In one line
Use clear names, docstrings, small single-job functions and named constants, and follow PEP 8.
Share with a friend