Python Comments


Python comments are one of the simplest parts of the language, but they play a big role in writing clear and meaningful code. Comments help you describe what your code is doing, why you wrote something a certain way and what someone should know before reading or modifying the code. Even though comments don’t affect how the program runs, they make a huge difference in how readable and maintainable your code becomes. In this chapter, we’ll look at why comments matter, how to write them and how they fit naturally into Python’s clean coding style.

What Are Comments in Python

Comments are notes you add inside your code that Python ignores when it’s running the program. They’re there only for humans, not for the computer. When your program grows bigger or when more people work on the same project, these notes help everyone understand the purpose behind different parts of the code. Python treats comments as pure text, so they don't need any special structure aside from the comment symbol.

The Purpose of Comments

Comments improve clarity. When someone reads your code later, the logic might not be obvious. A good comment explains why something is being done or what a particular section is supposed to accomplish. Sometimes you write comments for yourself too. When you return to the same file after weeks or months, a small note can save you time and confusion.

Comments are also useful when you're learning. Explaining each step in your own words reinforces your understanding of the logic.

Single-Line Comments

The most common type of comment in Python is the single-line comment. It begins with the # symbol. Everything written after this symbol on the same line is considered a comment.

Example:

# This calculates the final amount
amount = price + tax

You can also place a comment at the end of a line:

total = 100  # Initial total before discount

This keeps the code and explanation together without creating clutter.

Inline Comments

Inline comments are common when you want to explain one specific line. They should be short and relevant. Long inline comments look messy, so keep them concise.

Example:

speed = distance / time  # simple speed formula

These comments help when you're writing small calculations or returning values that may not be obvious at first glance.

Block Comments

Sometimes you need more than one line to explain a concept. In such cases, you can use block comments. These are simply multiple single-line comments stacked together.

Example:

# This section handles payment calculations.
# It adds tax, applies discount, and updates the final amount.

Block comments are great for describing whole sections of code instead of individual lines.

Comments Inside Code Blocks

Comments can live inside loops, conditions and functions. These help explain the flow of your logic. For example, inside a loop, you might describe how values change with each iteration.

Example:

for number in range(5):
    # Print the current number in the loop
    print(number)

Comments like these help readers follow the logic without needing to figure out the intention on their own.

When Not to Use Comments

While comments are important, they should not replace clean code. If your code is readable by itself, it needs fewer comments. Overusing comments can make your file look crowded and harder to navigate. Avoid comments that simply repeat what the code already says.

Bad example:

x = x + 1  # adds one to x

This adds no value because the line is already clear. Comments should help explain intention, not restate the obvious.

Writing Meaningful Comments

Good comments are clear, direct and useful. They should sound like natural explanations instead of long paragraphs. Use comments to explain why a decision was taken. For example, if you write a condition that checks an unusual value, explain why that value matters.

Example:

if score == -1:
    # -1 means the user skipped the question
    handle_skip()

This gives context that wouldn’t be obvious just by looking at the code.

Comments and Debugging

Comments are also handy when you’re debugging your program. You can temporarily disable a line of code by commenting it out. This helps test different parts of the program without removing the line permanently.

Example:

# print(data)   # Used earlier to verify data

This line stays in your file for reference while remaining inactive.

Multiline Strings as Temporary Comments

Python doesn’t have an official multiline comment symbol, but some developers use triple-quoted strings (""" """) to create comment-like blocks. These are treated as strings, not comments, unless they’re not assigned to a variable.

Example:

"""
This part needs improvement later.
We will replace this logic with a new approach.
"""

This method is useful for notes during development but should be avoided for production code.

Comments and Code Quality

Well-written comments improve your code quality. Projects with consistent comments are easier to update, extend and maintain. Teams especially benefit because comments provide shared understanding. When you build the habit of writing helpful comments early, your future projects become cleaner and more organised.

Summary of the Tutorial

Comments help you explain your Python code clearly. They don’t change how your program runs but they make your logic easier to follow. Using the # symbol, you can write single-line, inline and block comments to describe code sections, intentions and decisions. Good comments help you avoid confusion, improve collaboration and maintain clean code over time.


Practice Questions

  1. What is the purpose of comments in Python, and why are they important even though they don’t affect program execution?
  2. What symbol is used for single-line comments in Python?
  3. How do block comments differ from single-line comments, and when should you use them?
  4. Why is it recommended to avoid comments that simply restate what the code is doing?
  5. How can comments be helpful during debugging or testing Python programs?
  6. Write a single-line comment explaining a variable assignment.
  7. Add an inline comment to a print statement describing its purpose.
  8. Create a block comment explaining the steps in a small loop.
  9. Show how to temporarily disable a line of code using a comment.
  10. Use a triple-quoted string to add a temporary note about improving a section of code.

Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top