What are comments?
Comments are notes you write inside your program to describe what the code is doing. They are written for people, not for the computer. When Python runs your program, it skips right over every comment as if it were invisible.
In Python, a comment starts with the # symbol (called a hash or pound sign). Everything after the # on that line is part of the comment. Run this program and watch the console: the comments never show up in the output.
# This program says hello # Comments never appear in the output print("Hello from Python!")
Hello from Python!
Think of comments as sticky notes. A sticky note on your desk reminds you what to do - it does not change what your computer does. Comments work exactly the same way: they change nothing about how the program runs.
Why bother commenting?
If Python ignores comments, why write them at all? Three big reasons:
- Future you will forget. Code that makes perfect sense today can look like a mystery in two weeks. A quick note reminds you what you were thinking.
- Other people read your code. When a teammate (or your teacher) opens your program, good comments help them understand your code without asking you to explain every part.
- Comments help you plan. Before writing any code, you can sketch the whole program as a list of comments - step 1, step 2, step 3 - and then fill in the code under each step. Programmers call this a top-down approach.
# Step 1: greet the player print("Welcome to Math Quest!") # Step 2: ask the first question print("What is 6 * 7?") # Step 3: reveal the answer print("The answer is 42")
Welcome to Math Quest! What is 6 * 7? The answer is 42
The three step comments were the plan, written first. The code was filled in afterward. And once again: none of the comments appear in the console.
Single-line # comments
A comment on its own line
The simplest comment takes up a whole line. Place a # at the start, then write your note. Programmers usually put the comment on the line above the code it describes:
# Ask for the user's name name = input("What is your name? ") print("Nice to meet you,", name)
What is your name? Karel Nice to meet you, Karel
End-of-line (in-line) comments
When a note is short, you can tuck it onto the end of a code line instead. The code before the # still runs normally; only the part after the # is ignored. You do not need anything special to close the comment - it simply ends when the line ends.
name = input("What is your name? ") # user's name print("Hi,", name) # greet them
What is your name? Karel Hi, Karel
Style tip: most programmers put one space after the #, like # my note. The space is not required, but whatever style you pick, stay consistent through your whole program. Tidy comments are part of good coding etiquette.
Multi-line comments
Sometimes one line is not enough - maybe you are describing the whole plan for a program. Python gives you two ways to write longer notes. Which one you use is up to you; just be consistent.
Option 1: a stack of # lines
Simply start each line with its own #:
# Whether or not you choose # to use this method to create # comments is up to you! It # really depends on the complexity # of your code and your preference. print("The comments above never print")
The comments above never print
Option 2: triple quotes
You can also wrap several lines in a pair of triple quotes - either """ or '''. Put one set before the first line of your note and a matching set after the last line:
""" You can choose to write your comments using the type of triple quotes surrounding this message """ print("Triple-quoted notes stay hidden too")
Triple-quoted notes stay hidden too
One small secret: a triple-quoted block is technically a string that Python builds and then throws away because nothing uses it. When it is the very first thing in a file or a function, Python actually saves it as a docstring - a built-in description of that code. For everyday notes, the # is the standard choice.
The comment lab
Time to prove it to yourself. This little lab has two experiments.
Experiment 1: comments change nothing
Flip the switch to hide or show every comment in this program. Watch the console: the output is identical both ways, because Python never reads the comments in the first place.
# Game plan: greet the player, then count down player = "Sam" # the player's name print("Get ready,", player) # three big beeps before the start print("3... 2... 1... GO!")
Get ready, Sam 3... 2... 1... GO!
Comments shown: humans can read the notes, and the output is exactly the same.
Experiment 2: comment out the bug
Someone left a leftover test line in this countdown program, and it is ruining the launch. Click a line of code to comment it out (a # appears and the line grays out), then press Run. Can you silence the right line so the output matches the goal?
3... 2... 1... Liftoff!
Press Run to launch...
Tip: clicking a commented line brings it back. Commenting out code is exactly how programmers hunt bugs - silence a suspect line, run again, and see what changes.
Commenting out code
You just used the second superpower of comments: because Python skips anything after a #, you can put a # in front of a real line of code to switch it off temporarily. Programmers call this commenting out a line.
It is better than deleting the line, because your code is not gone - it is just asleep. If the line turns out to be innocent, remove the # and it wakes right up.
print("Score so far: 10") # print("DEBUG: checking the score") <- switched off, does not run print("Level complete!")
Score so far: 10 Level complete!
Debugging recipe: when a program misbehaves, comment out the line you suspect, run the program, and compare the output. If the problem disappears, you found your bug. If not, un-comment it and try the next suspect.
Good vs. bad comments
Not all comments are equally helpful. A weak comment just narrates what the code obviously says. A strong comment explains the why - the reason or the plan that you cannot see from the code alone.
Two programmers commented the exact same line below. Vote for the comment you think is more helpful, then see the verdict:
score = score + 10 # adds 10 to score
Narrates the obvious
score = score + 10 # bonus for finishing under 60s
Explains the why
Ultimately, what you write in a comment is your choice. The real test: will the next person to read the program - including future you - understand what the code is doing and why? If a comment does not help with that, it is just clutter.
Practice
Comment Tracy's Olympic Rings
This Tracy the Turtle program draws two rows of Olympic rings. Try these three tasks in your own editor:
- Multi-line: the plan at the top uses """ quotes. Change them to ''' and try moving where the note begins and ends.
- In-line: find speed(0) and rewrite its comment to explain what it does (it makes Tracy draw at top speed).
- Single line: add or improve comments anywhere you think the next reader needs help - like above the loop() function.
""" Tracy's feeling like an Olympian! Have Tracy draw 2 rows of rings: 3 on top, 2 on the bottom, each ring a different color. """ # Quickly checks work and prepares Tracy's adventure speed(0) penup() pensize(3) # Try writing a comment here! def loop(): pendown() circle(50) penup() # Top Row: Blue, Black, and Red Rings setposition(-100, 0) color("blue") loop() forward(100) color("black") loop() forward(100) color("red") loop() forward(100) # Bottom Row: Gold and Green Rings setposition(-50, -50) color("gold") loop() forward(100) color("green") loop() forward(100)
(Tracy draws 5 colorful rings: blue, black, and red on top, gold and green below - no comments anywhere in sight)
''' Tracy's feeling like an Olympian! Draw 2 rows of rings: 3 up top, 2 below, each ring a different color. ''' speed(0) # draw at max speed so we see results instantly penup() pensize(3) # thicker pen so the rings stand out # Draws one ring at Tracy's current spot def loop(): pendown() circle(50) penup()
Notice the triple quotes switched from """ to ''' with zero change in behavior, and the speed(0) comment now explains the why. Your comments can be worded differently - what matters is that they help the next reader.
Leave a note for future you
Write a short program that prints your favorite food and your favorite hobby. Include all three comment styles you learned:
- a multi-line note at the top describing the plan,
- a full-line comment above one print,
- an end-of-line comment on the other print.
# Plan: print my favorite food, # then my favorite hobby. # The food line print("Favorite food: pizza") print("Favorite hobby: coding") # obviously
Favorite food: pizza Favorite hobby: coding
Four comments, two lines of output. Every comment stayed invisible - exactly what should happen.
Quick quiz
1. What does Python do with a comment when the program runs?
2. What does this program print? print("Go!") # print("Stop!")
3. Your program's output has one extra, unwanted line. What is the best first debugging move?
Keep learning Python
Now your code can carry notes for every human who reads it. Next up: make your programs talk with print, and teach them to remember things with variables.