Stop 01 · Python FoundationsTopics · 6/8

Comments & PEP 8 Style

Part of the Python Roadmap

In short

Code is read far more often than it is written, mostly by future you. Good comments explain why, and PEP 8 makes every Python file look familiar so anyone on the team can jump in.

Comments (lines starting with #) explain why code exists, and PEP 8 is the official style guide for Python, covering naming, 4-space indentation, line length and spacing so code looks consistent across teams.

The jaadu adds a tiny pencil note in the margin of a neatly laid-out recipe book on a kitchen table next to a bowl of dough.

Think of it like this

Notes in the margin of a recipe book

Your nani's recipe book has the steps printed neatly, and in the margin she has written "use less salt, Papa doesn't like it". The steps say what to do; the margin note says why. PEP 8 is the printing standard that keeps every page laid out the same way, and comments are the margin notes.

Watch it happen

Same logic, two styles

Messy

  • def CalcTotal( Price,qty ):
  • Two-space indent, no spaces around =
  • Names like t and x tell you nothing
  • Comment repeats the code: # multiply

PEP 8

  • def calculate_total(price, quantity):
  • Four-space indent, spaces around operators
  • Descriptive snake_case names
  • Comment explains why, not what

Both run the same, but the PEP 8 version can be understood in seconds by anyone.

The big ideas

What you need to know

# starts a comment

Everything after # on a line is ignored by Python. Put comments on their own line above the code, or two spaces after it on the same line.

Explain why, not what

# add 18% GST because prices in the sheet are pre-tax helps. # multiply by 1.18 just repeats the code and adds noise.

Naming rules

snake_case for variables and functions (total_amount), PascalCase for classes (StudentRecord), UPPER_CASE for constants (MAX_SEATS).

Layout rules

4 spaces per indent level, spaces around = and +, about 79 characters per line, and two blank lines between top-level functions.

See it in code

Walk through the code

python
1# Bad: works, but hard to read2def CalcTotal( Price,qty ):3  t=Price*qty  # multiply4  return t5 6 7# Good: follows PEP 88GST_RATE = 0.189 10 11def calculate_total(price, quantity):12    # Prices in our sheet exclude tax, so add GST here.13    subtotal = price * quantity14    return subtotal + subtotal * GST_RATE15 16 17print(calculate_total(250, 3))

1/4PascalCase function name, odd spacing, a two-space indent, a one-letter name and a comment that just repeats the code.

Prints 885.0. Both functions run, but only the second is pleasant to read and change.

Save yourself the pain

Common mistakes

Oops

Commenting every single line with what it does.

Do this instead

Comment the tricky parts and the reasons. If code needs a comment to explain what it does, try a clearer name first.

Oops

Mixing tabs and spaces for indentation.

Do this instead

Use 4 spaces everywhere. VS Code inserts spaces when you press Tab in .py files, and Python 3 rejects inconsistent mixing.

Oops

Leaving old code commented out "just in case".

Do this instead

Delete it. Version control (Git) remembers old versions, so dead code only confuses readers.

Your mission

Clean up a messy script

~20 min

Quick check

Which name follows PEP 8 for a regular variable?

Go deeper

  1. 1Use snake_case for variables and functions and PascalCase for class names.
  2. 2Write comments that explain why, not what the code obviously does.
  3. 3Skim the PEP 8 page once and note the rules you break most often.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge