Python Comments

When learning Python, one of the first and most essential concepts to understand is comments. Comments are lines in your code that are not executed—they’re intended for humans to read, not machines. Python comments help you document your code, explain logic, and make collaboration easier.

In this article, we'll explore what Python comments are, why they're important, how to use them, and some best practices for writing clear and effective comments.

What is a Comment in Python?

A comment in Python is a line that starts with the hash symbol #. Anything following # on that line is ignored by the Python interpreter.

Example:

# This is a comment
print("Hello, world!")  # This is also a comment

In the example above, the text after the # is a comment and does not affect the output.

Why Use Comments in Python?

Comments serve several critical purposes:

  • Improve Code Readability
    Comments help you and others understand what your code is doing, especially when you revisit it after weeks or months.
  • Explain Complex Logic
    If a function or block of code involves tricky logic or algorithms, a comment can clarify the steps.
  • Assist in Collaboration
    When working in teams, comments act as documentation for your teammates.
  • Debugging Tool
    Temporarily commenting out lines of code can help isolate bugs during development.

Types of Comments in Python

Python supports two main types of comments:

1. Single-Line Comments

Single-line comments begin with # and extend to the end of the line.

# This is a single-line comment
x = 5  # Assigning 5 to x

2. Multi-Line Comments

Python does not have a specific syntax for multi-line comments like other languages (e.g., /* */ in C or Java). However, you can use multiple single-line comments:

# This is a multi-line comment
# that spans across several
# lines in the code.

Alternatively, some developers use triple-quoted strings (''' or """) as a form of multi-line comment. However, this is technically a string, not a comment, and it's best reserved for docstrings (more on that below).

'''
This is a block of text
that will not execute, but it's
technically a string, not a comment.
'''

⚠️ Note: Use triple quotes cautiously, as Python will treat this as a string object unless it’s placed in a context where it's not assigned to anything.

Python Docstrings vs Comments

Docstrings are a special kind of string used to document Python functions, classes, and modules. They are placed immediately after the definition and are enclosed in triple quotes.

def greet():
    """This function prints a greeting message."""
    print("Hello!")

While comments are ignored entirely by the interpreter, docstrings can be accessed using tools like help() and are part of the object’s metadata.

Best Practices for Writing Python Comments

Follow these best practices to write helpful and maintainable comments:

  • Be concise but clear – Avoid unnecessary words while still explaining the purpose.
  • Keep comments up-to-date – Remove or revise comments if the code changes.
  • Avoid obvious comments – Don’t state the obvious. Only comment on why something is done, not just what.

 Good Comment:

# Calculate interest for monthly payments
interest = balance * rate / 12

Bad Comment:

# Multiply balance by rate and divide by 12
interest = balance * rate / 12

Conclusion

Python comments are a simple but powerful tool that enhances code readability, maintainability, and collaboration. Whether you're a beginner or an experienced developer, writing clear and purposeful comments should be a regular part of your coding practice.

By understanding and using Python comments effectively, you'll write cleaner, more understandable code—and that benefits everyone.