\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{elhamsh} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (elhamsh) /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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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}{elhamsh} via \textcolor{DarkBackground}{\uline{cheatography.com/31327/cs/9479/}}} \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}elhamsh \\ \uline{cheatography.com/elhamsh} \\ \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 June, 2017.\\ 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}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{bisect (basic bisection algorithm)}} \tn % Row 0 \SetRowColor{LightBackground} import bisect & Provides support for maintaining a list in sorted order without having to sort the list after each insertion. \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{a={[}0, 2, 3, 8{]}} \tn % Row Count 7 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{bisect.bisect\_left(a}, value) & Returns first index that value can be inserted in list \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{bisect.bisect\_right(a}, value) & Returns last index that value can be inserted in list \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} a.insert(index, value) & Index from previous commands \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{bisect.insort\_left(a}, value) & Insert value to the correct position \tn % Row Count 17 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{bisect.insort\_right(a, value)} \tn % Row Count 18 (+ 1) % Row 7 \SetRowColor{white} \seqsplit{bisect.insort\_right(a}, value, lo, hi) & For all bisect modules, we can select a sublist {[}lo:hi{]} \tn % Row Count 21 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{def grade(score, breakpoints={[}60, 70, 80, 90{]}, grades='FDCBA'): i = bisect(breakpoints, score) return grades{[}i{]}} \tn % Row Count 24 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{[}grade(score) for score in {[}33, 99, 77, 70, 89, 90, 100{]}{]} output: {[}'F', 'A', 'C', 'C', 'B', 'A', 'A'{]}} \tn % Row Count 27 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{{\emph{Index might be len(a), if item will be added at the end.}}}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Dict}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{d = \{'a': 1, 'b': 2, 'c':3\}/ d=dict()} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} d{[}'a'{]} & Raise KeyError exception if 'a' not in dict \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} d.pop(key) & return value of remove from dic \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} for key, value in d.items(): & iterate \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{for key in d: print key, d{[}key{]}} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} d.clear() & Removes all items from d \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} d.items() & Returns a {\bf{list}} of (key, value) pairs \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} d.keys() & Returns a list of keys {[}'a','b','c'{]} \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} d.values() & Returns a list of values {[}1,2,3{]} \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} d.get(key, default\_value) & Returns value of key in d if exists, O.W. return default\_value \tn % Row Count 21 (+ 4) % Row 10 \SetRowColor{LightBackground} d.pop(key, default\_value) & Return d{[}key{]} and remove key from d if exists, else return default\_value \tn % Row Count 25 (+ 4) % Row 11 \SetRowColor{white} sorted(d) & Returns sorted keys \tn % Row Count 26 (+ 1) % Row 12 \SetRowColor{LightBackground} new = \{\} \{\{nl\}\} for (key, value) in data: \{\{nl\}\} group = new.setdefault(key, {[}{]})\{\{nl\}\} group.append( value ) & Key might exist already \{\{nl\}\} input: data = {[}(1, "a"), (2, "b"){]} \{\{nl\}\} output: new \{1: {[}'a'{]}, 2: {[}'b'{]}\} \tn % Row Count 32 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Dict (cont)}} \tn % Row 13 \SetRowColor{LightBackground} from collections import defaultdict & simpler with defaultdict \tn % Row Count 2 (+ 2) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{new = defaultdict(list) for (key, value) in data: new{[}key{]}.append( value )} \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Itertools}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import itertools} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} for i in itertools.count(5): & i starts from 5 and go to infinity. Use break to stop. \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} for i in itertools.cycle({[}1, 2, 3{]}): & i iterate over the list indefinitely \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} for i in itertools.izip({[}1, 2, 3{]}, {[}'a', 'b', 'c'{]}): \{\{nl\}\} for i in zip({[}1, 2, 3{]}, {[}'a', 'b', 'c'{]}): & (1, 'a') (2, 'b') (3, 'c') \tn % Row Count 12 (+ 6) % Row 4 \SetRowColor{LightBackground} for i in enumerate({[}'a', 'b', 'c'{]}): & (0, 'a') (1, 'b') (2, 'c') \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} for i in \seqsplit{itertools.compress('ABCDEF'}, {[}1,0,1,0,1,1{]}): & A C E F \tn % Row Count 17 (+ 3) % Row 6 \SetRowColor{LightBackground} for i in \seqsplit{itertools.product('AB'}, 'CD'): & ('A', 'C') ('A', 'D') ('B', 'C') ('B', 'D') \tn % Row Count 20 (+ 3) % Row 7 \SetRowColor{white} for i in \seqsplit{itertools.product('ABC'}, repeat=2): & ('A', 'A') ('A', 'B') ('A', 'C') ('B', 'A') ('B', 'B') ('B', 'C') ('C', 'A') ('C', 'B') ('C', 'C') \tn % Row Count 25 (+ 5) % Row 8 \SetRowColor{LightBackground} for i in \seqsplit{itertools.permutations('ABC'}, 2): & ('A', 'B') ('A', 'C') ('B', 'A') ('B', 'C') ('C', 'A') ('C', 'B') \tn % Row Count 29 (+ 4) % Row 9 \SetRowColor{white} for i in \seqsplit{itertools.combinations('ABC'}, 2): & ('A', 'B') ('A', 'C') ('B', 'C') \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Itertools (cont)}} \tn % Row 10 \SetRowColor{LightBackground} for i in \seqsplit{itertools.combinations\_with\_replacement('ABC'}, 2): & ('A', 'A') ('A', 'B') ('A', 'C') ('B', 'B') ('B', 'C') ('C', 'C') \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{String}} \tn % Row 0 \SetRowColor{LightBackground} chr(number) & Return string of one char. chr(65): A \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} ord(char) & Return int code of the char \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} len('asd') & 3 \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} 'asd'.capitalize() & Asd \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} 'asd'.center(width, fill\_char) & 'asd'.center(6, '\$'): \$asd\$\$ \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} 'asdas'.count('as') & 2 \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} 'asdas'.find('a') & 0 \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} 'asdas'.rfind('a') & 3 \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \seqsplit{'asdas'.rsplit(delim}, maxsplits) & 'asdas'.rsplit('a',1): {[}'asd', 's'{]} \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} & 'asdas'.split('a',1): {[}'', 'sdas'{]} \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{'asdas'.endswith('as')} & True \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{'asdas'.startswith('s')} & False \tn % Row Count 19 (+ 2) % Row 12 \SetRowColor{LightBackground} \seqsplit{'absddddsssssaaa'.strip('asd')} & 'b' \tn % Row Count 21 (+ 2) % Row 13 \SetRowColor{white} ' a b '.replace(' ','') & 'ab' \tn % Row Count 23 (+ 2) % Row 14 \SetRowColor{LightBackground} list('abc') & {[}'a', 'b', 'c'{]} \tn % Row Count 24 (+ 1) % Row 15 \SetRowColor{white} sorted('cba') & {[}'a', 'b', 'c'{]} \tn % Row Count 25 (+ 1) % Row 16 \SetRowColor{LightBackground} 'ABs'.lower() & 'abs' \tn % Row Count 26 (+ 1) % Row 17 \SetRowColor{white} 'ABs'.upper() & 'ABS' \tn % Row Count 27 (+ 1) % Row 18 \SetRowColor{LightBackground} 'absdf'.index('b')/ 'absdf'.index('b', 0, 3) & 1 /set start and end point \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{String (cont)}} \tn % Row 19 \SetRowColor{LightBackground} \seqsplit{'absdfa'.rindex('a')} & 5 \tn % Row Count 1 (+ 1) % Row 20 \SetRowColor{white} ''.join({[}'a', 'b', 'c'{]}) & 'abc' \tn % Row Count 3 (+ 2) % Row 21 \SetRowColor{LightBackground} int('34') & 34 \tn % Row Count 4 (+ 1) % Row 22 \SetRowColor{white} float('34.5') & 34.5 \tn % Row Count 5 (+ 1) % Row 23 \SetRowColor{LightBackground} long('345') & 345L \tn % Row Count 6 (+ 1) % Row 24 \SetRowColor{white} 345L==345 & True \tn % Row Count 7 (+ 1) % Row 25 \SetRowColor{LightBackground} '34567'.isdigit() & True \tn % Row Count 8 (+ 1) % Row 26 \SetRowColor{white} 'asd'.islower() & True \tn % Row Count 9 (+ 1) % Row 27 \SetRowColor{LightBackground} 'ASD'.isupper() & True \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Math}} \tn % Row 0 \SetRowColor{LightBackground} divmod(5,3) & 1,2 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} hex(int\_number) & hex(15)='0xf' \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import math} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} math.ceil(4.1) & 5.0 \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} math.floor(4.1) & 4.0 \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} math.pow(2,3) & 8.0 \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import random} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} random.random() & Return a float in {[}0.0,1.0) \tn % Row Count 9 (+ 2) % Row 8 \SetRowColor{LightBackground} random.randint(0,5) & Return a random integer in {[}0,5{]} \tn % Row Count 11 (+ 2) % Row 9 \SetRowColor{white} random.uniform(a,b) & Return a random floating point number N s.t. a\textless{}=N\textless{}=b for a\textless{}=b and b\textless{}=N\textless{}=a for b\textless{}a. \tn % Row Count 16 (+ 5) % Row 10 \SetRowColor{LightBackground} random.choice({[}"mert", "gunay", "kth"{]}) & Return an item form the sequence \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{random.choice('abcdefghij')} & 'c' \tn % Row Count 20 (+ 2) % Row 12 \SetRowColor{LightBackground} random.sample({[}"mert", "gunay", "kth", "stockholm"{]}, 2) & Return k item: {[}'gunay', 'kth'{]} \tn % Row Count 23 (+ 3) % Row 13 \SetRowColor{white} \seqsplit{random.shuffle(list)} & change the order of items randomly \tn % Row Count 25 (+ 2) % Row 14 \SetRowColor{LightBackground} \seqsplit{random.randrange(start}, stop, step) & random.randrange(0, 101, 2): Even integer from 0 to 100 \tn % Row Count 28 (+ 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}{Queue}} \tn % Row 0 \SetRowColor{LightBackground} from collections import deque & double ended queue \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} d=deque('ghi') & deque({[}'g', 'h', 'i'{]}) \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} d.append('j') & deque({[}'g', 'h', 'i', 'j'{]}) \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{d.appendleft('f')} & deque({[}'f', 'g', 'h', 'i', 'j'{]}) \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} d.pop() & 'j' deque({[}'f', 'g', 'h', 'i'{]}) \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} d.popleft() & 'f' deque({[}'g', 'h', 'i'{]}) \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} d.extend('abc') & deque({[}'f', 'g', 'h', 'i', 'a', 'b', 'c'{]}) \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{d.extendleft('abc')} & deque({[}{\bf{'c', 'b', 'a'}}, 'f', 'g', 'h', 'i', 'a', 'b', 'c'{]}) \tn % Row Count 16 (+ 3) % Row 8 \SetRowColor{LightBackground} d.rotate(1) & deque({[}'c', 'c', 'b', 'a', 'f', 'g', 'h', 'i', 'a', 'b'{]}) \tn % Row Count 19 (+ 3) % Row 9 \SetRowColor{white} d.rotate(-2) & deque({[}'b', 'a', 'f', 'g', 'h', 'i', 'a', 'b', 'c', 'c'{]}) \tn % Row Count 22 (+ 3) % Row 10 \SetRowColor{LightBackground} \seqsplit{a=deque(reversed(d))} & a: deque({[}'c', 'c', 'b', 'a', 'i', 'h', 'g', 'f', 'a', 'b'{]}) \tn % Row Count 25 (+ 3) % Row 11 \SetRowColor{white} del a{[}0{]} & deque({[}'c', 'b', 'a', 'i', 'h', 'g', 'f', 'a', 'b'{]}) \tn % Row Count 28 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Priority Queue (heap)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import heapq} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{heap={[}{]}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{heapq.heappush(heap}, 2) & heap == {[}2{]} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{heap={[}4,2,1{]}} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{heapq.heapify(heap)} & heap = {[}1,2,4{]} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{heapq.heappush(heap},3) & heap = {[}1, 2, 4, 3{]} \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{heapq.nlargest(3},heap) & {[}4, 3, 2{]} \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{heapq.nsmallest(2},heap) & {[}1,2{]} \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} heap{[}0{]} & min value. Access heap similar to list \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{heapq.heappop(heap)} & Return 1, heap: {[}2,3,4{]} \tn % Row Count 17 (+ 2) \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}{Bit level}} \tn % Row 0 \SetRowColor{LightBackground} \textasciitilde{}3 ?? & inverted bits of 3. -4 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} number\textless{}\textless{}num\_bits & 3\textless{}\textless{}1: 6 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number\textgreater{}\textgreater{}num\_bits & 3\textgreater{}\textgreater{}1: 1 \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Set}} \tn % Row 0 \SetRowColor{LightBackground} f = frozenset({[}1, 2, 2, 3, 3{]}) & frozenset({[}1, 2, 3{]}) immutable set \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{f = set({[}1,2,3,4{]})} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} f.issubset({[}1,2{]}) & False \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} f.issuperset({[}1,2{]}) & True \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} f.add(5) & {[}1,2,3,4,5{]} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} f.remove(1) & f: {[}2,3,4,5{]} \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{f.clear()} \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} f.intersection({[}3,6{]}) \{\{nl\}\} f \& set({[}3,6{]}) & Return {[}3{]}. Does not update f \tn % Row Count 11 (+ 3) % Row 8 \SetRowColor{LightBackground} f.union({[}3,6{]}) \{\{nl\}\} f|set({[}3,6{]}) & Return {[}2,3,4,5,6{]} \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} f.difference({[}2{]})\{\{nl\}\} f-set({[}2{]}) & {[}3,4,5{]} \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} f.symmetric\_difference({[}2,5,8{]})\{\{nl\}\} f\textasciicircum{}set({[}2,5,8{]}) & set({[}8, 3, 4{]}) {\bf{XOR}} \tn % Row Count 18 (+ 3) % Row 11 \SetRowColor{white} f.update({[}7,6,2{]}) & f: {[}2,3,4,5,6,7{]} \tn % Row Count 19 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Sequences}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{a = {[}'foo', 'bar', 'baz'{]}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} for i, j in enumerate(a): print i, j & returns 0 foo 1 bar 2 baz \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} a=list('abcsd') & a = {[}'a', 'b', 'c', 's', 'd'{]} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} if a: & Returns True if is not empty \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} {[}1{]} + {[}2{]} & Concatenate two list. {[}1,2{]} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} {[}1{]} * 5 & {[}1,1,1,1,1{]} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} b = a{[}:{]}/ b = list(a) & Copy a into b. {\bf{not a=b}} \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} a = reversed({[}1, 2, 3{]}) & Returns an {\bf{iterator}} through sequence in reverse order \tn % Row Count 15 (+ 3) % Row 8 \SetRowColor{LightBackground} for i in a: print i, & 3 2 1 \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} fahrenheit = map(lambda x: (float(9)/5)*x + 32, {[}39.2, 36.5{]}) & Returns a new list where ith item is fct(ith items of sequence(s)) \tn % Row Count 21 (+ 4) % Row 10 \SetRowColor{LightBackground} reduce(lambda x,y: x*y, {[}1, 2, 3{]}) & Returns a value: fct applied cumulatively \tn % Row Count 24 (+ 3) % Row 11 \SetRowColor{white} filter(lambda a: a \% 2 == 0, {[}1, 3, 5, 2{]}) & Returns a list where fct(item) is True. \tn % Row Count 27 (+ 3) % Row 12 \SetRowColor{LightBackground} a = zip({[}1,2,3{]}, {[}"a", "b", "c"{]}) & Returns a list of tuples, ith tuple contains ith items of each sequences \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Sequences (cont)}} \tn % Row 13 \SetRowColor{LightBackground} a={[}0,1,2,3{]} & {[}start:stop{[}:step{]}{]} \tn % Row Count 1 (+ 1) % Row 14 \SetRowColor{white} a{[}::-1{]} & Reverse {[}3,2,1,0{]} \tn % Row Count 2 (+ 1) % Row 15 \SetRowColor{LightBackground} a{[}::-2{]} & {[}3,1{]} \tn % Row Count 3 (+ 1) % Row 16 \SetRowColor{white} a{[}::2{]} & {[}0,2{]} \tn % Row Count 4 (+ 1) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{a.insert(index, value)} \tn % Row Count 5 (+ 1) % Row 18 \SetRowColor{white} a.remove(value) & Remove first occurrence of value \tn % Row Count 7 (+ 2) % Row 19 \SetRowColor{LightBackground} a.pop(index) & Return item at this index and remove it from a \tn % Row Count 10 (+ 3) % Row 20 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{a.count(value)} \tn % Row Count 11 (+ 1) % Row 21 \SetRowColor{LightBackground} a.index(value {[},start{[},stop{]}{]}) & Return first index of item in a. start and end can be defined \tn % Row Count 15 (+ 4) % Row 22 \SetRowColor{white} a.reverse() & Reverse items in a. Returns None! \tn % Row Count 17 (+ 2) % Row 23 \SetRowColor{LightBackground} del a{[}1:3{]} & Remove sublist a{[}1:3{]} from a. a={[}0,3{]} \tn % Row Count 19 (+ 2) % Row 24 \SetRowColor{white} range(10,0,-1) & {[}10, 9, 8, 7, 6, 5, 4, 3, 2, 1{]} \tn % Row Count 21 (+ 2) % Row 25 \SetRowColor{LightBackground} range(0,10,-1) & {[}{]} \tn % Row Count 22 (+ 1) % Row 26 \SetRowColor{white} range(0,10,1) & {[}0, 1, 2, 3, 4, 5, 6, 7, 8, 9{]} \tn % Row Count 24 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Others}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import re} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} re.split("{[}-+{\emph{//{]}+", "3+2}}2+63/3") & {[}'3', '2', '2', '63', '3'{]} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} float("inf") & inf \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{list(itertools.product(*listoflist))} & return all \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import sys} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} sys.maxint & Maximum integer value \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} -1-sys.maxint & Minimum integer value. Convert int to long automatically if its getting larger \tn % Row Count 13 (+ 4) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{from difflib import SequenceMatcher} \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} match = \seqsplit{SequenceMatcher(None}, string1, \seqsplit{string2).find\_longest\_match(0}, len(string1), 0, len(string2)) & match: Match(a=0, b=15, size=9) \tn % Row Count 19 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}