← Intro to programming with Python

Comments: notes for humans

Leave notes in your code, and switch lines off while testing.

Start this lesson

A comment is a note written for people, not for the computer. Python ignores everything after a # sign on a line. Why bother? Code tells the computer WHAT to do; a comment tells a human WHY. Next month, you (or a friend) will read your program and be glad the notes are there. Comments are also handy for switching a line off without deleting it, which programmers call "commenting out".

# Show the price of a bus ticket
print("Ticket: $2.75")   # adult fare
# print("This line is switched off")

Only one line appears: Ticket: $2.75. The first and last lines are comments, and the note after the # on the middle line is ignored too.

Common mistake: writing a note without the #. Python then tries to run your English sentence as code and stops with a SyntaxError.

A # inside quotes is just a character, not a comment. print("You are #1") shows You are #1.

Good comments explain reasons ("prices include tax") rather than repeating the code ("print the price").