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

Python Comments

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)

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.