LambdaLabTM
Computer Science · Class 11 · Variables & User Input
CommentsGood habits⏱️ 5 min read

Comments in Python

Not every line in a program is for the computer. Some lines are notes you leave for a human being — for your teacher, for a friend, or for yourself in three months when you have forgotten how your own program works. Those notes are called comments.

1The # symbol

A comment starts with a hash sign, #. When Python sees a #, it stops reading that line and jumps to the next one. Whatever you wrote after the # is skipped completely.

You can put a comment on a line of its own:

comment_line.py
# this line is a note for humans
print('Hello')
Output
Hello

Or at the end of a line of code. The code before the # runs as usual, and only the part after it is ignored:

comment_end.py
price = 250    # price of one book
print(price)
Output
250
Key Takeaway
# starts a comment. Everything after it on that line is ignored by Python. The next line is read normally again.

2Python really does ignore them

Press the button below. It shows you the same program twice: once as you wrote it, and once as Python reads it. Watch the output while the comments disappear.

What you wrote
 # program to find the total marks of a student
maths = 37
science = 45 # out of 50
english = 41
 
total = maths + science + english # add all three
print('Total marks =', total)
Output — exactly the same either way
Total marks = 123

The green text is for you, not for Python. Python throws it away before it runs a single line — so it can never change what your program does.

The output did not change, because comments are not instructions. Python removes them before the program runs. A comment can never make your program do something — and it can never break it either.

3So why write them at all?

Because programs are read by people far more often than they are written. A comment lets you say the things the code itself cannot say — what the program is for, why you chose a number, what a tricky line is doing.

They explain the plan

A line at the top saying what the program does saves the reader from working it out.

They organise the code

A short comment above each part of a long program splits it into steps you can find.

They explain the why

The code shows what happens. A comment can say why — why 100, why this rule.

Here is the simple interest program from the last lesson, with comments added. Nothing about how it runs has changed. Everything about how easily it can be read has:

simple_interest.py
# program to find the simple interest and the total amount

p = 10000      # principal amount
r = 7.5        # rate of interest per year
t = 3          # time in years

# formula: simple interest = (p * r * t) / 100
si = (p * r * t) / 100
amount = p + si

print('Simple interest =', si)
print('Amount to be paid =', amount)
Output
Simple interest = 2250.0
Amount to be paid = 12250.0
A comment is not a translation
Do not write total = a + b  # add a and b. Anyone can see that. Write the thing the code cannot tell them:  # marks of all three subjects added together. A good comment explains why, not what.

4One catch: a # inside quotes

A # only starts a comment when it is out in the open. Inside quotes it is just an ordinary character, because everything inside quotes is text:

hash_in_text.py
print('Roll number #14')    # this part IS a comment
Output
Roll number #14
Python has only one kind of comment
There is no multi-line comment in Python — only the single-line #. To comment out five lines, put a # in front of each of the five. (Some books show '''…''' as a “multi-line comment”. That is really just a string that Python builds and then throws away, not a comment.)

5The everyday trick: switching a line off

Programmers use # for one more thing all day long. If a line is causing trouble, you do not have to delete it — put a # in front of it and Python will skip it. Take the # away later and the line comes back to life.

Try it below. Run the program, then put a # in front of the print(b) line and run it again. Then take it away again.

switch_off.py

6Recap

Key Takeaway
A comment starts with #. Python ignores everything after the # on that line, so comments never change what a program does. They are written for humans: they explain the program, organise it, and make it readable. Python has only one kind of comment — the single-line one.
Quick Check

What does Python do with everything written after a # on a line?

Quick Check

What does print('Roll number #14') display?

Quick Check

Why do programmers write comments?