\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{Gaston} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (Gaston) /Subject (Python 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}{3153A3} \definecolor{LightBackground}{HTML}{F2F4F9} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Gaston} via \textcolor{DarkBackground}{\uline{cheatography.com/24027/cs/5463/}}} \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}Gaston \\ \uline{cheatography.com/gaston} \\ \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 8th 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*}{2} \begin{tabularx}{8.4cm}{p{0.88 cm} x{7.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Identifier classes}} \tn % Row 0 \SetRowColor{LightBackground} \_\_*\_\_ & System-defined names \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \_* & Module private name, not imported via import * \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \_\_* & Class-private name \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.56 cm} x{5.44 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Tuple}} \tn % Row 0 \SetRowColor{LightBackground} ( obj1, obj2, ... ) & tuple literal (parenthesis are optional, at least one comma is required) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} ( obj1 , ) & singleton, tuple with only one object (parenthesis are optional) \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} ( ) & empty tuple \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{A tuple is an immutable sequence of objects} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.32 cm} x{3.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List}} \tn % Row 0 \SetRowColor{LightBackground} {[} obj1, obj2, ... {]} & list literal \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {[} obj1 {]} & single item list \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {[} {]} & empty list \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{A list is a mutable sequence of objects} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.36 cm} x{6.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Slices}} \tn % Row 0 \SetRowColor{LightBackground} s{[}i{]} & Item at index i of sequence s (not a slice) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} s{[}i:j{]} & Items i to j-1 of s \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} s{[}i:j:k{]} & every k\textasciicircum{}th\textasciicircum{} item starting at index i and not including index j or higher \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} s{[}:{]} & "s{[}:{]}" is a copy of seqquence s, while "s" is a reference to the same sequence \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{A Slice is a part of a sequence\{\{nl\}\} \newline Indexes start at 0 for the first item\{\{nl\}\} \newline Negative indexes start at the end of the sequence (eg. -1 is last element, -2 second last,...)} \tn \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}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} \{ key1: itm1, key2: itm2, ... \} & Dictionary of items referenced by key \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} d{[}key{]} & return item referened by given key \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} d{[}key{]} = x & Add (or replaces) given key with attached item x \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} del d{[}key{]} & Remove d{[}key{]} from d. Raises KeyError if key not present in d \tn % Row Count 11 (+ 4) % Row 4 \SetRowColor{LightBackground} key in d & returns {\emph{True}} if key is in dictionary \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} key not in d & returns {\emph{False}} if key is in dictionary \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} d.items() & returns a list of key,value pairs \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} d.keys() & returns list of keys in dictionary \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} d.values() & returns a list of the dictionaries values \tn % Row Count 22 (+ 3) % Row 9 \SetRowColor{white} d.copy() & shallow copy of the dictionary (only top level objects are copies, any object remains is a reference to the "old" object) \tn % Row Count 29 (+ 7) % Row 10 \SetRowColor{LightBackground} d.deepcopy() & deep copy of the dictionary \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries (cont)}} \tn % Row 11 \SetRowColor{LightBackground} d.get(key{[}, default{]}) & returns d{[}key{]} if th ekey is present, or default if not. If default is not passed, None is returned \tn % Row Count 5 (+ 5) % Row 12 \SetRowColor{white} d.pop(key{[}, default{]}) & same as get() except that the key,value pair is also removed from the dictionary \tn % Row Count 9 (+ 4) % Row 13 \SetRowColor{LightBackground} d.popitem() & pops an arbitrary (key, value) pair from the dictionary. \tn % Row Count 12 (+ 3) % Row 14 \SetRowColor{white} d.setdefault(key{[}, default{]}) & returns d{[}{\emph{key}}{]} if it exists, otherwhise does {\emph{d{[}key{]}=default}} and returns {\emph{default}} as well. {\emph{default}} defaults to None \tn % Row Count 19 (+ 7) % Row 15 \SetRowColor{LightBackground} viewitems()\{\{nl\}\}viewkeys()\{\{nl\}\}viewvalues() & returns dictionary view object contining either (key,value) pairs, only keys or only values \tn % Row Count 24 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{1) Keys are unique\{\{nl\}\} \newline 2) If the key is a string it needs to be quoted \newline 3) for loop "for k in dict" returns the keys, not the item} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.68 cm} x{6.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{(Im)mutuable sequences}} \tn % Row 0 \SetRowColor{LightBackground} x in s & {\emph{True}} if an item of s is equal to x \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} x not in s & {\emph{False}} if an item of s is equal to x \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} len(s) & number of items in s \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} min(s) & smallest item of s \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} max(s) & largest item if s \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \seqsplit{s.index(x)} & first index of item equal to x in s \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{s.count(x)} & number of occurences of x in s \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{These operations apply to mutable and immutable sequences \newline \newline Sequence indexes start at 0 (first element), negative indexes start at the end (inex -1 is last element, -2 second last,...) \newline s: sequence \newline x : object/item} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.68 cm} x{4.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Mutable sequences}} \tn % Row 0 \SetRowColor{LightBackground} s{[}i{]} = x & replace item i of sequence s by x \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} s{[}i:j{]} = x & replace items i to j-1 (slice) by x \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} s{[}i:j:k{]} = x & replace each k\textasciicircum{}th\textasciicircum{} item of s starting at i and not including j or higher by x \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} del \textless{}slice\textgreater{} & delete the specified slice from the sequence \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} s.append(x) & appends obect x to the end of the sequence s \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} s.extend(s2) & appends items of sequence s2 to s \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} s.insert(i, x) & inserts object x before the current item at index i \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} s.pop(i) & Removes item i from the sequence and returns it. If i is omitted i=-1 is ssumed \tn % Row Count 23 (+ 4) % Row 8 \SetRowColor{LightBackground} s.remove(x) & same as del s{[}s.index(x){]} \tn % Row Count 25 (+ 2) % Row 9 \SetRowColor{white} s.reverse() & reverse the order of the items in s in place \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} s.sort({[} cmp {[}, key {[}, reverse{]}{]}{]}) & sort elements in place (1) \tn % Row Count 30 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{(1) cmp is optional comparison function with 2 arguments (the items to be compared) (i.e. func(a,b)) and returning -1 if a\textless{}b, 0 if a==b and 1 if a\textgreater{}b \newline (2) key is an optional function taking one argument (item from the sequence) and used to extract the actual information from that item to be compared \newline (3) reverse, if True, causes the sort result to have reverse order \newline (4) in general specifying key and reverse (if possible) is much faster than the equivalent cmp function} \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}{Classes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class \textless{}classname\textgreater{}:(1) \newline \textless{}static-var\textgreater{}=\textless{}value\textgreater{} (2) \newline \newline def \_\_init\_\_(self,\textless{}args\textgreater{}...): (3) \newline \textless{}class constructor\textgreater{} \newline self.\textless{}attribute\textgreater{}=\textless{}value\textgreater{} (4) \newline \newline def method(self,\textless{}args\textgreater{},...): (5) \newline \textless{}method body\textgreater{} \newline \newline def \_\_privateMethod(self, \textless{}args\textgreater{}...): (6) \newline \textless{}method body\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{(1) Start definition of class \newline (2) Static variable, same value for all instances and referenced via \textless{}classname\textgreater{}.\textless{}varname\textgreater{} rather than via self \newline (3) Class constructor called via \textless{}calssname\textgreater{}() with optional arguments \newline (4) Attribute local to the instance \newline (5) Class method definition. Each and every method in a class has at least one argument, "self" referring the class instance \newline (6) private methods are not intended to be used from outside the class and are marked by two leading underscores} \tn \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}{Special class methods}} \tn % Row 0 \SetRowColor{LightBackground} \_\_init\_\_(self{[}, ...{]}) & Called for any new instance creation in order to initialize it. Derived classes must call their base class \_\_init\_\_ explicitely via \textless{}baseclass\textgreater{}.\_\_init\_\_(self,...) where self needs to be explicitely passed \tn % Row Count 11 (+ 11) % Row 1 \SetRowColor{white} \_\_del\_\_(self) & Called when the instance is to be destroyed \tn % Row Count 14 (+ 3) % Row 2 \SetRowColor{LightBackground} \_\_repr\_\_(self) & Called by the repr() function to return a string containing the classes representation. Usually used for debugging. If thsi method is defined and \_\_str()\_\_ is not, this method is also used for string conversions \tn % Row Count 25 (+ 11) % Row 3 \SetRowColor{white} \_\_str\_\_(self) & called by the str() function and by "print" \tn % Row Count 28 (+ 3) % Row 4 \SetRowColor{LightBackground} \_\_lt\_\_(self,other)\{\{nl\}\}\_\_le\_\_(self,other)\{\{nl\}\}\_\_eq\_\_(self,other)\{\{nl\}\}\_\_ne\_\_(self,other)\{\{nl\}\}\_\_gt\_\_(self,other)\{\{nl\}\}\_\_ge\_\_(self,other)\{\{nl\}\} & Rich comparison methods returning True if self\textless{}other, self\textless{}=other, self==other, self!=other. There is no defined relationship, i.e. x==y True doe not imply that x!=y is False \tn % Row Count 37 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}