\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{otkl} \pdfinfo{ /Title (nus-cs1010s-python.pdf) /Creator (Cheatography) /Author (otkl) /Subject (nus cs1010s 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{nus cs1010s python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{otkl} via \textcolor{DarkBackground}{\uline{cheatography.com/179565/cs/37358/}}} \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}otkl \\ \uline{cheatography.com/otkl} \\ \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 19th April, 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.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Types of Errors}} \tn % Row 0 \SetRowColor{LightBackground} IndexError & Raised when the index of a sequence is out of range \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} NameError & Raised when a variable is not found in the local and global scope \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} SyntaxError & Raised by the parser when a syntax error is encountered \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} TypeError & Raised when a function/operation is applied to an object of an incorrect type \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} \seqsplit{UnboundLocalError} & Raised when a reference is made to a local variable in a function/method, but no value has been bound to that variable \tn % Row Count 19 (+ 6) % Row 5 \SetRowColor{white} \seqsplit{ZeroDivisionError} & Raised when the second operand of a division/module operation is zero \tn % Row Count 22 (+ 3) % Row 6 \SetRowColor{LightBackground} ValueError & Raised when a function gets an argument of a correct type but improper value \tn % Row Count 26 (+ 4) % Row 7 \SetRowColor{white} MemoryError \seqsplit{(RecursionError)} & Raised when an operation runs out of memory \tn % Row Count 28 (+ 2) % Row 8 \SetRowColor{LightBackground} RuntimeError & Raised when an error does not fall under any other category \tn % Row Count 31 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Alphabetical Order (ASCII Table, ord \& chr)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{48: 0 49: 1 50: 2 51: 3 52: 4 53: 5 54: 6 55: 7 56: 8 57: 9 58: : 59: ; 60: \textless{} 61: = 62: \textgreater{} 63: ? 64: @ \newline % Row Count 3 (+ 3) 65: A 66: B 67: C 68: D 69: E 70: F 71: G 72: H 73: I 74: J 75: K 76: L 77: M 78: N 79: O 80: P 81: Q 82: R 83: S 84: T 85: U 86: V 87: W 88: X 89: Y 90: Z \newline % Row Count 7 (+ 4) 97: a 98: b 99: c 100: d 101: e 102: f 103: g 104: h 105:i 106: j 107: k 108: l 109: m 110: n 111: o 112: p 113: q 114: r 115: s 116: t 117: u 118: v 119: w 120: x 121: y 122:z \newline % Row Count 11 (+ 4) ord('A') = 65, chr(66) = 'B' \newline % Row Count 12 (+ 1) 0 \textless{} 9 \textless{} 'A' \textless{} 'Z' \textless{} 'a' \textless{} 'z' \newline % Row Count 13 (+ 1) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z% Row Count 15 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Loop Statements}} \tn % Row 0 \SetRowColor{LightBackground} break & Terminates the whole loop \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{continue} & Stops the current iteration of the loop, and goes on to the next iteration of the loop \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} pass & Does nothing and continues the rest of the code inside the current iteration of the loop \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Boolean Values}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{False evaluates to 0; int(False) == 0, while True evaluates to 1; int(True) = 1 \newline % Row Count 2 (+ 2) On the other hand, any empty str, tuple, list ('', (), {[}{]}), the value 0 and None evaluates to False; bool(0/None/""/()) = False, and any other expression will evaluate to True; bool(1/-95/"CS1010S is fun"/("C", "S", "S", "U", "C", "K", "S")) = True% Row Count 7 (+ 5) } \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}{String Slicing Mechanism}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{s = 'abcdef ' \newline % Row Count 1 (+ 1) 0 1 2 3 4 5 \newline % Row Count 2 (+ 1) -6-5-4-3-2-1 \newline % Row Count 3 (+ 1) s{[}start(inclusive):stop(exclusive):step{]} \newline % Row Count 4 (+ 1) e.g. \newline % Row Count 5 (+ 1) s{[}1:{]} = 'bcdef' \newline % Row Count 6 (+ 1) s{[}3::-1{]} = 'dcba' \newline % Row Count 7 (+ 1) s{[}6:{]} = '' \newline % Row Count 8 (+ 1) s{[}2:-6:-1{]} = 'cb'% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.44333 cm} x{3.53367 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tuple and string functions}} \tn % Row 0 \SetRowColor{LightBackground} len() & Returns the length of the string/number of items in the tuple \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} max() & Returns the largest item in the tuple \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} min() & Returns the smallest item in the tuple \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} sum() & Returns the sum of all elements in the tuple \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} tuple() & Converts an iterable into a tuple \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{tuple.count(ele)} & Counts the number of occurrences of an element in a tuple \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} \seqsplit{str.index(ele)} & Searches the string for a specified ele from the left and returns the position of where it was found \tn % Row Count 18 (+ 4) \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}{Checking data type}} \tn % Row 0 \SetRowColor{LightBackground} type(value) == Type & isinstance(value, Type) \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Orders of Growth (OOG)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{O(1) \textless{} O(log n) \textless{} O(n) \textless{} O(n log n) \textless{} O(n\textasciicircum{}2\textasciicircum{}) \textless{} O(2\textasciicircum{}n\textasciicircum{}) \textless{} O(n!) \textless{} O(n\textasciicircum{}n\textasciicircum{}) \newline % Row Count 2 (+ 2) O(1): Indexing, replacing variable name \newline % Row Count 3 (+ 1) O(log n): Constantly halving/doubling a number (depending on direction) \newline % Row Count 5 (+ 2) O(n): Going through the whole tuple/string (for loop/recursion) \newline % Row Count 7 (+ 2) O(n\textasciicircum{}2\textasciicircum{}): Going through the whole tuple once for each element (Usually nested for loop) \newline % Row Count 9 (+ 2) O(2\textasciicircum{}n\textasciicircum{}): The tree splits into 2/x number of branches for each level (Usually for recursion tree) \newline % Row Count 11 (+ 2) Sample Answer: \newline % Row Count 12 (+ 1) Time: O(n), there is a total of n recursive calls. \newline % Row Count 14 (+ 2) Space: O(n), there is a total of n recursive calls, and each call will take up space on the stack. \newline % Row Count 16 (+ 2) Time: O(n), the loop will iterate n times. \newline % Row Count 17 (+ 1) Space: O(1), no extra memory is needed because the variables are overwritten with the new values.% Row Count 19 (+ 2) } \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}{Big O Notation}} \tn % Row 0 \SetRowColor{LightBackground} Time Complexity: Sum of time taken at each level of the recursion tree (number of recursive calls, intensive operations) & Time Complexity: Count the loops, and the intensive operations (eg string concatenation) \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} Space Complexity: Height of the recursion tree (Also check for strings, tuples, etc) & Space Complexity: Count the variables stored (need to store individual chars for strings) \tn % Row Count 11 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{String Concatenation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677490353_2023-02-27 17_31_30-CS1010S Tutorial 3.png}}} \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}{String Concatenation (2)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677490410_2023-02-27 17_33_07-CS1010S Tutorial 3.png}}} \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}{String Slicing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677490454_2023-02-27 17_33_42-CS1010S Tutorial 3.png}}} \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}{String Slicing (2)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677490503_2023-02-27 17_34_21-CS1010S Tutorial 3.png}}} \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}{Extra OOG}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677490801_2023-02-27 17_39_18-CS1010S Tutorial 3.png}}} \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}{Copy of Tree}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677514776_2023-02-28 00_17_31-Attempt - Lecture 6_ Working with Sequences - Main - Trainings - CS1010S - Progr.png}}} \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}{Flatten Tuples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677493424_2023-02-27 18_23_09-list - How to flatten a tuple in python - Stack Overflow.png}}} \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}{Counting Leaves}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677491186_2023-02-27 17_46_00-Attempt - Lecture 6_ Working with Sequences - Main - Trainings - CS1010S - Progr.png}}} \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}{Counting Change Problem}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677515093_2023-02-28 00_24_26-Attempt - Midterm Exam Practice - Part 1 _Optional_ - Main - Trainings - CS1010S.png}}} \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}{Towers of Hanoi}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1677516960_2023-02-28 00_55_28-Attempt - Tutorial 5_ Working with Sequences - Tutorial - Trainings - CS1010S - .png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}