A Python comment starts with # outside a string and runs to the end of that physical line. Python generally ignores comments when it parses and executes your program, so their job is to preserve useful context for the person reading the code later—not to repeat what the code already makes obvious.
How do I comment in Python?
Put # before a note on its own line, or after a statement for a short inline note:
As an Amazon Associate I earn from qualifying purchases.
# A standalone comment
count = 3 # An end-of-line comment
message = "Use # in this displayed example" # The hash in the string is not a comment
In ordinary code, the comment continues to the end of the physical line. A # inside a quoted string is part of that string, not a comment. The Python tutorial shows these same basic forms. Python tutorial: An Informal Introduction to Python
What does # do in Python?
Outside a string literal, # marks the start of a comment. Comments are for people; Python does not interpret them as program instructions. As the Python 3.14.8 language reference puts it, “Comments are ignored by the syntax.” Python language reference: Comments
#1 Best Overall
There is one specialized source-file exception: a comment in the first or second line that matches the encoding-declaration form, such as # coding: utf-8, can tell Python how to decode the file. UTF-8 is the default when no encoding declaration is found. This matters mainly when working with source files that use a different encoding; it does not turn ordinary comments into executable code. Python language reference: Comments
When should you add a comment?
Add a comment when it gives a future reader information that the code alone does not reveal: a non-obvious reason, assumption, constraint, or design choice. PEP 8 advises keeping comments clear and current, using inline comments sparingly, and avoiding notes that merely restate an operation. PEP 8: Comments
Rank #2
Prefer context over narration
count += 1 # Add one to count
The comment above repeats the statement. If the increment has a meaningful reason that cannot be inferred from the surrounding code, explain that instead:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutecount += 1 # Keep the zero-based offset aligned with the file header
That explanation is useful only if it accurately describes the code’s real purpose. A comment should not invent a rationale just to make a line look documented.
Choose standalone or inline placement
- Standalone comments work well for context that applies to a block or needs room for a complete explanation.
- Inline comments sit after a statement and are best kept brief; use one only when the note adds something the statement cannot say for itself.
PEP 8 recommends complete sentences for block comments and says inline comments should be used sparingly. These are style recommendations, not syntax rules. PEP 8: Comments
What’s the difference between a comment and a docstring?
A # comment is an informal note placed near code. A docstring is a documentation string conventionally attached to a module, class, or function or method. PEP 257 recommends documenting applicable behavior, arguments, return values, side effects, exceptions, and restrictions, as relevant to the documented object. PEP 257: Docstring Conventions
Use comments for local implementation context; use docstrings to document the purpose and use of a module or public object. Triple-quoted strings are not a general-purpose replacement for comments: follow docstring conventions when a string is meant to document a module, class, or function.
Recommended Free Tools
How do you keep comments useful when you revisit code?
When changing code, check nearby comments against the new behavior. An outdated note can send you in the wrong direction: PEP 8 warns, “Comments that contradict the code are worse than no comments,” and urges developers to update comments when code changes. PEP 8: Comments
Best Value
- Ask what a reader will not be able to infer from the code itself.
- Write the reason or constraint, not a play-by-play of the next operation.
- Remove a comment if it has become redundant or no longer matches the implementation.
Studies of comments in Java and Python offer dataset-specific findings, not a universal measure of how much comments improve comprehension. A 2019 study examined 2,000 GitHub projects and reported 60% precision and 80% recall for its classifier of explanatory comments; those are classifier metrics, not success rates for comments generally. Shinyama, Arahori, and Gondow (2019) A 2021 study reported that 80% of class comments in the studied Java and Python projects followed writing-style and content conventions, while 30% violated structure conventions. Those findings describe the projects and methods studied, not all Python code. Rani et al. (2021)
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




