What Is PEP 8? The Python Style Guide Explained
What Is PEP 8?
PEP 8 is the official style guide for Python: a list of habits for how your code should look. Where to put spaces, how to name things, how long a line may be. It doesn't change what your program does. It changes how easy it is to read.
PEP stands for "Python Enhancement Proposal", a document that suggests something for Python. PEP 8 was written in 2001 by Guido van Rossum, the creator of Python, together with two other Python developers. Almost every Python programmer in the world follows it.
Think of it like handwriting rules at school: write on the lines, leave a space between words, start a sentence with a capital letter. Your teacher can still read messy handwriting, but neat handwriting is read faster, and with fewer mistakes.
Same program, two looks
price=250;qty=3
total=price*qty
print("Total:",total)Total: 750
price = 250
qty = 3
total = price * qty
print("Total:", total)Total: 750
Both print the same thing. Python doesn't care which one you write. People do: the second one reads like a sentence.
What PEP 8 covers
| Area | An example rule | You'll learn it in |
|---|---|---|
| Names | variables in snake_case | Naming |
| Indentation | 4 spaces per level | Spacing |
| Spaces | one space on each side of = | Spacing |
| Line length | about 79 characters at most | Spacing |
| Layout | one statement per line | Clean code |
| Comments | start with # and explain why | Clean code |
Good news: you already follow some of these. You met snake_case in chapter 4, the 4-space indentation in chapter 8, and good comments in chapter 6. This chapter puts them all in one place, and adds the rest.
PEP 8 is Python's style guide: it changes how your code looks, not what it does.