0
0
PythonHow-ToBeginner · 3 min read

How to Write Single Line Comment in Python: Simple Guide

In Python, you write a single line comment by starting the line with the # symbol. Everything after # on that line is ignored by Python and treated as a comment.
📐

Syntax

To write a single line comment in Python, start the line with the # symbol. Anything written after # on the same line is considered a comment and will not run as code.

  • #: Marks the start of a comment.
  • Text after #: The comment content, ignored by Python.
python
# This is a single line comment in Python
💻

Example

This example shows how a single line comment can be used to explain code or temporarily disable a line.

python
print("Hello, world!")  # This prints a greeting message
# print("This line is commented out and won't run")
Output
Hello, world!
⚠️

Common Pitfalls

Some common mistakes when writing single line comments include:

  • Forgetting the # symbol and expecting the text to be ignored.
  • Placing comments inside strings, which does not make them comments.
  • Using multi-line comments incorrectly (single line comments only comment one line).
python
print("Hello")  # Correct comment

# Wrong: This is not a comment because it is inside a string
print("# This is not a comment")
Output
Hello # This is not a comment
📊

Quick Reference

Remember these quick tips for single line comments in Python:

  • Use # at the start of the comment.
  • Comments can be on their own line or after code.
  • Comments help explain code but do not affect program execution.

Key Takeaways

Start a single line comment with the # symbol in Python.
Everything after # on the same line is ignored by Python.
Comments can be used to explain code or disable lines temporarily.
Do not put comments inside strings expecting them to be ignored.
Single line comments only affect the line they are on.