A docstring and the Zen of Python
import this prints the poem as soon as it is imported. The docstring is printed last.
A comment is a note for human readers that Python ignores. Good code is mostly readable names, with comments explaining *why*. PEP 8 is Python's shared style guide (four-space indents, snake_case names, CAPITAL constants), and tools such as Ruff apply it automatically.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Rewrite this code in good style: r = 0.05, then m = 1000, then i = m * r, then print(i). Rename the variables so they say what they hold, make the rate a CAPITAL constant, and add two comments that explain why.
Write a program with a module docstring and print the docstring.
Rewrite A=5;B=A*2 in PEP 8 style on two lines with good names.
Explain it without notes
What makes a good comment, and what makes a bad one?
What is a docstring, and how is it different from a comment?
List four rules from PEP 8 and say why they matter.
What is the naming convention for variables, classes and constants in Python?
Expected output
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Shopping total.
A docstring is a string written at the very top of a file.