livepython.net
CHAPTER 06Comments
6.1  COMMENTS

Python Comments: Single-Line, Inline and Multi-Line

What Is a Comment?

A comment is a note in your code for people, not for Python. It starts with a hash sign #. Python ignores everything from the # to the end of the line:

PYTHONedit me
# This program greets the user
print("Hello, Sunny!")
OUTPUT
Hello, Sunny!

The first line doesn't print anything. Python skips it completely.

Think of comments as sticky notes on your code. They explain what's going on to whoever reads it next, and that's often you, a few weeks later, who has forgotten the details.

Why write comments?

  • To explain. Code says what happens; a comment can say why.
  • To remember. Notes help you understand your own old programs.
  • To share. Other people can read your code faster.
  • To test. You can switch a line off by turning it into a comment. You'll learn this in the last topic of this chapter.

Comments can say anything

Python doesn't check spelling, grammar or language inside a comment:

PYTHONedit me
# Store the price in rupees
price = 50
# Hindi, English, emojis: all fine here 🙂
print(price)
OUTPUT
50

Mind the

Without the #, Python tries to run your note as code, and stops:

PYTHONedit me
This is a note
print("Hi")
OUTPUT
SyntaxError: invalid syntax

A comment starts with # and is a note for people. Python skips it.

your turnPredict the output

What will this print?

# print("Hello") print("Bye")
Your turn! What will this code show on the screen?
your turnFix the code

Make the note a comment, so the code runs

It should print: Hello!

Greet the user
print("Hello!")
This code does not work yet. Fix it, then press Run.

Inline Comments

A comment can also go at the end of a line of code. This is called an inline comment:

PYTHONedit me
price = 50  # in rupees
tax = 5  # 10 per cent of the price
print(price + tax)  # total to pay
OUTPUT
55

Python runs the code before the #, and ignores everything after it.

Think of an inline comment as a small label stuck right next to one item on a shelf.

The style rule

The official Python style guide (PEP 8) says:

  • leave two spaces before the #,
  • and one space after it.
Hard to readEasy to read
price = 50#rupeesprice = 50 # rupees

Keep them short

Inline comments are for quick facts: a unit, a reason, a warning. If you need a longer explanation, put a full comment on its own line above the code instead:

PYTHONedit me
# The shop gives a 10 per cent discount on all orders above 500,
# so we work out the discount before adding delivery.
total = 600
discount = total * 0.1
print(total - discount)
OUTPUT
540.0

Common beginner doubt: "Does an inline comment change the result?" Never. Everything after # is ignored, so the code works exactly as if the comment weren't there.

An inline comment sits after code: two spaces, #, one space, a short note.

your turnFill the gap

Make it a comment, so the code still runs

speed = 60 ? km per hour print(speed)

It should print: 60

Which piece makes this code work?

Comments Over Several Lines

Python doesn't have a special symbol for multi-line comments. The usual way is simple: start every line with #:

PYTHONedit me
# This program prints a receipt.
# It was written for Sunny's Shop.
# Prices are in rupees.
print("Receipt")
OUTPUT
Receipt

Most code editors can do this for you: select several lines and press Ctrl + / (or Cmd + / on a Mac).

Triple-quoted text as a note

You may also see notes written in triple quotes, like the multi-line strings from chapter 2:

PYTHONedit me
"""
This program prints a receipt.
Prices are in rupees.
"""
print("Receipt")
OUTPUT
Receipt

Strictly speaking, this isn't a comment: it's a string that Python creates and then throws away, because nothing uses it. But at the top of a file (or, later, of a function), this kind of string has a special name: a docstring, short for documentation string. You'll write docstrings in the Functions chapter.

Which one to use?

ForUse
notes inside your code# on each line
a description at the top of a file or a functiona docstring """…"""

Think of # notes as sticky notes, and a docstring as the title page of a book.

For notes over several lines, start each line with #.

your turnPick the right idea

How do you write a comment over three lines in Python?

Think about it, then pick one.

A # Inside a String Is Not a Comment

A # only starts a comment when it's outside quotes. Inside a string, it's just a character:

PYTHONedit me
print("I love #Python")  # a hashtag in the text
OUTPUT
I love #Python

The first # is inside the quotes, so it's part of the text and gets printed. The second # is outside, so it starts a comment.

How Python decides

Python reads each line from left to right. While it's inside quotes, everything is text. Once the string ends, a # starts a comment.

PYTHONedit me
tag = "#1 player"
print(tag)
print(len(tag))
OUTPUT
#1 player
9

The # is one of the 9 characters in the string.

Comments don't work inside f-strings either

PYTHONedit me
score = 90
print(f"Score: {score} # well done")
OUTPUT
Score: 90 # well done

The # well done is inside the quotes, so it's printed as text. If you want a comment, put it after the closing quote and bracket.

Think of quotes as a glass box: whatever is inside the box is shown exactly as it is, including # signs.

Inside quotes, # is just text. Outside quotes, # starts a comment.

your turnPredict the output

What will this print?

print("Use # for comments") # a tip
Your turn! What will this code show on the screen?
your turnPredict the output

What will this print?

print(10 // 3)
Your turn! What will this code show on the screen?

Practice: Comments

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 44.Python Program to Print a Hashtag Inside a StringEasySolve
  2. 45.Fix a Python Program with a Comment Missing Its #EasySolve