Python Comments
Table of Contents
In the last lesson, we learned Python syntax basics. Now let’s learn how comments help us write notes inside code.
After learning Python Syntax Basics, we learned the basic rules Python expects, including indentation, quotes, and brackets. Now let’s learn how to write notes inside our code.
What Is a Python Comment?
A comment is a note we write in our code. Python ignores it. It does not run it. It does not care about it. But humans care. Let me show why.
Look at This Code
price = 100tax = 0.18total = price + (price * tax)print(total)Do we know what this does? Maybe yes. Maybe no. It is not obvious.
Now look at this:
price = 100 # The price of the itemtax = 0.18 # 18% tax ratetotal = price + (price * tax) # Add tax to priceprint(total)See the difference? The second one is easier to understand. Because of the comments.
Why Do Python Comments Matter?
Here is the truth. Code tells us what it does. Comments tell us why. And “why” is often more important than “what.”
Think about it. Six months from now, we will look at our own code. And we will think — “What was I doing here? Why did I write it like this?”
Comments save us from that.
They also help other people who read our code. Our teammate. Our boss. The person who takes over our project.
And here is a bonus — when we write a comment, we are forced to think clearly about what we are doing. So comments make us better programmers.
How to Write a Comment in Python
A comment starts with the # symbol. Everything after # on that line is a comment. Python ignores all of it.
# This is a commentprint("Hello") # This is also a commentSee? Python runs the print part. It ignores the # part.
1 Single-Line Comments in Python
This is the most common type. Just put # at the start.
# This program calculates the area of a circleradius = 5area = 3.14 * radius * radiusprint(area)The first line is a comment. Python skips it. The next three lines are real code.
Inline Comments in Python
We can also put a comment at the end of a line of code.
radius = 5 # radius of the circleThis is called an inline comment. It goes on the same line as the code. Let’s use this sparingly. Too many inline comments make code messy.
Multi-Line Comments in Python
Python does not have a special syntax for multi-line comments. But there are two ways to do it.
Way 1: Use # on each line.
# This is a long comment# that spans multiple# linesThis is the cleanest way. It is what most Python programmers do.
Way 2: Use triple quotes.
"""This is also a comment.It spans multiple lines.Python ignores it."""Technically, triple-quoted text is a string, not a comment. But if we do not assign it to anything, Python ignores it. So it works like a comment.
Many people use triple quotes for documentation at the top of a file. Like a description of what the file does.
When Should We Use Python Comments?
Here are good times to use comments.
To explain why we did something:
# Using 3.14 instead of math.pi for simplicityarea = 3.14 * radius * radiusTo describe what a complex piece of code does:
# This loop finds the largest number in the listlargest = numbers[0]for num in numbers: if num > largest: largest = numTo leave notes for ourselves:
# TODO: Fix this later when I have timeresult = old_function()To temporarily disable a line of code:
print("Hello")# print("This line is disabled")print("World")When Should We Not Use Python Comments?
Let’s not write comments that just repeat the code.
Bad:
x = 5 # set x to 5That comment adds nothing. We can already see that x = 5. It is obvious.
Good:
x = 5 # discount rate for premium membersNow the comment tells us something useful. Something we could not guess from the code alone.
Python Comments: A Real-Life Example
Think of comments like sticky notes. We put a sticky note on a document to remind ourselves — “Check this later” or “This number is from the old system.” The sticky note is not part of the document. It does not change anything. But it helps us understand.
Comments are the same. They are notes for humans. Not for the computer.
Let’s Look at a Full Example
# Program: Calculate the average of three numbers# Author: Sam# Date: 2025-01-15
# The three numbersa = 10b = 20c = 30
# Calculate the sumtotal = a + b + c
# Calculate the averageaverage = total / 3
# Show the resultprint("The average is:", average)This is clean, well-commented code. Anyone can read it and understand what is happening. Even someone who has never seen this code before. That is the goal.
A Quick Tip About Python Comments
Let’s not write too many comments. And let’s not write too few.
Too many comments:
# Add 1 to xx = x + 1
# Print xprint(x)
# DoneThis is annoying. The code is already clear. The comments just add noise.
Too few comments:
def process(d, t): r = d * t return rWhat is d? What is t? What is r? This is confusing.
Just right:
def calculate_distance(speed, time): # Distance = speed x time return speed * timeClean. Clear. Helpful.
Good Comment vs Bad Comment in Python
Let me show the difference one more time.
Bad comment:
# increment i by 1i = i + 1We can see that. We do not need a comment.
Good comment:
# Reset counter after each batchi = 0Now we know why the counter is being reset. That is the difference. Good comments explain why. Bad comments explain what.
Next, we will learn about Python Keywords so we can continue step by step.