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.

Tiny Practice: Leave a Note

Add a comment explaining that the message welcomes a new learner.

Starter template

print("Welcome to Python!")
Line-by-line explanation
  1. This line displays a welcome message.
  2. There is no comment yet.
Hint
  1. Add a line above the code.
  2. Begin it with #.
  3. Explain the reason for the message.
Show Solution
# Welcome someone starting the course
print("Welcome to Python!")
Line-by-line explanation
  1. The first line is a human-readable note.
  2. The second line displays the message.
  3. Only Welcome to Python! appears as output.