← Back to Python Course | Chapter 1: Basics | Lesson 6 of 14

Python Comments

Comments are little notes you leave in your code for people to read. The computer ignores them, like a sticky note on a recipe that says why you added extra sugar.
Syntax
python
# single-line comment

x = value  # inline comment

"""
multi-line comment
(docstring-style)
"""

Single-Line Comments

A # marks the start of a single-line comment; the interpreter ignores everything from that character to the end of the line. They're the quickest way to leave yourself a short note without affecting how the code runs.

Example: Single-Line Comments

python
# This is a comment
print("Comments are ignored by the interpreter")

Multi-Line Comments

There's no dedicated multi-line comment syntax in Python — developers either stack several # lines together, or repurpose an unassigned triple-quoted string ("""...""") as a block comment, since a standalone string statement has no effect when executed.

Example: Multi-Line Comments

python
# This is line one
# This is line two

"""
This triple-quoted string
acts as a block comment
"""
print("done")

Documenting Code

Good comments explain *why* a piece of code exists — a tricky workaround, a business rule, a non-obvious edge case — rather than restating *what* the code already makes obvious.

That distinction is what makes comments genuinely useful to the next person reading the file, including future you.

Example: Documenting Code

python
# Using a set here because duplicate ids would break the billing report
unique_ids = set()
print(unique_ids)

Commenting Out Code

Wrapping a line or block in # prevents it from executing without deleting it, which is a common way to isolate which part of a script is causing a bug during debugging.

Most editors offer a keyboard shortcut to comment/uncomment a whole selection at once.

Example: Commenting Out Code

python
print("this runs")
# print("this is commented out and does not run")

Comment Best Practices

Comments that go stale are worse than no comment at all, since they actively mislead the next reader about what the code does. Keep them short, update them whenever the code they describe changes, and delete ones that no longer add information.

Example: Comment Best Practices

python
# Retry limit tuned after the 2024 outage postmortem
MAX_RETRIES = 5
print(MAX_RETRIES)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using // or /* ... */ for comments as in other languages; Python only uses # (a bare /* */ is a SyntaxError).
  2. Treating a triple-quoted string as a true comment: it is a string expression, and when placed first in a function it becomes the docstring.
  3. Writing comments that just repeat the code, like x = x + 1 # add one to x, instead of explaining why.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.