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:
# this line is a note for humans
print('Hello')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:
price = 250 # price of one book
print(price)250
# 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.
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.
A line at the top saying what the program does saves the reader from working it out.
A short comment above each part of a long program splits it into steps you can find.
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:
# 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)Simple interest = 2250.0 Amount to be paid = 12250.0
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:
print('Roll number #14') # this part IS a commentRoll number #14
#. 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.
6Recap
#. 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.What does Python do with everything written after a # on a line?
What does print('Roll number #14') display?
Why do programmers write comments?