Table of Contents
Why Comments Matter
Comments are notes you write for humans, not for the computer. Python ignores them when it runs your program.
You use comments to:
- Explain what your code is doing
- Explain why you chose a certain approach
- Leave reminders for things to fix or improve later
- Temporarily disable code without deleting it
Good comments make your code easier to read, understand, and maintain—both for you and for anyone else who reads it later.
Single-Line Comments with `#`
In Python, the most common kind of comment is a single-line comment. It starts with # and continues to the end of the line.
# This is a single-line comment
print("Hello") # This is a comment after some code
Everything after # on that line is treated as a comment and ignored by Python.
Examples:
# Ask the user for their name
name = input("What is your name? ")
# Greet the user
print("Hello,", name)
age = 25 # age in yearsYou can place comments:
- On a line by themselves
- At the end of a line of code (after some whitespace for readability)
Commenting Out Code (Temporarily Disabling Code)
Sometimes you want to turn off a line of code without deleting it—maybe for testing or debugging. You can do this by turning that line into a comment:
print("This will run")
# print("This will NOT run")
Python will ignore the second line because it starts with #.
You can also comment out several lines by putting # at the start of each line:
# print("Line 1")
# print("Line 2")
# print("Line 3")This is useful when you are experimenting with different versions of your code.
Multi-Line Comments (Using Multiple `#`)
Python does not have a special “multi-line comment” symbol like some other languages. The usual way is to use multiple # lines:
# This program calculates the area of a rectangle.
# Formula:
# area = width * height
width = 5
height = 3
area = width * height
print("Area:", area)
Each line starting with # is a separate comment line, but together they form a multi-line explanation.
Docstrings vs Comments (Very Briefly)
You might see text inside triple quotes like this:
"""
This looks like a multi-line comment,
but it's actually a string called a docstring.
"""
Docstrings are mainly used to document modules, functions, and classes. They are different from comments and have special uses later on, especially in functions. For simple notes in your code, prefer # comments.
Where to Put Comments
Comments are most helpful when they answer questions a reader might have, such as:
- “What is this piece of code supposed to do?”
- “Why is it done this way and not another way?”
- “What does this strange-looking code mean?”
Typical places to add comments:
- At the top of a file: A short description of what the program does.
# Simple temperature converter: Celsius to Fahrenheit- Above a block of code: To explain its purpose.
# Convert Celsius to Fahrenheit using the formula:
# F = C * 9/5 + 32
celsius = 20
fahrenheit = celsius * 9/5 + 32
print(fahrenheit)- Next to complex or non-obvious lines: To explain what’s going on.
seconds_per_day = 24 * 60 * 60 # 24 hours * 60 minutes * 60 secondsGood vs Bad Comments
Not all comments are helpful. Some are unnecessary or even confusing.
Redundant (Bad) Comments
Comments that just repeat the code are usually not helpful:
x = 5 # set x to 5
y = x + 10 # add 10 to x and store in yThe code already says that. A better comment explains the meaning:
x = 5 # starting score
y = x + 10 # bonus points added to the scoreHelpful Comments
Helpful comments explain intent (the “why”) or clarify non-obvious details:
# Using 1.8 and 32 for Celsius to Fahrenheit conversion
fahrenheit = celsius * 1.8 + 32Or:
# Use integer division to get full minutes (discard seconds)
minutes = total_seconds // 60Style Tips for Comments
Some simple habits make your comments cleaner and easier to read:
- Start sentences with a capital letter and end with punctuation, like normal writing.
- Leave one space after the
#:
# Good comment
#bad comment
#Another bad comment- Keep comments short and focused.
- Update comments when you change the code. An incorrect comment is worse than no comment.
Using Comments to Plan Your Code
Comments can help you plan a program before you write the actual code. This is sometimes called “comment first” or “pseudo-code”.
Example:
# 1. Ask the user for their name
# 2. Ask the user for their age
# 3. Calculate the year when they will turn 100
# 4. Print a message with that yearThen you fill in the code under each step:
# 1. Ask the user for their name
name = input("What is your name? ")
# 2. Ask the user for their age
age = int(input("How old are you? "))
# 3. Calculate the year when they will turn 100
current_year = 2025
years_to_100 = 100 - age
year_turn_100 = current_year + years_to_100
# 4. Print a message with that year
print(name, "will turn 100 in the year", year_turn_100)Comments guide your thinking and help you break a problem into smaller steps.
Commenting Practice Ideas
Try these small exercises to get comfortable using comments:
- Take a short program you’ve already written and:
- Add a comment at the top describing what it does.
- Add at least two comments explaining why you do something.
- Write a new tiny program (like adding two numbers), but:
- First, write the steps using comments.
- Then, write the Python code under each comment.
- Take a piece of code with no comments (maybe from an example online) and:
- Add comments to explain what it does line by line or block by block.
Use comments to make your code understandable to “future you” who may not remember what you were thinking today.