Skip to content

Module 2 · Python Syntax and Fundamentals — topic 5 of 14

Comments

Comments are notes in your code that Python ignores. Learn single-line and inline comments, when comments help, when they hurt, and how to use them to explain intent.

Beginner 10 min read·Lesson 19 of 306 in Python Programming

Learning objectives

  • Write single-line and inline comments
  • Use comments to explain why, not what
  • Temporarily disable code with comments

Definition

Comments are notes in your code that Python ignores. Learn single-line and inline comments, when comments help, when they hurt, and how to use them to explain intent.

Introduction

A comment is text in a program meant for people, not the computer. In Python a comment starts with # and continues to the end of the line. The interpreter skips it completely.

Why it matters

Programs are read far more often than they are written. A short comment explaining a decision, a formula’s source or a tricky edge case can save the next reader, often your future self, a long time.

Detailed explanation

Full-line comments start with # at the beginning of the line (after any indentation) and describe the code that follows.

Inline comments follow code on the same line, separated by at least two spaces: total = price * 1.18 # includes 18% GST.

Good comments explain why something is done, give context, or warn about surprises. Bad comments repeat what the code already says clearly.

A # inside a string is not a comment: print("#1 in class") prints the # character.

Commenting out a line (putting # in front of it) is a quick way to switch it off while testing, but delete such lines before sharing code.

Python has no special block-comment syntax. To comment several lines, put # at the start of each one; most editors do this for a selection with Ctrl+/. A triple-quoted string on its own is sometimes used as a block comment, but it is really an unused string, so prefer # for notes.

Syntax

Python
# full-line comment explaining the next block
value = 10 # inline comment
  • # starts a comment; everything after it on that line is ignored.
  • Inline comments need two spaces before #.
  • Put a space after # for readability.

Code examples

Comments don’t change the output

Python
# Calculate the area of a circle
radius = 3
area = 3.14159 * radius ** 2 # area = pi * r squared
print(round(area, 2))
Output
28.27

A # inside a string is just text

Python
print("Ranked #1") # this part is a comment
Output
Ranked #1

More examples

Comments that explain intent

Python
# Prices are stored in paise to avoid floating-point rounding errors.
price_paise = 49_999
quantity = 2
# Free shipping applies from Rs 999 (99,900 paise), as per store policy.
subtotal = price_paise * quantity
shipping = 0 if subtotal >= 99_900 else 4_000
total = subtotal + shipping
print(f"Total: Rs {total / 100:.2f}")
Output
Total: Rs 999.98

Step-by-step explanation

  • The first comment explains a design choice the code alone doesn’t reveal.
  • Underscores in 49_999 are only for readability; the value is 49999.
  • The second comment gives the business rule behind the 99,900 threshold.
  • subtotal is 99,998 paise, which is at least 99,900, so shipping is 0.
  • Dividing by 100 converts paise back to rupees for display: Rs 999.98.

Expected output

Running “Comments that explain intent” prints exactly:

TextOutput
Total: Rs 999.98

Practical applications

  • Teams use comments to link code to a requirement or bug report number.
  • Comments warn about workarounds, for example for a known bug in a library.
  • Code reviewers often ask for a comment wherever a number or rule looks surprising.

Best practices

  • Comment the why, the context and the surprises.
  • Keep comments short and update them with the code.
  • Prefer clear names over comments that explain unclear names.

Common mistakes

Comments that repeat the code

Avoid

count = count + 1 # add one to count

Prefer

count = count + 1 # include today in the streak

Explain the reason, not the obvious action.

Out-of-date comments

Avoid

# 5% discount
discount = 0.10

Prefer

# 10% festive discount
discount = 0.10

A comment that contradicts the code is worse than none. Update comments when code changes.

Key takeaways

  • # starts a comment that Python ignores.
  • Inline comments need two spaces before #.
  • Good comments explain intent; they don’t restate the code.

Try it yourself

  1. 1
    Add one helpful comment to a program you wrote earlier that explains a decision rather than an action.
  2. 2
    Rewrite this to need no comment: x = 86400 # seconds in a day
    Show hint

    Choose a descriptive variable name.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhich line is a comment in Python?
Practise this topic