Last updated

Python Comments

Definition: A comment is text in your code that Python ignores when running. Comments exist only to explain the code to people reading it.

Example 1 — a single-line comment

Anything after a hash # on a line is ignored:

# This whole line is a comment
print("Hello")  # this part is ignored too

Example 2 — disabling code temporarily

Put a # in front of a line to stop it running, without deleting it. This is great for testing:

print("This runs")
# print("This is switched off")

Example 3 — multi-line notes

Python has no special multi-line comment, so either start each line with #, or use a triple-quoted string as a block of notes:

"""
This is a block of notes
spread over several lines.
Python ignores it here.
"""
print("Done")

Why use comments?

  • Explain why the code does something
  • Leave reminders for your future self
  • Help teammates understand your work

💡 Good habit: explain the reason, not the obvious. "# loop over users" is fine; "# add 1 to i" wastes space.

Try it Yourself
Output

          
Ad · responsive