Python PEP8 style guide Cheat Sheet
by jmds via cheatography.com/84942/cs/20012/
Naming conventions Comments
Never use l, O, or I single letter names as O = 2 # This may look Limit the line length of Use complete Make sure to update
these can be mistaken for 1 and 0, like you're trying to comments and sentences, starting comments if you
depending on typeface reassign 2 to zero docstrings to 72 with a capital change your code.
Function function, my_function characters. letter.
Variable x, var, my_variable
Block Comments
Class Model, MyClass
Indent block comments Start each line Separate
Method class_method, method
to the same level as the with a # followed paragraphs by a
Constant CONSTANT, MY_CON‐ code they describe. by a single space. line containing a
STANT, MY_LONG_C‐ single #.
ONSTANT
Module module.py, my_mod‐ Inline Comments
ule.py
Use inline comments sparingly.
Package package, mypackage
Write inline comments on the same line as the statement they refer
to.
Maximum Line Length and Line Breaking
Separate inline comments by two or more spaces from the
PEP 8 suggests lines should be limited to 79 statement.
characters. This is because it allows you to have
Start inline comments with a # and a single space, like block
multiple files open next to one another, while
comments.
also avoiding line wrapping.
Don’t use them to explain the obvious.
Python will assume line continuation if code is
contained within parentheses, brackets, or
When to Avoid Adding Whitespace
braces:
The most important place to avoid adding whitespace is at the end of
def function(arg_one, arg_two,
a line. This is known as trailing whitespace
arg_three, arg_four):
return arg_one Immediately inside parentheses, brackets, or braces:
If it is impossible to use implied continuation, Before a comma, semicolon, or colon:
then you can use backslashes to break lines Before the open parenthesis that starts the argument list of a function
instead: call:
from mypkg import example1, \ Before the open bracket that starts an index or slice:
example2, example3
Between a trailing comma and a closing parenthesis:
# Recommended
To align assignment operators:
total = (first_variable
immediately inside brackets, as well as before commas and colons.
+ second_variable
- third_variable)
Programming Recommendations
# Not Recommended
total = (first_variable + Don’t compare boolean values to Use the fact that empty
second_variable - True or False using the equivalence sequences are falsy in if
operator. statements.
third_variable)
Use is not rather than not ... is in if Don’t use if x: when you
Identation statements. mean if x is not None:
Use 4 consecutive spaces to indicate indentation. Use .startswith() and .endswith() instead of slicing.
Prefer spaces over tabs.
By jmds Not published yet. Sponsored by ApolloPad.com
cheatography.com/jmds/ Last updated 10th July, 2019. Everyone has a novel in them. Finish
Page 1 of 3. Yours!
https://apollopad.com
Python PEP8 style guide Cheat Sheet
by jmds via cheatography.com/84942/cs/20012/
Code Layout Where to Put the Closing Brace (cont)
Surround top-level functions and classes with two blank lines > ]
Surround method definitions inside classes with a single blank line. 2 - Line up the closing brace with the first character of the line that
starts the construct:
Use blank lines sparingly inside functions to show clear steps.
list_of_numbers = [
1, 2, 3,
Indentation Following Line Breaks
4, 5, 6,
There are two styles of indentation you can use. 7, 8, 9
The first of these is to align the indented block with the opening ]
delimiter:
An alternative style of indentation following a line break is a hanging Documentation Strings
indent. This is a typographical term meaning that every line but the Surround docstrings with three double quotes on either side, as in
first in a paragraph or statement is indented. """This is a docstring""".
Write them for all public modules, functions, classes, and methods.
Indentation Following Line Breaks 2
Put the """ that ends a multiline docstring on a line by itself:
def function(arg_one, arg_two,
For one-line docstrings, keep the """ on the same line:
arg_three, arg_four):
return arg_one
Whitespace Around Binary Operators
x = 5
Surround the following binary operators with a single space on either
if (x > 3 and
side:
x < 10):
Assignment operators (=, +=, -=, Comparisons (==, Booleans
print(x)
and so forth) !=, >, <. >=, <=) and (and, not,
x = 5
(is, is not, in, not in) or)
if (x > 3 and
x < 10): When = is used to assign a def function(default_paramet‐
default value to a function er=5):
# Both conditions satisfied
argument, do not surround it with
print(x)
spaces.
x = 5
if (x > 3 and y = x**2 + 5 z = (x+y) * (x-y)
x < 10): if x>5 and x%2==0:
print(x) # Treat the colon as the operator with lowest priority
# hanging indent list[x+1 : x+2]
var = function(
In an extended slice, both colons must be surrounded by the same
arg_one, arg_two,
amount of whitespace
arg_three, arg_four)
list[3:4:5] list[x+1 : x+2 : x+3]
The space is omitted if a slice list[x+1 : x+2 :]
Where to Put the Closing Brace
parameter is omitted
PEP 8 provides two options for the position of the
closing brace in implied line continuations:
1 - Line up the closing brace with the first non-
whitespace character of the previous line:
list_of_numbers = [
1, 2, 3,
4, 5, 6,
7, 8, 9
By jmds Not published yet. Sponsored by ApolloPad.com
cheatography.com/jmds/ Last updated 10th July, 2019. Everyone has a novel in them. Finish
Page 2 of 3. Yours!
https://apollopad.com
Python PEP8 style guide Cheat Sheet
by jmds via cheatography.com/84942/cs/20012/
When to Ignore PEP 8
If complying with PEP 8 would break compatibility with existing
software
If code surrounding what you’re working on is inconsistent with PEP
8
If code needs to remain compatible with older versions of Python
By jmds Not published yet. Sponsored by ApolloPad.com
cheatography.com/jmds/ Last updated 10th July, 2019. Everyone has a novel in them. Finish
Page 3 of 3. Yours!
https://apollopad.com