Lesson 6 of 10
Comments
A comment is a note in your code that Python ignores when running — it's there for humans.
In Python, anything after a # on a line is a comment.
# This calculates a 20% tip
bill = 50
tip = bill * 0.2
print(tip) # prints the tip amount
Comments are useful for explaining why something is done a certain way — especially when it's not obvious from the code itself. Avoid comments that just restate what the code already says clearly.
# Good: explains a non-obvious reason
retries = 3 # API is flaky, so we allow a few retries
# Not useful: just repeats the code
retries = 3 # set retries to 3
PEP 8 — Python's style guide
PEP 8 is Python's official style guide. It's not enforced by the interpreter — code that breaks it still runs — but following it makes your code far easier for other people (and future you) to read. A few of the most common conventions:
- Use
snake_casefor variable and function names (total_score, notTotalScore). - Use 4 spaces per indentation level (never tabs).
- Put spaces around operators:
x = 1 + 2, notx=1+2. - Keep lines under about 79–99 characters where reasonable.
- Use blank lines to separate logical sections of code.
# Following PEP 8
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print(full_name)
You don't need to memorize the whole guide today — just build the habit of writing clean, consistently-spaced code from the start. It becomes automatic quickly.
Try it yourself
Add a comment explaining what this code does, then run it.
Checkpoint
#starts a comment; Python ignores everything after it on that line.- Good comments explain why, not what.
- PEP 8 is Python's style guide —
snake_case, 4-space indents, spaced-out operators.
Questions about this lesson
Log in to ask a question.