\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{sean} \pdfinfo{ /Title (python-3-the-basics.pdf) /Creator (Cheatography) /Author (sean) /Subject (Python 3 - the basics 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}{068AFF} \definecolor{LightBackground}{HTML}{EFF7FF} \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 3 - the basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{sean} via \textcolor{DarkBackground}{\uline{cheatography.com/22969/cs/4904/}}} \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}sean \\ \uline{cheatography.com/sean} \\ \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 13th May, 2016.\\ 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*}{3} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{NUMBERS}} \tn % Row 0 \SetRowColor{LightBackground} max() & returns largest argument of list \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} min() & returns smallest argument of list \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} abs() & returns absolute value of argument \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{type()} & return data type of argument \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.23965 cm} x{2.73735 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{STRINGS}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Create Strings}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} 'string' & creates string \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} "string" & creates string \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{bt\}\}{\bf{String Methods}}} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} print( ) & ("hello"), (variable) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{g = "Golf"\{\{nl\}\}h = "Hotel"\{\{nl\}\}print("\%s, \%s" \% (g, h))\{\{nl\}\}print("My name is \{0\}".format('Fred'))} \tn % Row Count 8 (+ 3) % Row 6 \SetRowColor{LightBackground} str(3) & returns "3", not \#3 \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} len("string") & returns 5 \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} "string".upper() & returns 'STRING' \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} "STRING".lower() & returns 'string' \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} word{[}0{]} & returns "w" \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} word{[}1:len(word){]} & returns "ord" \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{LOOPING}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{For}} loops ~~~~~~`for {\bf{variable}} in {\bf{sequence}} {\bf{:}}`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`for {\bf{x}} in {\bf{range}}(1, 10):\{\{nl\}\}~~~~print({\bf{x}})`\{\{nl\}\}{\emph{The variable will iterate through each item in the sequence.}}} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{If}}~~~~~~`if {\bf{condition}} {\bf{:}}`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`if {\bf{x == 7}}:\{\{nl\}\}~~~~print(x)`\{\{nl\}\}{\emph{The following lines are only executed if the {\bf{condition}} is true. The {\bf{condition}} can be either a single comparison, eg. 5 \textgreater{} 3, or multiple comparisons using Boolean operators to evaluate ultimate True or False eg. 1\textless{}2 {\bf{and}} 2\textless{}3 (returns True, so the following lines are evaluated)}}} \tn % Row Count 16 (+ 10) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{elif}}~~~~~~`elif {\bf{condition}} {\bf{:}}`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}{\emph{provides second {\bf{if}} test, when first {\bf{if}} test is false}}:\{\{nl\}\}`elif {\bf{x == 5}}:\{\{nl\}\}~~~~print(x)`} \tn % Row Count 21 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{else}} {\emph{(runs if the {\bf{if}}/{\bf{elif}} test is false)}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`else:\{\{nl\}\}~~~~print(y)`} \tn % Row Count 24 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{while}}~~~~~~`while {\bf{condition}} {\bf{:}}`} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}{\emph{Remains in the loop while the condition is true. Use the "break" command to leave the loop. This is useful if waiting on an input from microcontroller. Once the microcontroller input is received, the break command kicks out of the loop and continues with the program.}}} \tn % Row Count 32 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{FUNCTION STRUCTURE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{def}} {\emph{shut\_down(s){\bf{}}:}} \newline {\bf{ if}} {\emph{s == "yes"{\bf{}}:}} \newline {\bf{return}} {\emph{"shutting down"}} \newline {\bf{elif}} {\emph{s == "no"{\bf{}}:}} \newline {\bf{return}} {\emph{"Shutdown aborted"}} \newline {\bf{else:}} \newline {\bf{return}} {\emph{"Sorry"}} \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline {\bf{def}} {\emph{is\_numeric(num){\bf{}}:}} \newline {\bf{return}} {\emph{type(num) == int or}} \newline {\emph{type(num) == float{\bf{}}:}} \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline {\bf{def}} {\emph{finish\_game(score){\bf{}}:}} \newline {\emph{tickets = 10 * score}} \newline {\bf{if}} {\emph{score \textgreater{}= 10{\bf{}}:}} \newline {\emph{tickets += 50}} \newline {\bf{elif}} {\emph{score \textgreater{}= 7{\bf{}}:}} \newline {\emph{tickets += 20}} \newline {\bf{return}} {\emph{tickets}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{IMPORTING MODULES \& FUNCTIONS}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{import}} math} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}makes math module available\{\{nl\}\}{\emph{usage:}} ~math.sqrt()} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{from}} math {\bf{import}} *} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}imports {\bf{all}} math functions into program\{\{nl\}\}{\emph{usage:}} ~sqrt()} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{from}} math {\bf{import}} sqrt} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}imports {\bf{only}} the math function specified\{\{nl\}\}{\emph{usage:}} ~sqrt()} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{READING \& WRITING FILES}} \tn % Row 0 \SetRowColor{LightBackground} `input = open(file)` & variable to hold file \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `indata = input.read()` & reads the file \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `output = open(to\_file, 'w')` & open file to write to, 'w' makes file writable \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`output.write(indata)`} & writes data to `to\_file` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{`output.close()`} & finish by closing `to\_file` \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `input.close()` & finish by closing `file` \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{example: exercise 17 in the book Python The Hard Way (page 43).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{LISTS}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{list\_name = {[}item\_1, item\_2{]}}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Items can be 'strings', variables, integers, etc.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Index}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use the index to access specific locations\{\{nl\}\}{\emph{usage:}}~~`list\_name{[}0{]} \#first entry`} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Add to Lists:}}~~{\emph{.append}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use .append to add a new entry to the end of the List; note the use of ( )\{\{nl\}\}`list\_name.append('string')`\{\{nl\}\}adds 'string' to end of the list contents} \tn % Row Count 12 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Add to Lists:}}~~{\emph{assignment}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}replace an item in a List:\{\{nl\}\}`list\_name{[}4{]} = 'string'`\{\{nl\}\}replaces 5th item in list with 'string'} \tn % Row Count 16 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Adding Lists together}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`list\_A = {[}1,2,3{]}\{\{nl\}\}list\_B = {[}4,5{]}\{\{nl\}\}list\_C = list\_A + list\_B\{\{nl\}\}print(list\_C)~~~~\#returns {[}1,2,3,4,5{]}`} \tn % Row Count 20 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{len(}}{\emph{list\_name{\bf{}})}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}returns the number of items in the list} \tn % Row Count 22 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{print (}}{\emph{list\_name{\bf{}})}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}{\emph{returns}} `{[}item\_1, item\_2, etc.{]}`} \tn % Row Count 24 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Slicing}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`letters = {[}'a', 'b', 'c', 'd'{]}\{\{nl\}\}slice = letters{[}1:3{]}\{\{nl\}\}print slice \#returns {[}'b', 'c'{]}`\{\{nl\}\}`{[}1:3{]}` means this will return entries starting at index 1 and continue up to, {\emph{but not including}} the third index position} \tn % Row Count 30 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{LISTS (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Slicing with Strings}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}strings are considered to be natural lists, with each letter being an item of the list\{\{nl\}\}`animal = 'blueWhale'\{\{nl\}\}print = (animal{[}4:{]})~~~~\#returns Whale\{\{nl\}\}print = (animal{[}:4{]})~~\#returns blue\{\{nl\}\}`} \tn % Row Count 6 (+ 6) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Manipulation}}~~~{\emph{.index}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`letters = {[}'a', 'b', 'c'{]}\{\{nl\}\}print(letters.index('b'))~~~~\#prints 1`} \tn % Row Count 9 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Manipulation}}~~~{\emph{.sort}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`numbers = {[}5, 3, 7{]}\{\{nl\}\}print(numbers.sort())~~~~\#prints {[}3,5,7{]}`} \tn % Row Count 12 (+ 3) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Manipulation}}~~~{\emph{.pop}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`numbers = {[}5, 3, 7{]}\{\{nl\}\}print(numbers.pop())~~\#no index pops last item\{\{nl\}\}~~\#prints 7, and removes it from the list`\{\{nl\}\}`print(numbers.pop(1))~~\#pops second item\{\{nl\}\}~~\#prints 3, and removes it from the list`} \tn % Row Count 19 (+ 7) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{List Manipulation}}~~~{\emph{.insert}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`numbers = {[}5, 3, 7{]}\{\{nl\}\}print(numbers.insert(1,9))~~~\#prints {[}5,9,3,7{]}`\{\{nl\}\}{\emph{inserts the number 9 "before" position 1 in the list}}} \tn % Row Count 24 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`ph\_numbers = \{'Jack':x123, 'Mark':x655\}`\{\{nl\}\}`ph\_numbers{[}'Mark'{]}`~~~`\#returns~x655`\{\{nl\}\}`ph\_numbers{[}'Mark'{]} = x899~~\#assigns new number`\{\{nl\}\}`ph\_numbers`~~~`\#returns~\{'Jack':x123, 'Mark'=x899\}`} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A dictionary is comprised of both a "key" and a "value". You access or reassign a particular value by using the key, NOT the position. The dictionary does not maintain a specific order to any of its entries.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}