\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{LasseBoe (Lasse1618)} \pdfinfo{ /Title (python-basics.pdf) /Creator (Cheatography) /Author (LasseBoe (Lasse1618)) /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}{060673} \definecolor{LightBackground}{HTML}{F7F7FA} \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}{LasseBoe (Lasse1618)} via \textcolor{DarkBackground}{\uline{cheatography.com/110079/cs/21547/}}} \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}LasseBoe (Lasse1618) \\ \uline{cheatography.com/lasse1618} \\ \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 14th March, 2023.\\ 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.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Operations}} \tn % Row 0 \SetRowColor{LightBackground} type(x) & \seqsplit{int/float/bool/str/complex} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} bin(x) & binary \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} hex(x) & hexadecimal \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} 1e8 & 1*10\textasciicircum{}8\textasciicircum{} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} inf & infinity \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} NaN & not a number \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} abs(x) & absolute value \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} x//3 & floor division \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} x *= 2 & x = x*2 \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} 3 \% 2 & remainder \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} a ** x & a\textasciicircum{}x\textasciicircum{} \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} \& & and \tn % Row Count 13 (+ 1) % Row 12 \SetRowColor{LightBackground} | & or \tn % Row Count 14 (+ 1) % Row 13 \SetRowColor{white} \textasciicircum{} & xor \tn % Row Count 15 (+ 1) % Row 14 \SetRowColor{LightBackground} == & check for equality (-\textgreater{}bool) \tn % Row Count 17 (+ 2) % Row 15 \SetRowColor{white} != & not equal \tn % Row Count 18 (+ 1) % Row 16 \SetRowColor{LightBackground} range(start, finish, stepsize) & includes start, excludes finish \tn % Row Count 20 (+ 2) \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}{files}} \tn % Row 0 \SetRowColor{LightBackground} f = \seqsplit{open('file.txt'}, 'w') & opens the file\{\{nl\}\}'w' = write\&read\{\{nl\}\}'r' = read \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{f.write('This is a file')} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{f.open} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{f.close} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} f.flush & can be removed safely now \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} f.readline() & reads first line \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} f.readline(x) & reads first x positions \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{f = open({\emph{\textless{}path\textgreater{}}}, 'w')\{\{nl\}\}for line in f:\{\{nl\}\}~`code`\{\{nl\}\}f.close()} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.9862 cm} x{1.9908 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{control structures}} \tn % Row 0 \SetRowColor{LightBackground} if x = y:\{\{nl\}\}~{\emph{command}}\{\{nl\}\}elif:\{\{nl\}\}else: & if condition \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} break & ends innermost loop \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} if x=y and y\textless{}z: & two conditions \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} for i in range(10): & for loop \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} while i \textless{} 5: & while loop \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} my\_iter =\{\{nl\}\}my\_list.\_\_iter\_\_() & creates an iterator \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} next(my\_iter)\{\{nl\}\}{\emph{my\_iter.\_\_next\_\_()}} & gets next element from iter \tn % Row Count 12 (+ 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}{vectors \& matrices}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import numpy as np} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} v = np.array({[}{[}3, 2{]}{]})\{\{nl\}\}\{\{nl\}\}note: np.array({[}3, 2{]}) is a rank 1 array, NOT a math. vector & Vectors are lists of lists \tn % Row Count 6 (+ 5) % Row 2 \SetRowColor{LightBackground} v.T & transpose \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} v.shape & returns dimensions \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} np.linalg.norm(v)\{\{nl{]}{]}{\emph{or}}\{\{nl\}\}math.sqrt(dot(v.T,v)) & length of vector \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} A = np.array({[}{[}1,2{]},{[}3,4{]}{]}) & creates a matrix \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 13 (+ 0) % Row 7 \SetRowColor{white} np.random.rand(2,2) & random matrix\{\{nl\}\}values normal distribution 0 to 1 \tn % Row Count 16 (+ 3) % Row 8 \SetRowColor{LightBackground} np.random.randn(2,2) & normal distribution (mean 0, variance 1) \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} A.dtype & specifically for np \tn % Row Count 19 (+ 1) % Row 10 \SetRowColor{LightBackground} A.ndim & number of dimensions \tn % Row Count 20 (+ 1) % Row 11 \SetRowColor{white} np.arange(x) & range as array with x elements \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} np.identity(x) & identity matrix \tn % Row Count 23 (+ 1) % Row 13 \SetRowColor{white} np.zeros((x,y)) & zeros matrix \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{LightBackground} A{[}0{]} & first row \tn % Row Count 25 (+ 1) % Row 15 \SetRowColor{white} A{[}0{]}{[}0{]} {\emph{or}} A{[}0, 0{]} & first element of first row \tn % Row Count 27 (+ 2) % Row 16 \SetRowColor{LightBackground} A{[}:, 0{]} & first column \tn % Row Count 28 (+ 1) % Row 17 \SetRowColor{white} A{[}1:4, 0{]} & slicing \tn % Row Count 29 (+ 1) % Row 18 \SetRowColor{LightBackground} A{[}1:, 1:3{]} = 9 & assigning to parts of the matrix\{\{nl\}\}Slicing in python creates copy\{\{nl\}\}In numpy it doesn't! \tn % Row Count 34 (+ 5) \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}{vectors \& matrices (cont)}} \tn % Row 19 \SetRowColor{LightBackground} np.full((2, 3), x) & 2by3 matrix filled with x \tn % Row Count 2 (+ 2) % Row 20 \SetRowColor{white} view = A{[}0:3,2:4{]} & writing into a view changes underlying data \tn % Row Count 5 (+ 3) % Row 21 \SetRowColor{LightBackground} view1 = A{[}0:3,2:4{]}.copy & writing doesn't change data \tn % Row Count 7 (+ 2) % Row 22 \SetRowColor{white} np.where(A\textless{}0, 0, A) & condition, then, else \tn % Row Count 9 (+ 2) % Row 23 \SetRowColor{LightBackground} np.c\_{[}A{[}:, 0:1{]}, A{[}:,2:6{]}{]} & stack slices horizontally \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{strings}} \tn % Row 0 \SetRowColor{LightBackground} s = input("enter here") & prompts input \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} s = 'x'; s = "x" & are both strings \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} s{[}0{]} & returns character at first position \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} s{[}0:5{]} & slicing \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} s = 'I\textbackslash{}'m fine' & escape \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} s = """long string"""" & enables linebreak \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{print(s)} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \textbackslash{}n & linebreak \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} + & links strings (creates a copy) \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{s =\{\{nl\}\}'\{0\}, beautiful \{1\}!'.format('Hello','World')} \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} s.find('x') & returns postion of first occurance \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} s.replace('l','p') & replace all \tn % Row Count 19 (+ 2) % Row 12 \SetRowColor{LightBackground} s.split & returns list of words \tn % Row Count 20 (+ 1) % Row 13 \SetRowColor{white} len('string') & length \tn % Row Count 21 (+ 1) % Row 14 \SetRowColor{LightBackground} s{[}i{]} & retrieves character at position i \tn % Row Count 23 (+ 2) % Row 15 \SetRowColor{white} s{[}-1{]} & retrieves last character \tn % Row Count 24 (+ 1) % Row 16 \SetRowColor{LightBackground} s{[}i:j{]} & range i:j \tn % Row Count 25 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.83689 cm} x{2.14011 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{lists}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Same functions as strings}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} l = {[}index0, index1{]} & create list \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} l{[}i{]} & retrieves index i \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} l{[}i{]} = x & stores x to index i \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} l.insert(index,'string') & doesn't remove \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} l.append & add to end \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} l.sort & smallest to largest \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{l.reverse} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} l.remove('string'){\emph{\{\{nl\}\}or del l{[}i{]}}} & removes first occurance \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} b = a.copy() & change in a doesn't effect b \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} b = a & change in a effects b \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{tuples}} \tn % Row 0 \SetRowColor{LightBackground} t = ('super', 'man') & create tuple \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} len(t) & returns number of items \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} t.count(3) & returns number of 3s \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} x,y = t & tuple unpacking\{\{nl\}\}x = t{[}0{]}; y = t{[}1{]} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{tuples are immutable lists \newline tuples can't have one number, (1) is a math operation} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{functions}} \tn % Row 0 \SetRowColor{LightBackground} def my\_fct({\emph{var\_a}}, {\emph{var\_b}}): & defines function \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} return({\emph{result}}) & outputs result for further use \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{No typechecking in functions (ducktyping) \newline Keep scope in mind ({\emph{var\_a}} and {\emph{var\_b}} are local variables and are destroyed after functions runs \newline If local variable has same name as global variable, the local one will be referred to when name is used \newline Pitfall: `print = 5` -\textgreater{} predefined functions can be overwriten} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{lambda \& mapping}} \tn % Row 0 \SetRowColor{LightBackground} lambda a, b: a + b & small nameless function that can be applied to lists etc.\{\{nl\}\}e.g.\{\{nl\}\}`sorted(list, key=lambda x: x{[}1{]})` \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} map & e.g.\{\{nl\}\}`list(map(lambda x: x + 10, values))` \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} filter & for filtering lists, e.g.:\{\{nl\}\}`list(filter(lambda x: x \% 2 == 0, values))` \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{operations for Vectors \& matrices}} \tn % Row 0 \SetRowColor{LightBackground} np.dot(v, w) & dot product \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} np.linalg.norm(v) & length of vector \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} is3 = (A == 3)\{\{nl\}\}A \textgreater{} 0 & returns boolean matrix \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} A{[}A \textless{} 0{]} = 0 & replaces entries where conditions is met (true) \tn % Row Count 7 (+ 3) % Row 4 \SetRowColor{LightBackground} np.linalg.solve(A, b) & solves system of linear equations \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} np.mean(v)\{\{nl\}\}A.mean(0) & \{\{nl\}\}0 for col max, 1 for row max \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{np.maximum(v, w)} \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} v.std() & standard deviation \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{v.sort()} \tn % Row Count 14 (+ 1) % Row 9 \SetRowColor{white} np.vstack(A, B)\{\{nl\}\}np.hstack(A, B) & \seqsplit{vertically/horizontaly} stack arrays \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{numpy analytics}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{np.minimum.accumulate(array)} & minimum up until an entry \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} np.argmax(array) & index of max \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.23505 cm} x{1.74195 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{math basics}} \tn % Row 0 \SetRowColor{LightBackground} a = 2 + 3j & complex num \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} from math import pi & π \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} from math import exp & e\textasciicircum{}x\textasciicircum{} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{np.abs(v)} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{np.sqrt(v)} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{np.exp(v)} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{np.log2(v)} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{np.sin(v)} \tn % Row Count 8 (+ 1) \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}{dictionary}} \tn % Row 0 \SetRowColor{LightBackground} d = \{'x' : 'y', 'a' : 'b'\} & key:value \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} d{[}'x'{]} & returns 'y' \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} 'y' in d & returns bool \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} d.keys() & returns {[}'x', 'a'{]} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} d.values() & returns {[}'y', 'b'{]} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} d.items() & returns {[}('x':'y'), ('a':'b'){]} \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} d{[}'tea'{]} = 'Tee' & add item \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} d{[}('super', 'man'){]} = 'Supermann' & tuples as immutable lists \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{del d{[}k{]}} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{key can only occur once, values can occur multiple times \newline (keys need to map to one value) \newline key: must be immutable (int, float, str, tuple (not list)) \newline value: any data type} \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}{sets}} \tn % Row 0 \SetRowColor{LightBackground} s = \{1, 2, 3\} & create set \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} s | s2\{\{nl\}\}{\emph{or s.union(s2)}} & all elements of both sets \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} s \textasciicircum{} s2\{\{nl\}\}{\emph{or s.symmetric\_difference(s2)}} & elements that are in either s or s2 \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} s \& s2\{\{nl\}\}{\emph{or\{\{nl\}\}s.intersection(s2)}} & common elements \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} s \textless{}= s2\{\{nl\}\}{\emph{or s.issubset(s2)}} & test if all elements of s is in s2 (bool) \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} s - s2\{\{nl\}\}{\emph{or s.difference(s2)}} & elements in s but not in s2 \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{no order \newline identical items are combined to one item} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{logging}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import logging} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{from logging import debug} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{logger = logging.getlogger()} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{logger.setLevel(logging.DEBUG)} & activates logger \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} debug('Start') & returns message with some stats \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} logging.disable() & all loggers (incl debug) turned off \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{logging.disable(logging.NOTSET)} & all loggers reactivated \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} try:\{\{nl\}\}~`command`\{\{nl\}\}except:\{\{nl\}\}~print('oops')\{\{nl\}\}{\emph{or}}\{\{nl\}\}except IndexError:\{\{nl\}\}~print('Index not found') & dealing with exceptions \tn % Row Count 18 (+ 7) % Row 8 \SetRowColor{LightBackground} raise Exception ('{\emph{message}}') & creates an exception \tn % Row Count 20 (+ 2) % Row 9 \SetRowColor{white} assert x\textgreater{}0, 'No neg. numbers' & returns error if assertion not met \tn % Row Count 22 (+ 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}{other}} \tn % Row 0 \SetRowColor{LightBackground} np.repeat(1, 10) & array with ten 1s \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} np.asarray({\emph{list}}) & converts list to array \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} np.polyfit({\emph{x\_array}}, {\emph{y\_array}}, {\emph{degree}}) & fits a polynomial the points (x/y), minimizing the squared error \tn % Row Count 7 (+ 4) % Row 3 \SetRowColor{white} np.polyval({\emph{coeff\_array}}, {\emph{x\_eval\_array}}) & evaluates a polynomial with given coefficients (sortet highest degree to lowest!) at a number of x-point \tn % Row Count 13 (+ 6) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{reverse\_a = a{[}::-1{]}} \tn % Row Count 14 (+ 1) \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}{general numpy}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{np.random.randint(low=0, high=10, size=20)} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}