Comments

Module 2 · Comments · Lesson 2 of 2

Making fragments of code

The hash has a second, very practical use. Because Python ignores anything after a #, you can put one in front of a line of code to switch it off — without deleting it. This is called commenting out, and that switched-off line is your “fragment.”

Switch a line off, keep it around
print("one")
print("two")
print("three")
output
one
two
three

It's the everyday tool for figuring out what's going on. Suspect one line is causing trouble? Comment it out and run again — if the problem vanishes, you've found it. Want to test an idea without losing the original? Comment the old version out and write the new one beside it.

Run it yourselfruns in your browser · Python
main.py

Try it: delete the # and "two" joins the output; put it back and it's gone again — all without losing a single character of the code. Comment out, uncomment, repeat: it's how a lot of debugging actually gets done.