Comments in Code

► Overview

Comments are notes in your code that explain what the code does. They are ignored by the computer when the program runs. Comments help you and others understand your code better.

► Real-World Analogy

Think of comments like sticky notes on a recipe:
» You add notes explaining why you use certain ingredients
» Notes remind you of important steps
» Others can understand your recipe modifications
» The oven doesn’t read the notes – only people do

► Key Concepts

✓ Single-line Comment: Used for brief explanations (#)
✓ Multi-line Comment: Used for longer descriptions (”’ or “””)
✓ Documentation: Comments that explain how to use code
✓ Readability: Comments make code easier to understand

■ Comment Examples

/code

This is a single-line comment

age = 25 # You can add comments after code

”’
This is a multi-line comment.
Use it for longer explanations.
It can span multiple lines.
”’

def calculate_total(price, tax):
# Calculate total with tax
return price + (price * tax)

► Best Practices

✓ Write comments that explain WHY, not WHAT
✓ Keep comments short and clear
✓ Update comments when you change code
✓ Don’t over-comment obvious code

► Remember

✓ Comments are for humans, not computers
✓ Good comments save time for future you
✓ Use comments to explain complex logic
✓ Remove commented-out code before finalizing

■ Practice Challenge

  1. Add comments to explain a simple calculator program
  2. Write a multi-line comment describing a function’s purpose
  3. Use comments to document your variable names