Beginner 5 min · March 05, 2026

Python Comments — Why Deleting One Cost 4% Revenue

A deleted comment hid a tax calculation rule, causing 2 months of wrong discounts.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
Quick Answer
  • Comments start with # and are ignored by Python
  • Use block comments on their own line, inline comments sparingly
  • Python has no multi-line comment syntax — stack # lines
  • Triple-quoted strings are not comments; they become docstrings at function/module start
  • Only comment WHY, never WHAT — let clean code speak for itself

Every line of code you write today is a mystery you'll need to solve tomorrow. That sounds dramatic, but ask any developer who's opened a file they wrote six months ago — without comments, even your own code can look like a foreign language. Comments are the single cheapest investment you can make to keep your code readable, maintainable, and friendly to every person who touches it after you.

Python runs fast. It processes your instructions line by line, but it skips comments entirely — they exist purely for people, not machines. This solves a real problem: code tells the computer WHAT to do, but it rarely explains WHY a decision was made, what a tricky block is trying to achieve, or what a number like 0.0875 actually represents (is that a tax rate? a discount? who knows). Comments fill that gap.

By the end of this article you'll know exactly how to write single-line comments, how to fake multi-line comments the Pythonic way, how to use docstrings to document functions properly, and — most importantly — when to write a comment versus when to just write cleaner code. You'll also see the mistakes that trip up beginners and the answers that impress interviewers.

Single-Line Comments — The Workhorse You'll Use Every Day

A single-line comment in Python starts with the hash symbol #. Everything after that # on the same line is ignored by Python. That's the whole rule. There's nothing to install, no special mode to activate — just type # and write your note.

You can put a comment on its own line above the code it describes, which is called a 'block comment'. Or you can put it at the end of a line of code, which is called an 'inline comment'. Both are valid, but block comments are usually easier to read because they don't crowd the code.

Here's the key habit to build early: comment the WHY, not the WHAT. If you write total_price = price + tax and then comment # adds price and tax, that comment adds zero value — anyone can see that. But if you comment # tax is added here because EU law requires it to appear in the final total, now you've explained something the code can't explain itself. That's what comments are for.

Multi-Line Comments — What Python Actually Does (and Doesn't Have)

Here's something that surprises beginners: Python has no official multi-line comment syntax. Languages like Java and JavaScript have / ... / block comments, but Python never added an equivalent. So how do Python developers write longer notes that span multiple lines?

The answer is simple — you just start each line with its own #. Stack as many lines as you need, each prefixed with a #. Python treats each one as a separate single-line comment, but visually they read as a block. This is the officially recommended approach in Python's own style guide (PEP 8).

You'll also see beginners use triple-quoted strings (''' or """) as a workaround for multi-line comments. A string that isn't assigned to a variable and isn't attached to a function is technically just a value Python evaluates and immediately throws away — so it behaves like a comment in practice. However, this is not a true comment, and there's one important exception: when a triple-quoted string appears as the very first statement inside a function, class, or module, it becomes a docstring — a documented description that tools and IDEs can actually read. More on that in the next section.

Docstrings — Comments That Your Code Can Actually Read

A docstring is a special triple-quoted string placed as the very first statement in a function, class, or module. It's Python's official way of documenting what a piece of code does — and unlike a regular comment, Python stores the docstring and makes it accessible at runtime via the __doc__ attribute and the built-in help() function.

Think of a regular comment as a sticky note on your desk — helpful to you, invisible to everyone else. A docstring is like a label on a product — it's built into the thing itself and anyone can read it just by inspecting the object.

Docstrings follow a convention: the first line is a short, one-sentence summary of what the function does. If you need more detail, add a blank line, then a longer description, then document the parameters and return value. You don't have to memorise a rigid format right now — just know that the first line matters most and should be a clear, complete sentence.

For beginners, the most important takeaway is this: every function you write should have a docstring. It takes ten seconds, and it pays back tenfold.

Best Practices — When to Comment and When to Clean Code

Great developers don't write more comments — they write code that needs fewer comments. The best comment is a well-named variable, a clear function, or a small, focused method. But even the cleanest code can't express business context, historical decisions, or reasoning behind a non-obvious fix.

Here's the practical rule I use on every team: write a comment when the answer to 'Why is this here?' isn't obvious from reading the code alone. If you're explaining a workaround for a third-party bug, add a comment. If you're applying a formula that looks wrong but is correct for the business, add a comment. If you're adding a sleep() to avoid a race condition, for heaven's sake comment that — or it'll be removed in the next 'cleanup' PR.

Also: treat comments like code. They rot. A stale comment is worse than no comment because it actively misleads. During code review, ask the author if each comment is still accurate. Delete comments that describe what the code already says. Update comments when the logic changes.

Style and Conventions — PEP 8 and Team Standards

PEP 8, Python's official style guide, has clear rules for comments. Follow them and your team will read each other's code faster:

  • Comments should be complete sentences. Start with a capital letter, end with a period.
  • Use a space after #. It's # comment not #comment.
  • Block comments apply to the following code. Indent them to the same level.
  • Inline comments use at least two spaces between the code and the #.
  • Docstrings use triple double quotes """ — triple single quotes ''' are acceptable but less common.

Most importantly: keep comments relevant. A comment that describes how code works is noise when the code is clear. A comment that describes why the code exists is gold.

Teams that enforce these standards through linters (flake8, pylint) catch comment style violations automatically. It's a small win, but it keeps the codebase consistent.

Comment Types at a Glance
Feature / AspectSingle-Line Comment (#)Triple-Quoted String (""")Docstring (""" at module/func start)
Syntax# your note here""" your note here """""" your note here """
Spans multiple lines?Only by stacking multiple # linesYes, nativelyYes, natively
Ignored by Python?Yes — completely ignoredNo — evaluated as a string expressionNo — stored as __doc__ attribute
Readable at runtime?NoNo (unless assigned to a variable)Yes — via help() or __doc__
Recommended for?Inline and block notesNever for comments; use # insteadDocumenting functions, classes, modules
PEP 8 positionYes, for all non-obvious logicDiscouraged as commentsRequired for all public functions/classes

Key Takeaways

  • Every comment you write is for a human, not Python — the interpreter ignores every # comment completely and never executes it.
  • Python has no official multi-line comment syntax — stack multiple # lines for block comments; triple-quoted strings are a workaround, not a language feature.
  • A docstring is not just a comment — it's stored as the __doc__ attribute of a function or class and can be read at runtime by help(), IDEs, and documentation tools.
  • Comment the WHY, not the WHAT — if your comment just re-states what the code clearly does, delete it; comments earn their place by explaining intent, constraints, and non-obvious decisions.
  • Stale comments are worse than no comments — treat them as code that must be maintained, or remove them.
  • Consistent comment style enforced by linting reduces cognitive load and prevents debates in code reviews.

Common Mistakes to Avoid

  • Commenting WHAT instead of WHY
    Symptom: Code like `counter = counter + 1 # increment counter` adds zero value. Anyone can read the code to see what it does. The comment is noise.
    Fix: Ask yourself: 'Would a new developer know WHY this is done without my comment?' If the code is self-explanatory, skip the comment. If there's a business rule, edge case, or non-obvious decision, explain that instead.
  • Using triple-quoted strings as comments inside function bodies
    Symptom: A triple-quoted string that isn't the first statement of a function is still a string expression that Python evaluates. It won't crash your program, but it wastes a tiny amount of processing, and more importantly it can confuse other developers who expect triple quotes to mean 'docstring'.
    Fix: Always use # for mid-function notes. Reserve triple quotes for the opening docstring only.
  • Commenting out dead code and leaving it forever
    Symptom: Beginners often comment out old code 'just in case I need it later' and commit it to the project. After weeks this creates files full of commented-out clutter that nobody dares delete.
    Fix: Use version control (like Git). If you might need old code later, that's what commit history is for — delete the code confidently and know you can retrieve it from Git any time.
  • Stale comments that contradict current logic
    Symptom: A comment says one thing but the code does something else. During code review, nobody noticed the comment wasn't updated when the logic changed.
    Fix: Treat comments as code: they must be reviewed and updated together. In code review, challenge each comment. Use a linter rule that flags comments older than a certain number of commits without modification.

Interview Questions on This Topic

  • QWhat is the difference between a comment and a docstring in Python, and how does Python treat each one at runtime?JuniorReveal
    A comment (starting with #) is completely ignored by Python — it doesn't exist at runtime. A docstring (a triple-quoted string as the first statement of a function, class, or module) is stored as the object's __doc__ attribute and can be accessed programmatically via help() or by reading __doc__ directly. Comments document individual lines; docstrings document the purpose and interface of an entire function or class.
  • QDoes Python have a built-in multi-line comment syntax? If not, what are the two approaches developers use, and which one does PEP 8 recommend?Mid-levelReveal
    Python has no built-in multi-line comment syntax (no equivalent of / /). The two approaches are: (1) stack multiple single-line comments, each starting with # (this is what PEP 8 recommends); (2) use a triple-quoted string that is not assigned to a variable, which Python evaluates and discards. PEP 8 explicitly recommends stacked # lines, as triple-quoted strings are not true comments and can accidentally become docstrings if placed at the start of a function or class.
  • QIf I write a triple-quoted string at the start of a function versus in the middle of a function, does Python treat them the same way? Explain what happens in each case.Mid-levelReveal
    No, they are treated differently. A triple-quoted string as the very first statement in a function becomes a docstring: Python stores it as the function's __doc__ attribute, and it can be read by IDEs and help(). A triple-quoted string placed in the middle of a function is just a string expression that Python evaluates (creating a string object) and immediately discards if not assigned or used. It is not stored anywhere and is not a true comment — it's a runtime operation (though tiny) and can confuse developers who expect it to be ignored.
  • QExplain the concept of 'comment debt' and how it affects production codebases.SeniorReveal
    Comment debt is the accumulating cost of stale, misleading, or unnecessary comments. Every comment adds a maintenance burden: when code changes, comments must be updated to match. Over time, outdated comments become misinformation that leads to bugs (e.g., a developer following an old comment's advice). The fix is to treat comments as code: review them in pull requests, delete or update them when logic changes, and prefer self-documenting code (clear naming, small functions) over excessive comments.

Frequently Asked Questions

Does Python have multi-line comments like /* */ in other languages?

No, Python has no dedicated multi-line comment syntax. The standard approach is to prefix each line with a # symbol and stack them vertically. Some developers use triple-quoted strings as a workaround, but these are technically string expressions, not true comments, so the recommended PEP 8 approach is always stacked # lines.

What is the difference between a comment and a docstring in Python?

A comment (starting with #) is completely ignored by Python at runtime and exists only for human readers. A docstring (a triple-quoted string as the first statement of a function, class, or module) is stored in memory as the object's __doc__ attribute and can be accessed programmatically using help() or by reading __doc__ directly. Comments document individual lines of logic; docstrings document the purpose and interface of an entire function or class.

Should I comment every single line of Python code I write?

No — over-commenting is just as harmful as under-commenting. Well-named variables and functions often make code self-documenting. A comment earns its place when it explains WHY a decision was made, documents a business rule, warns about a non-obvious side effect, or clarifies a complex algorithm. If the comment just re-states what the code obviously does, it's noise — remove it.

What is the best way to handle a comment that is no longer accurate?

Update it immediately, or delete it. A stale comment is more dangerous than no comment because it actively misleads. During code review, challenge every comment that seems outdated. Use git blame to see when the comment was last modified relative to the code it describes.

🔥

That's Python Basics. Mark it forged?

5 min read · try the examples if you haven't

Previous
Type Conversion in Python
8 / 17 · Python Basics
Next
Python Indentation and Syntax