Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.11 — Comments, Names and PEP 8
Python · Example 1 of 2

A commented program with a constant

The last print shows that a # inside quotes is just text.

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.

01

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.

02

Write a program with a module docstring and print the docstring.

03

Rewrite A=5;B=A*2 in PEP 8 style on two lines with good names.

Explain it without notes

01

What makes a good comment, and what makes a bad one?

02

What is a docstring, and how is it different from a comment?

03

List four rules from PEP 8 and say why they matter.

04

What is the naming convention for variables, classes and constants in Python?

A commented program with a constant
Sign in to run this example in your browser.

Expected output

Total with tax: 236.00
# this is inside a string, not a comment