Comments
Outside quoted text, anything after a # on the same line is a comment. Python skips the comment.
Comments are notes for humans. They also let you switch off a line without deleting it. Programmers call that "commenting it out".
# This line explains the code below print("I run") # print("I am switched off")
See it run
# Greet, then say goodbye print("Hi") # print("Bye") print("Done") # runs: the # starts mid-line
Watch which lines make Output. Lines starting with # are notes for humans.
Practice comments
Exercise 1
This program prints three steps in a morning routine:
print("Step 1: wake up") print("Step 2: hit snooze 7 times") print("Step 3: write Python")
Edit this program in the code editor:
- Stop the second line,
print("Step 2: hit snooze 7 times"), from running by putting#at the beginning of that line. Keep the line; do not delete it. - Add a new comment at the very top explaining what the program does. Start your comment with
#.
When you run your code, only these two lines should appear in Output:
Step 1: wake up Step 3: write Python
Exercise 2
In the code editor below, the shop receipt includes an internal note. Comment out its print line, keep both customer lines, and add a top comment describing the receipt.
Exercise 3
In the code editor below, prepare the launch countdown. Re-enable the commented Launch! print, disable the Test mode print without deleting it, and keep a descriptive top comment.
Free lesson: use the editor to practice it.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.