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

Whitespace around operators

face Josiah Wang

PEP 8 recommends that you do not align assignment operators.

# Recommended:
x = 1
y = 2
long_variable = 3

# Not recommended:
x             = 1
y             = 2
long_variable = 3

PEP 8 recommends you include spaces around operators for better readability. But also consider NOT adding spaces to group operations in longer expressions (again, easier to read). Use your judgment here. The examples below are taken directly from PEP 8.

# Recommended:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)

# Not recommended:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)