\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{ordowe (hahahaha)} \pdfinfo{ /Title (python3.pdf) /Creator (Cheatography) /Author (ordowe (hahahaha)) /Subject (Python3 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}{2178A3} \definecolor{LightBackground}{HTML}{F1F6F9} \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{Python3 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{ordowe (hahahaha)} via \textcolor{DarkBackground}{\uline{cheatography.com/80914/cs/19454/}}} \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}ordowe (hahahaha) \\ \uline{cheatography.com/hahahaha} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 23rd April, 2019.\\ Updated 24th April, 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{3.44 cm} x{4.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} Create an empty list & newlist={[}{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Assign value at index & alist{[}index{]}= value \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Access value at index & alist{[}index{]} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Add item to list & alist.append(new item) \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} Insert into list & alist.insert(at position, new item) \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} Count \# of an item in list & alist.count( item ) \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} Delete 1 matching item & alist.remove(del item) \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} Remove item at index & del alist{[}index{]} \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Looping}} \tn % Row 0 \SetRowColor{LightBackground} For loop 0 thru 9 & for x in range(10): \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} For loop 5 thru 10 & for x in range(5,11): \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} For each char in a string & for char in astring: \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} For items in list & for x in alist: \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} For indexes/values in a list & \{\{noshy\}\}for index,value in enumerate(alist): \tn % Row Count 8 (+ 3) % Row 5 \SetRowColor{white} For each key in a dict & \{\{noshy\}\}for x in adict.keys(): \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} For all items in dict & \{\{noshy\}\}for key,value in adict.items(): \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} while \textless{}logic test\textgreater{} & do: \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} Exit loop immediately & break \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} Skip rest of loop and do loop again & continue \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.964 cm} x{2.888 cm} x{1.748 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Logic and Math Operators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Math Operator}} & {\bf{Example}} & {\bf{X=7, Y=5}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Addition & X + Y & 12 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} Subtraction & X - Y & 2 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Multiplication & X * Y & 35 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} Division & X / Y & 1.4 \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} Floor & X // Y & 1 \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} Exponent & X ** Y & 16807 \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} Modulo & X \% Y & 2 \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Logic Operator}}} \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} Equality & X == Y & False \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} Greater Than & X \textgreater{} Y & False \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} Less Than & X \textless{} Y & True \tn % Row Count 13 (+ 1) % Row 12 \SetRowColor{LightBackground} Less or Equal & X \textless{}= Y & True \tn % Row Count 14 (+ 1) % Row 13 \SetRowColor{white} Not Equal & X !=Y or X\textless{}\textgreater{}Y & True \tn % Row Count 15 (+ 1) % Row 14 \SetRowColor{LightBackground} Bitwise Exclusive Or & a \textasciicircum{} b\{\{nl\}\}xor(a, b) & \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.872 cm} x{2.16 cm} x{2.088 cm} x{1.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Converting Data Types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Covert}}\{\{nobreak\}\} & {\bf{Syntax}}\{\{nobreak\}\} & {\bf{Example}}\{\{nobreak\}\} & {\bf{Result}}\{\{nobreak\}\} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Num -\textgreater{} string & str(number)\{\{nl\}\}int, float or long & str(100)\{\{nl\}\}str(3.14) & '100' \{\{nl\}\}'3.14' \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} Encoded bytes -\textgreater{} string & str(txt,encoding) & str(data,"utf8") & \seqsplit{string} with data \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} Num String -\textgreater{} int & \seqsplit{int("string"},base)\{\{nl\}\}default base is 10 & int("42") \{\{nl\}\}int("101",2)\{\{nl\}\} int("ff", 16) & 42 \{\{nl\}\}5\{\{nl\}\} 255 \tn % Row Count 15 (+ 5) % Row 4 \SetRowColor{LightBackground} int -\textgreater{} hex string & \seqsplit{hex(integer)} & hex(255) hex(10) & '0xff'\{\{nl\}\} '0xa' \tn % Row Count 18 (+ 3) % Row 5 \SetRowColor{white} integer -\textgreater{} binary string\{\{nobreak\}\} & \seqsplit{bin(integer)} & bin(5) bin(3) & '0b101'\{\{nl\}\} \seqsplit{'0b11'} \tn % Row Count 22 (+ 4) % Row 6 \SetRowColor{LightBackground} float -\textgreater{} integer & int(float)\{\{nl\}\}drops decimal & \seqsplit{int(3.14159)} int(3.9) & 3\{\{nl\}\} 3 \tn % Row Count 25 (+ 3) % Row 7 \SetRowColor{white} int or str -\textgreater{} float & float(int or str) & \seqsplit{float("3.4")} float(3) & 3.4\{\{nl\}\} 3.0 \tn % Row Count 28 (+ 3) % Row 8 \SetRowColor{LightBackground} String -\textgreater{} ASCII & ord(str) & ord("A") ord("1") & 65 \{\{nl\}\}49 \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.872 cm} x{2.16 cm} x{2.088 cm} x{1.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Converting Data Types (cont)}} \tn % Row 9 \SetRowColor{LightBackground} int -\textgreater{} ASCII & \seqsplit{chr(integer)} & chr(65)\{\{nl\}\} chr(49) & 'A' \{\{nl\}\}'1' \tn % Row Count 3 (+ 3) % Row 10 \SetRowColor{white} bytes -\textgreater{} string & \textless{}bytes\textgreater{}.decode()\{\{nobreak\}\} & b'ABC'.decode()\{\{nobreak\}\} & 'ABC' \tn % Row Count 6 (+ 3) % Row 11 \SetRowColor{LightBackground} string -\textgreater{} bytes & \textless{}str\textgreater{}.encode()\{\{nobreak\}\} & \seqsplit{'abc'.encode()} & \seqsplit{b'abc'} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.36 cm} x{2.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Useful OS functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{import os}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Executing a shell command & os.system() \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Rename the file or directory src to dst & \seqsplit{os.rename(src}, dst) \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} Change working directory & \seqsplit{os.chdir(path)} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} Get the users environment & os.environ() \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} Returns the current working directory & os.getcwd() \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} Create an empty dict & dict=\{\} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Initialize a non-empty dictionary & dict= \{ "key":"value","key2":"value2"\}\{\{noshy\}\} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} Assign a value & dict{[}"key"{]}="value"\{\{noshy\}\} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} Determine if key exists & "key" in dict \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} Access value at key & dict{[}"key"{]}, dict.get("key")\{\{noshy\}\} \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} Iterable View of all keys & dict.keys() \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} Iterable View of all values & dict.values() \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} Iterable View of (key,value) tuples & dict.items() \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.812 cm} x{2.128 cm} x{2.66 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Slicing and Indexing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{x{[}start:stop:step{]}}} & {\bf{x={[}4,8,9,3,0{]}}} & {\bf{x="48930"}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} x{[}0{]} & 4 & '4' \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} x{[}2{]} & 9 & '9' \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} x{[}:3{]} & {[}4,8,9{]} & '489' \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} x{[}3:{]} & {[}3,0{]} & '30' \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} x{[}:-2{]} & {[}4,8,9{]} & '489' \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} x{[}::2{]} & {[}4,9,0{]} & '490' \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} x{[}::-1{]} & {[}0,3,9,8,4{]} & '03984' \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} len(x) & 5 & 5 \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} sorted(x) & {[}0,3,4,8,9{]} & {[}'0','3','4','8','9'{]} \tn % Row Count 12 (+ 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}{Misc}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Adding Comments to code:}} \newline \#Comments begin the line with a pound sign \newline \newline {\bf{Adding Multi-line Comment to code}} \newline """ \newline Multi-Line Comment \newline """ \newline \newline {\bf{Get user input from keyboard}} \newline name = input("What is your name? ") \newline \newline {\bf{Functions}} \newline def add(num1, num2): \newline \#code blocks must be indented \newline \#each space has meaning in python \newline myresult = num1 + num2 \newline return myresult \newline \newline {\bf{if then else statements}} \newline if \textless{}logic test 1\textgreater{}: \newline \#code block here will execute \newline \#when logic test 1 is True \newline elif \textless{}logic test 2\textgreater{}: \newline \#code block executes if logic test 1 is \newline \#False and logic test 2 is True \newline else: \newline \#else has no test and executes when if \newline \#and all elif are False \newline \newline {\bf{python3 shebang}} \newline \#!/usr/bin/env python3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Printing}} \tn % Row 0 \SetRowColor{LightBackground} Standard print & print('\{\}'.format(STRING)) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Print string variable & print ("I am \%s" \% name) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Print int variable & print ("I am \%d" \% number) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Print with + & print ("I am " + name) \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.16 cm} x{3.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Operations}} \tn % Row 0 \SetRowColor{LightBackground} Make lowercase & "Ab".lower()="ab" \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Make UPPERCASE & "Ab".upper()="AB" \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Make Title Format & "hi world".title()="Hi World" \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} Replace a substring & "123".replace('2','z')= "1z3" \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} Count occurrences of substring & \seqsplit{"1123".count("1")=2} \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} Get offset of substring in string & "123".index("2")=1 \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} Detect substring in string & "is" in "fish" == True \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}