Python Comments

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 = 100
tax = 0.18
total = 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 item
tax = 0.18 # 18% tax rate
total = price + (price * tax) # Add tax to price
print(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 comment
print("Hello") # This is also a comment

See? 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 circle
radius = 5
area = 3.14 * radius * radius
print(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 circle

This 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
# lines

This 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 simplicity
area = 3.14 * radius * radius

To describe what a complex piece of code does:

# This loop finds the largest number in the list
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num

To leave notes for ourselves:

# TODO: Fix this later when I have time
result = 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 5

That comment adds nothing. We can already see that x = 5. It is obvious.

Good:

x = 5 # discount rate for premium members

Now 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 numbers
a = 10
b = 20
c = 30
# Calculate the sum
total = a + b + c
# Calculate the average
average = total / 3
# Show the result
print("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 x
x = x + 1
# Print x
print(x)
# Done

This is annoying. The code is already clear. The comments just add noise.

Too few comments:

def process(d, t):
r = d * t
return r

What is d? What is t? What is r? This is confusing.

Just right:

def calculate_distance(speed, time):
# Distance = speed x time
return speed * time

Clean. Clear. Helpful.

Good Comment vs Bad Comment in Python

Let me show the difference one more time.

Bad comment:

# increment i by 1
i = i + 1

We can see that. We do not need a comment.

Good comment:

# Reset counter after each batch
i = 0

Now 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.