Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Clarity through comments

Why use comments in your code? In this article, James Robinson explains why they can be useful, and gives a few tips for how to use them.

Throughout this course, as well as developing your technical ability to program, we’ll also be modelling good practices and habits that we hope you’ll pick up.

One really important practice that we’ll be using throughout is the use of comments or annotations in our code to help communicate what the code is doing and how it’s working.

All programming languages support commenting in some form, and Python is no exception. In most text-based languages, there is a special character that is used to denote the start of a comment. In Python this is done with the # symbol. When it is used on a line, every following character on the same line is ignored by the computer, and can contain any text the programmer wants to insert. We might use comments in a number of different ways.

Some examples

  • We can use comments at the start of a program to describe its purpose:
# This program will display a welcome message and then find out the user's name

name = input("What is your name?")

  • Comments can be added to the end of each line:
print("Welcome!") #Displays a welcome message
name = input("What is your name?") #Prompts the user for their name and stores it in a variable
  • A programmer might use comments to explain the purpose of different sections of code:
# Welcome the user

# Collect their name, and greet them
name = input("What is your name?")
print("Hi {}".format(name))

#Collect the user's age
age = int(input("How old are you?"))

Multiline comments

Sometimes you may want to write longer comments over several lines. This is particularly useful when documenting your code. This can be achieved in a couple of ways:

  • Triple quoted strings can surround a piece of text over several lines, creating a multiline comment:
def someFuntion():

The someFunction function does X, Y, Z
First it does X
then Y
and finally Z

print("done X")
print("done Y")

  • The alternative is to precede each line with the # symbol:
def someFuntion():

# The someFunction function does X, Y, Z
# First it does X
# then Y
# and finally Z

print("done X")
print("done Y")


Use the comment section below to discuss the following questions:

  • How do comments benefit the author of the program?
  • How do the programmer’s comments benefit others?
  • How do you encourage your learners to include comments in their code?
This article is from the free online

Programming 102: Think Like a Computer Scientist

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education