This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: The PEP 8 style guide

PEP-8 Style Guide

face Josiah Wang

Python codes are designed to be simple and readable.

In you are interested in Python’s design philosophy, you can read the “The Zen of Python” by doing this:

>>> import this

We have mentioned the official Python Enhancement Proposal (PEP) 8 several times so far. PEP 8 provides guidelines on how to write (good) Python code in order to improve readability and consistency.

There is also a more stylised version of the same PEP 8 that you might find easier to read.

Remember that most of these recommendations are guidelines, not hard rules. Use your judgement if you feel that there is a good reason to ignore the guidelines. Read the section on A Foolish Consistency is the Hobgoblin of little minds about when guidelines should be followed, and when should they be ignored.

We have already talked about some of these guidelines in the last few lessons - naming variables, indentation, and writing comments. Hopefully you still remember them!

We will look at a few more in this chapter.