\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{stonekirby} \pdfinfo{ /Title (python-basics.pdf) /Creator (Cheatography) /Author (stonekirby) /Subject (Python 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}{2470A3} \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{Python basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{stonekirby} via \textcolor{DarkBackground}{\uline{cheatography.com/25026/cs/6414/}}} \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}stonekirby \\ \uline{cheatography.com/stonekirby} \\ \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 12th 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{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Primitive Datatypes and Operators}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# You have numbers \newline % Row Count 1 (+ 1) 3 \# =\textgreater{} 3 \newline % Row Count 2 (+ 1) \# Math is what you would expect \newline % Row Count 3 (+ 1) 1 + 1 \# =\textgreater{} 2 \newline % Row Count 4 (+ 1) 8 - 1 \# =\textgreater{} 7 \newline % Row Count 5 (+ 1) 10 * 2 \# =\textgreater{} 20 \newline % Row Count 6 (+ 1) \# Except division which returns floats, real numbers, by default \newline % Row Count 8 (+ 2) 35 / 5 \# =\textgreater{} 7.0 \newline % Row Count 9 (+ 1) \# Result of integer division truncated down both for positive and negative. \newline % Row Count 11 (+ 2) 5 // 3 \# =\textgreater{} 1 \newline % Row Count 12 (+ 1) 5.0 // 3.0 \# =\textgreater{} 1.0 \# works on floats too \newline % Row Count 13 (+ 1) -5 // 3 \# =\textgreater{} -2 \newline % Row Count 14 (+ 1) -5.0 // 3.0 \# =\textgreater{} -2.0 \newline % Row Count 15 (+ 1) \# When you use a float, results are floats \newline % Row Count 16 (+ 1) 3 * 2.0 \# =\textgreater{} 6.0 \newline % Row Count 17 (+ 1) \# Modulo operation \newline % Row Count 18 (+ 1) 7 \% 3 \# =\textgreater{} 1 \newline % Row Count 19 (+ 1) \# Exponentiation (x**y, x to the yth power) \newline % Row Count 20 (+ 1) 2**4 \# =\textgreater{} 16 \newline % Row Count 21 (+ 1) \# Enforce precedence with parentheses \newline % Row Count 22 (+ 1) (1 + 3) * 2 \# =\textgreater{} 8 \newline % Row Count 23 (+ 1) \# Boolean values are primitives (Note: the capitalization) \newline % Row Count 25 (+ 2) True \newline % Row Count 26 (+ 1) False \newline % Row Count 27 (+ 1) \# negate with not \newline % Row Count 28 (+ 1) not True \# =\textgreater{} False \newline % Row Count 29 (+ 1) not False \# =\textgreater{} True \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Primitive Datatypes and Operators (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# Boolean Operators \newline % Row Count 1 (+ 1) \# Note "and" and "or" are case-sensitive \newline % Row Count 2 (+ 1) True and False \# =\textgreater{} False \newline % Row Count 3 (+ 1) False or True \# =\textgreater{} True \newline % Row Count 4 (+ 1) \# Note using Bool operators with ints \newline % Row Count 5 (+ 1) 0 and 2 \# =\textgreater{} 0 \newline % Row Count 6 (+ 1) -5 or 0 \# =\textgreater{} -5 \newline % Row Count 7 (+ 1) 0 == False \# =\textgreater{} True \newline % Row Count 8 (+ 1) 2 == True \# =\textgreater{} False \newline % Row Count 9 (+ 1) 1 == True \# =\textgreater{} True \newline % Row Count 10 (+ 1) \# Equality is == \newline % Row Count 11 (+ 1) 1 == 1 \# =\textgreater{} True \newline % Row Count 12 (+ 1) 2 == 1 \# =\textgreater{} False \newline % Row Count 13 (+ 1) \# Inequality is != \newline % Row Count 14 (+ 1) 1 != 1 \# =\textgreater{} False \newline % Row Count 15 (+ 1) 2 != 1 \# =\textgreater{} True \newline % Row Count 16 (+ 1) \# More comparisons \newline % Row Count 17 (+ 1) 1 \textless{} 10 \# =\textgreater{} True \newline % Row Count 18 (+ 1) 1 \textgreater{} 10 \# =\textgreater{} False \newline % Row Count 19 (+ 1) 2 \textless{}= 2 \# =\textgreater{} True \newline % Row Count 20 (+ 1) 2 \textgreater{}= 2 \# =\textgreater{} True \newline % Row Count 21 (+ 1) \# Comparisons can be chained! \newline % Row Count 22 (+ 1) 1 \textless{} 2 \textless{} 3 \# =\textgreater{} True \newline % Row Count 23 (+ 1) 2 \textless{} 3 \textless{} 2 \# =\textgreater{} False \newline % Row Count 24 (+ 1) \# (is vs. ==) is checks if two variable refer to the same object, but == checks \newline % Row Count 26 (+ 2) \# if the objects pointed to have the same values. \newline % Row Count 27 (+ 1) a = {[}1, 2, 3, 4{]} \# Point a at a new list, {[}1, 2, 3, 4{]} \newline % Row Count 29 (+ 2) b = a \# Point b at what a is pointing to \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Primitive Datatypes and Operators (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{b is a \# =\textgreater{} True, a and b refer to the same object \newline % Row Count 2 (+ 2) b == a \# =\textgreater{} True, a's and b's objects are equal \newline % Row Count 4 (+ 2) b = {[}1, 2, 3, 4{]} \# Point a at a new list, {[}1, 2, 3, 4{]} \newline % Row Count 6 (+ 2) b is a \# =\textgreater{} False, a and b do not refer to the same object \newline % Row Count 8 (+ 2) b == a \# =\textgreater{} True, a's and b's objects are equal% Row Count 10 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Using {[}None{]}}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# None is an object \newline % Row Count 1 (+ 1) None \# =\textgreater{} None \newline % Row Count 2 (+ 1) \# Don't use the equality "==" symbol to compare objects to None \newline % Row Count 4 (+ 2) \# Use "is" instead. This checks for equality of object identity. \newline % Row Count 6 (+ 2) "etc" is None \# =\textgreater{} False \newline % Row Count 7 (+ 1) None is None \# =\textgreater{} True \newline % Row Count 8 (+ 1) \# None, 0, and empty strings/lists/dicts all evaluate to False. \newline % Row Count 10 (+ 2) \# All other values are True \newline % Row Count 11 (+ 1) bool(0) \# =\textgreater{} False \newline % Row Count 12 (+ 1) bool("") \# =\textgreater{} False \newline % Row Count 13 (+ 1) bool({[}{]}) \# =\textgreater{} False \newline % Row Count 14 (+ 1) bool(\{\}) \# =\textgreater{} False% Row Count 15 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Strings}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# Strings are created with " or ' \newline % Row Count 1 (+ 1) "This is a string." \newline % Row Count 2 (+ 1) 'This is also a string.' \newline % Row Count 3 (+ 1) \# Strings can be added too! But try not to do this. \newline % Row Count 5 (+ 2) "Hello " + "world!" \# =\textgreater{} "Hello world!" \newline % Row Count 6 (+ 1) \# Strings can be added without using '+' \newline % Row Count 7 (+ 1) "Hello " "world!" \# =\textgreater{} "Hello world!" \newline % Row Count 8 (+ 1) \# A string can be treated like a list of characters \newline % Row Count 10 (+ 2) "This is a string"{[}0{]} \# =\textgreater{} 'T' \newline % Row Count 11 (+ 1) \# .format can be used to format strings, like this: \newline % Row Count 13 (+ 2) "\{\} can be \{\}".format("Strings", "interpolated") \# =\textgreater{} "Strings can be interpolated" \newline % Row Count 15 (+ 2) \# You can repeat the formatting arguments to save some typing. \newline % Row Count 17 (+ 2) "\{0\} be nimble, \{0\} be quick, \{0\} jump over the \{1\}".format("Jack", "candle stick") \newline % Row Count 19 (+ 2) \# =\textgreater{} "Jack be nimble, Jack be quick, Jack jump over the candle stick" \newline % Row Count 21 (+ 2) \# You can use keywords if you don't want to count. \newline % Row Count 23 (+ 2) "\{name\} wants to eat \{food\}".format(name="Bob", food="lasagna") \# =\textgreater{} "Bob wants to eat lasagna" \newline % Row Count 25 (+ 2) \# If your Python 3 code also needs to run on Python 2.5 and below, you can also \newline % Row Count 27 (+ 2) \# still use the old style of formatting: \newline % Row Count 28 (+ 1) "\%s can be \%s the \%s way" \% ("Strings", "interpolated", "old") \# =\textgreater{} "Strings can be interpolated the old way"% Row Count 31 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Lists}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# Lists store sequences \newline % Row Count 1 (+ 1) li = {[}{]} \newline % Row Count 2 (+ 1) \# You can start with a prefilled list \newline % Row Count 3 (+ 1) other\_li = {[}4, 5, 6{]} \newline % Row Count 4 (+ 1) \# Add stuff to the end of a list with append \newline % Row Count 5 (+ 1) li.append(1) \# li is now {[}1{]} \newline % Row Count 6 (+ 1) li.append(2) \# li is now {[}1, 2{]} \newline % Row Count 7 (+ 1) li.append(4) \# li is now {[}1, 2, 4{]} \newline % Row Count 8 (+ 1) li.append(3) \# li is now {[}1, 2, 4, 3{]} \newline % Row Count 9 (+ 1) \# Remove from the end with pop \newline % Row Count 10 (+ 1) li.pop() \# =\textgreater{} 3 and li is now {[}1, 2, 4{]} \newline % Row Count 11 (+ 1) \# Let's put it back \newline % Row Count 12 (+ 1) li.append(3) \# li is now {[}1, 2, 4, 3{]} again. \newline % Row Count 13 (+ 1) \# Access a list like you would any array \newline % Row Count 14 (+ 1) li{[}0{]} \# =\textgreater{} 1 \newline % Row Count 15 (+ 1) \# Look at the last element \newline % Row Count 16 (+ 1) li{[}-1{]} \# =\textgreater{} 3 \newline % Row Count 17 (+ 1) \# Looking out of bounds is an IndexError \newline % Row Count 18 (+ 1) li{[}4{]} \# Raises an IndexError \newline % Row Count 19 (+ 1) \# You can look at ranges with slice syntax. \newline % Row Count 20 (+ 1) \# (It's a closed/open range for you mathy types.) \newline % Row Count 21 (+ 1) li{[}1:3{]} \# =\textgreater{} {[}2, 4{]} \newline % Row Count 22 (+ 1) \# Omit the beginning \newline % Row Count 23 (+ 1) li{[}2:{]} \# =\textgreater{} {[}4, 3{]} \newline % Row Count 24 (+ 1) \# Omit the end \newline % Row Count 25 (+ 1) li{[}:3{]} \# =\textgreater{} {[}1, 2, 4{]} \newline % Row Count 26 (+ 1) \# Select every second entry \newline % Row Count 27 (+ 1) li{[}::2{]} \# =\textgreater{}{[}1, 4{]} \newline % Row Count 28 (+ 1) \# Return a reversed copy of the list \newline % Row Count 29 (+ 1) li{[}::-1{]} \# =\textgreater{} {[}3, 4, 2, 1{]} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# Use any combination of these to make advanced slices \newline % Row Count 2 (+ 2) \# li{[}start:end:step{]} \newline % Row Count 3 (+ 1) \# Make a one layer deep copy using slices \newline % Row Count 4 (+ 1) li2 = li{[}:{]} \# =\textgreater{} li2 = {[}1, 2, 4, 3{]} but (li2 is li) will result in false. \newline % Row Count 6 (+ 2) \# Remove arbitrary elements from a list with "del" \newline % Row Count 8 (+ 2) del li{[}2{]} \# li is now {[}1, 2, 3{]} \newline % Row Count 9 (+ 1) \# Remove first occurrence of a value \newline % Row Count 10 (+ 1) li.remove(2) \# li is now {[}1, 3{]} \newline % Row Count 11 (+ 1) li.remove(2) \# Raises a ValueError as 2 is not in the list \newline % Row Count 13 (+ 2) \# Insert an element at a specific index \newline % Row Count 14 (+ 1) li.insert(1, 2) \# li is now {[}1, 2, 3{]} again \newline % Row Count 15 (+ 1) \# Get the index of the first item found matching the argument \newline % Row Count 17 (+ 2) li.index(2) \# =\textgreater{} 1 \newline % Row Count 18 (+ 1) li.index(4) \# Raises a ValueError as 4 is not in the list \newline % Row Count 20 (+ 2) \# You can add lists \newline % Row Count 21 (+ 1) \# Note: values for li and for other\_li are not modified. \newline % Row Count 23 (+ 2) li + other\_li \# =\textgreater{} {[}1, 2, 3, 4, 5, 6{]} \newline % Row Count 24 (+ 1) \# Concatenate lists with "extend()" \newline % Row Count 25 (+ 1) li.extend(other\_li) \# Now li is {[}1, 2, 3, 4, 5, 6{]} \newline % Row Count 27 (+ 2) \# Check for existence in a list with "in" \newline % Row Count 28 (+ 1) 1 in li \# =\textgreater{} True \newline % Row Count 29 (+ 1) \# Examine the length with "len()" \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{len(li) \# =\textgreater{} 6% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Variables}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\# Python has a print function \newline % Row Count 1 (+ 1) print("I'm Python. Nice to meet you!") \# =\textgreater{} I'm Python. Nice to meet you! \newline % Row Count 3 (+ 2) \# By default the print function also prints out a newline at the end. \newline % Row Count 5 (+ 2) \# Use the optional argument end to change the end character. \newline % Row Count 7 (+ 2) print("Hello, World", end="!") \# =\textgreater{} Hello, World! \newline % Row Count 9 (+ 2) \# Simple way to get input data from console \newline % Row Count 10 (+ 1) input\_string\_var = input("Enter some data: ") \# Returns the data as a string \newline % Row Count 12 (+ 2) \# Note: In earlier versions of Python, input() method was named as raw\_input() \newline % Row Count 14 (+ 2) \# No need to declare variables before assigning to them. \newline % Row Count 16 (+ 2) \# Convention is to use \seqsplit{lower\_case\_with\_underscores} \newline % Row Count 18 (+ 2) some\_var = 5 \newline % Row Count 19 (+ 1) some\_var \# =\textgreater{} 5 \newline % Row Count 20 (+ 1) \# Accessing a previously unassigned variable is an exception. \newline % Row Count 22 (+ 2) \# See Control Flow to learn more about exception handling. \newline % Row Count 24 (+ 2) some\_unknown\_var \# Raises a NameError% Row Count 25 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}