Python · Stage 0 · Before You Code · Lesson 6 of 50

Comments

Add notes for humans that Python ignores when it runs the program.

A Python comment begins with a hash symbol #. Everything after it on that line is ignored by Python.

# Display a friendly message
print("Welcome!")
Line-by-line explanation
  1. The first line starts with #, so it is a comment. Python does not display it.
  2. The second line is code. It displays Welcome!.

Good comments explain why something exists or clarify an unusual choice. They should not merely repeat an obvious instruction.

print("Try again in 30 seconds")  # The server limits requests
Line-by-line explanation
  1. print(...) displays the message.
  2. The text after # explains why the user must wait.
  3. Python ignores the comment while running the line.