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
- The first line starts with
#, so it is a comment. Python does not display it. - 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 requestsLine-by-line explanation
print(...)displays the message.- The text after
#explains why the user must wait. - 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
- This line displays a welcome message.
- There is no comment yet.
Hint
- Add a line above the code.
- Begin it with
#. - Explain the reason for the message.
Show Solution
# Welcome someone starting the course
print("Welcome to Python!")Line-by-line explanation
- The first line is a human-readable note.
- The second line displays the message.
- Only
Welcome to Python!appears as output.