\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{jmds} \pdfinfo{ /Title (python-pep8-style-guide.pdf) /Creator (Cheatography) /Author (jmds) /Subject (Python PEP8 style guide Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{5262A3} \definecolor{LightBackground}{HTML}{F4F5F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Python PEP8 style guide Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{jmds} via \textcolor{DarkBackground}{\uline{cheatography.com/84942/cs/20012/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}jmds \\ \uline{cheatography.com/jmds} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 10th July, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Naming conventions}} \tn % Row 0 \SetRowColor{LightBackground} Never use l, O, or I single letter names as these can be mistaken for 1 and 0, depending on typeface & O = 2 \# This may look like you're trying to reassign 2 to zero \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} {\bf{Function}} & function, my\_function \tn % Row Count 7 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Variable}} & x, var, my\_variable \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} {\bf{Class}} & Model, MyClass \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{Method}} & class\_method, method \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} {\bf{Constant}} & CONSTANT, MY\_CONSTANT, MY\_LONG\_CONSTANT \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{Module}} & module.py, my\_module.py \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} {\bf{Package}} & package, mypackage \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Maximum Line Length and Line Breaking}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{PEP 8 suggests lines should be limited to 79 characters. This is because it allows you to have multiple files open next to one another, while also avoiding line wrapping.}} \newline \newline \newline {\bf{Python will assume line continuation if code is contained within parentheses, brackets, or braces:}} \newline def function(arg\_one, arg\_two, \newline arg\_three, arg\_four): \newline return arg\_one \newline \newline \newline {\bf{If it is impossible to use implied continuation, then you can use backslashes to break lines instead:}} \newline from mypkg import example1, \textbackslash{} \newline example2, example3 \newline \newline {\bf{\# Recommended}} \newline total = (first\_variable \newline + second\_variable \newline - third\_variable) \newline \newline {\bf{\# Not Recommended}} \newline total = (first\_variable + \newline second\_variable - \newline third\_variable)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Identation}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Use 4 consecutive spaces to indicate indentation.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Prefer spaces over tabs.} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} Limit the line length of comments and docstrings to 72 characters. & Use complete sentences, starting with a capital letter. & Make sure to update comments if you change your code. \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Block Comments}} \tn % Row 0 \SetRowColor{LightBackground} Indent block comments to the same level as the code they describe. & Start each line with a \# followed by a single space. & Separate paragraphs by a line containing a single \#. \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Inline Comments}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Use inline comments sparingly.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Write inline comments on the same line as the statement they refer to.} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Separate inline comments by two or more spaces from the statement.} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Start inline comments with a \# and a single space, like block comments.} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Don't use them to explain the obvious.} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{When to Avoid Adding Whitespace}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The most important place to avoid adding whitespace is at the end of a line. This is known as trailing whitespace} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Immediately inside parentheses, brackets, or braces:} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Before a comma, semicolon, or colon:} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Before the open parenthesis that starts the argument list of a function call:} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Before the open bracket that starts an index or slice:} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Between a trailing comma and a closing parenthesis:} \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To align assignment operators:} \tn % Row Count 13 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{immediately inside brackets, as well as before commas and colons.} \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Programming Recommendations}} \tn % Row 0 \SetRowColor{LightBackground} Don't compare boolean values to True or False using the equivalence operator. & Use the fact that empty sequences are falsy in if statements. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Use is not rather than not ... is in if statements. & Don't use if x: when you mean if x is not None: \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Use .startswith() and .endswith() instead of slicing.}}} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Code Layout}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Surround top-level functions and classes with two blank lines}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Surround method definitions inside classes with a single blank line.}}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Use blank lines sparingly inside functions to show clear steps.}}} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Indentation Following Line Breaks}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{There are two styles of indentation you can use.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The first of these is to align the indented block with the opening delimiter:} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{An alternative style of indentation following a line break is a hanging indent. This is a typographical term meaning that every line but the first in a paragraph or statement is indented.} \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Indentation Following Line Breaks 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def function(arg\_one, arg\_two, \newline arg\_three, arg\_four): \newline return arg\_one \newline \newline \newline x = 5 \newline if (x \textgreater{} 3 and \newline x \textless{} 10): \newline print(x) \newline \newline x = 5 \newline if (x \textgreater{} 3 and \newline x \textless{} 10): \newline \# Both conditions satisfied \newline print(x) \newline \newline x = 5 \newline if (x \textgreater{} 3 and \newline x \textless{} 10): \newline print(x) \newline \newline \# hanging indent \newline var = function( \newline arg\_one, arg\_two, \newline arg\_three, arg\_four)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Where to Put the Closing Brace}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{PEP 8 provides two options for the position of the closing brace in implied line continuations:}} \newline \newline {\bf{1 - Line up the closing brace with the first non-whitespace character of the previous line:}} \newline \newline list\_of\_numbers = {[} \newline 1, 2, 3, \newline 4, 5, 6, \newline 7, 8, 9 \newline {]} \newline \newline {\bf{2 - Line up the closing brace with the first character of the line that starts the construct:}} \newline list\_of\_numbers = {[} \newline 1, 2, 3, \newline 4, 5, 6, \newline 7, 8, 9 \newline {]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Documentation Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Surround docstrings with three double quotes on either side, as in """This is a docstring""".} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Write them for all public modules, functions, classes, and methods.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Put the """ that ends a multiline docstring on a line by itself:} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{For one-line docstrings, keep the """ on the same line:} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.964 cm} x{2.964 cm} x{1.672 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Whitespace Around Binary Operators}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Surround the following binary operators with a single space on either side:}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Assignment operators (=, +=, -=, and so forth) & Comparisons (==, !=, \textgreater{}, \textless{}. \textgreater{}=, \textless{}=) and (is, is not, in, not in) & \seqsplit{Booleans} (and, not, or) \tn % Row Count 7 (+ 5) % Row 2 \SetRowColor{LightBackground} {\bf{When = is used to assign a default value to a function argument, do not surround it with spaces.}} & def \seqsplit{function(default\_parameter=5):} & \tn % Row Count 14 (+ 7) % Row 3 \SetRowColor{white} y = x**2 + 5 & z = (x+y) * (x-y) & \tn % Row Count 16 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{if x\textgreater{}5 and x\%2==0:} \tn % Row Count 17 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{\# Treat the colon as the operator with lowest priority} \tn % Row Count 19 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{list{[}x+1 : x+2{]}} \tn % Row Count 20 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{In an extended slice, both colons must be surrounded by the same amount of whitespace} \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} list{[}3:4:5{]} & list{[}x+1 : x+2 : x+3{]} & \tn % Row Count 24 (+ 2) % Row 9 \SetRowColor{white} The space is omitted if a slice parameter is omitted & list{[}x+1 : x+2 :{]} & \tn % Row Count 28 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{When to Ignore PEP 8}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{If complying with PEP 8 would break compatibility with existing software} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{If code surrounding what you're working on is inconsistent with PEP 8} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{If code needs to remain compatible with older versions of Python} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}