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.
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
- # 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
28.27
A # inside a string is just text
Ranked #1
More examples
Comments that explain intent
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:
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
Prefer
Explain the reason, not the obvious action.
Out-of-date comments
Avoid
Prefer
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
- 1Add one helpful comment to a program you wrote earlier that explains a decision rather than an action.
- 2Rewrite 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?