Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Comments

How to use comments in Python and why they are useful.

As your programs are getting more complex, you may find it more difficult to keep track of what each part of your code does.

A way to help you remember what different parts of your code do is to include comments within the code.

Comments are text within code that are notes for the programmer or another person using the code. The computer ignores all comments when it runs the code.

In Python, you create a comment by putting a hash character # before some text. For example:

# this is a comment

You can add comments anywhere within your program.

Typically, comments are reminders about what particular lines or sections of code do. For new learners such as yourself, comments are also a useful tool for explaining what programs do step by step.

# ask the user to enter their name
name = input("what is your name?")

# print a message including the name
print("Hello " + name)

In Python, you can make a comment span multiple lines by putting a # at the start of each line.

# comments can span
# multiple lines
# if a hash character is placed
# at the start of each line

Add some comments to your bot program to describe what it does.

This article is from the free online

Programming 101: An Introduction to Python for Educators

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now