🎓 A1Learner
🔍

Python Comments

A comment is a note in code that Python ignores. The # symbol starts each comment.

A comment on its own line

The comment sits above the code it explains.

# This program says hello
print("Hello!")

A comment at line end

A short comment can sit after code on the same line.

print("Hello!")  # Say hello to the reader

Comment out code

The first print is turned off by #. Only the second print runs.

# print("This line stays quiet")
print("This line runs")

A docstring first look

A docstring is a longer note at the top of a file. Triple quotes hold it over a few lines.

"""
A tiny demo program.
It prints one line.
"""
print("Docstring demo")

Example

# Course demo: show a friendly message
# The next line prints the main message
print("Comments help readers.")  # Keep notes short
# print("This line is turned off.")
print("The program still runs.")

Key points

  • Comment: a note in code that Python ignores
  • Use # to start a comment in Python
  • Use comments to explain why code does something
  • Commenting out: putting # before code so it does not run
  • Docstring: a text block at the start that describes the file
  • A docstring does not print by itself.

In this example, the first two lines are comments for the reader. The next line prints the main message. Its short end comment explains the style choice. The turned-off print line does not run. The last line prints a closing message.

Note

A docstring is a real string, not a comment.

Python keeps a docstring.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which symbol starts a comment in Python?