←Previous page

11CLEAN CODE · COMMENTS

Leave notes for people.

A line that starts with # is a comment: a note for people that Python skips.

IN REAL LIFE

Notes help people remember.

People leave short notes so others know the plan or the reason.

A note on the fridge

"Save the cake for Sunday!" The fridge ignores it. It is for your family.

A recipe tip

"Stir slowly so it won't burn." It tells you why, not only what.

Notes in a textbook

Your teacher writes "Test on Friday!" in the margin. The book stays the same.

LOOK FIRST

How comments work.

# starts a note

Python skips everything after # on that line. It is only for people.

Say why, not what

# add 1 is obvious. # bonus for finishing first explains the reason.

Keep notes true

When you change the code, change the comment too. Python never checks it.

LOOK FIRST

Read the notes line by line.

Lines with # are for people. The others are for Python.

# Plan: count the cookies for the partyThe plan, for people. Python skips it.
cookies = 12Python runs this: a box with 12.
# Mia brings 6 more for her friendsExplains WHY the next line adds 6.
cookies = cookies + 6Now the box keeps 18.
# print("Not yet!")A line switched off with #. Python skips it.
print("Cookies:", cookies)A blank line before it groups the steps: count first, then show.

RUN IT

PyBot counts party cookies.

Guess first. Then run real Python and check.

1

Guess the result

# Plan: count the cookies for the party cookies = 12 # Mia brings 6 more for her friends cookies = cookies + 6 # print("Not yet!") print("Cookies:", cookies)

What will Python show?

cookies.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Delete the # before print("Not yet!") and run again.

YOUR TURN

Notes for people.

Try each tiny question. A wrong answer becomes something to review.

0Completed
9Not tried
0Review
01

A helpful note

A good comment explains the reason.

Which note on a cake box helps most?

02

What runs?

# print("Hello") print("Bye")

What will Python show?

03

The best comment

lives = 3

Which comment helps most above this line?

04

An old note

# PyBot has 5 stickers stickers = 8 print(stickers)

What will Python show?

05

Switch a line off

print("Start") print("Beep beep beep!") print("End")

How do you skip line 2 for a test, without deleting it?

06

Find the running line

# score = 10 score = 5 # starting score #print(score)

Which line does Python really run?

07

Blank lines

A program has a blank line between the counting steps and the printing step.

What does the blank line do?

08

Fix PyBot's code

Tickets now cost 3 coins, but an old comment still says 2, and line 5 trusted it. Fix the code and the comment.

Goal output
Total: 12 coins
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Comments are notes for people.

Python skips everything after #. Explain why, keep notes true, and use blank lines to group steps.