Comments

Module 2 · Comments · Lesson 1 of 2

Comments — why, when, and how?

Not everything in a program is meant for the computer. A comment is a note for the humans reading the code — and Python ignores it completely. You write one with a hash, #: everything from the # to the end of the line is invisible to Python.

Everything after # is ignored
# Greet the user
print("Hi")# show it on screen
output
Hi

That's the how: a # on its own line, or tacked onto the end of a line of code. The why is that code tells you what happens, but rarely why — and the why is what your future self will have forgotten.

As for when: a good comment explains intent or a tricky bit — why a value is what it is, or what a confusing line is for. A bad one just restates the obvious (# add 1 to x above x += 1 earns its keep nowhere). Comment the surprising, not the self-evident.

Run it yourselfruns in your browser · Python
main.py

Run it: only the line without a leading # does anything. Python has no special multi-line comment — for a longer note, you just start each line with its own #.