\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 (cs1010s-finals.pdf) /Creator (Cheatography) /Author (otkl) /Subject (CS1010S Finals 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}{1A1AA3} \definecolor{LightBackground}{HTML}{F0F0F9} \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{CS1010S Finals Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{otkl} via \textcolor{DarkBackground}{\uline{cheatography.com/179565/cs/38302/}}} \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 20th 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*}{2} \begin{tabularx}{8.4cm}{x{1.368 cm} x{3.116 cm} x{3.116 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Types of Errors and Examples}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{IndexError} & The index of a sequence is out of range. & (1,){[}1{]} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{KeyError} & Key is not found in the dictionary. & cs = \{10:10\}; cs{[}s{]} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{SyntaxError} & Invalid syntaxes found in code. & print(Hello World) (no quote marks) or for i in range(3) (no colon) \tn % Row Count 11 (+ 5) % Row 3 \SetRowColor{white} \seqsplit{TypeError} & A \seqsplit{function/operation} is applied to objects of incorrect types. & (1,2) + {[}3,4{]} or "cs" + 1010 \tn % Row Count 15 (+ 4) % Row 4 \SetRowColor{LightBackground} \seqsplit{ValueError} & Function gets an argument of a correct type but improper value. & int("3.14") \tn % Row Count 19 (+ 4) % Row 5 \SetRowColor{white} \seqsplit{NameError} & The variable with the name is not found in the local \& global scope. & del cs1010s \tn % Row Count 24 (+ 5) % Row 6 \SetRowColor{LightBackground} \seqsplit{AttributeError} & An attribute reference or assignment fails due to incorrect data type. & (1,2).append(3) \tn % Row Count 29 (+ 5) % Row 7 \SetRowColor{white} \seqsplit{ZeroDivisionError} & Second operand (denominator) of a division/module operation is zero. & 1/0 \tn % Row Count 34 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.368 cm} x{3.116 cm} x{3.116 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Types of Errors and Examples (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \seqsplit{RecursionError} & An operation/runs out of memory. & def f(x): return x + f(x-1) \tn % Row Count 2 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{UnboundLocalError} & A reference is made to a local variable in a function/method, but no value has been bound to that variable. & def f(x): ;z = y + x; y = 0 \tn % Row Count 9 (+ 7) % Row 10 \SetRowColor{LightBackground} \seqsplit{RuntimeError} & Can be due to many reasons. One of them being list/dictionary size changing during iteration. & a = {[}1, 2{]}; for ele in a: a.append(ele) \tn % Row Count 15 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Defining Custom Errors}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{class CustomError: \newline % Row Count 1 (+ 1) pass% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.4 cm} x{5.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Exception Handling}} \tn % Row 0 \SetRowColor{LightBackground} try: & The code inside the try block will run until it reaches an error. Once it does, it will be handled accordingly by the respective exception statements. \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} except Error: & The code inside this block will only run if the encountered error is the error to be handled by this except statement. \tn % Row Count 11 (+ 5) % Row 2 \SetRowColor{LightBackground} except Exception: & The code inside this block will only run if the error has not been handled by the previous except statements. \tn % Row Count 15 (+ 4) % Row 3 \SetRowColor{white} else: & The code inside the else block will only run if no errors were encountered in the try statement. \tn % Row Count 19 (+ 4) % Row 4 \SetRowColor{LightBackground} finally: & The code inside the finally block will run no matter what. \tn % Row Count 22 (+ 3) \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}{For/While Loop Statements}} \tn % Row 0 \SetRowColor{LightBackground} break & Terminates the loop (once) \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 current 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}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{List shallow copies (list.copy(), list{[}:{]})}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{8.4cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/otkl_1681908664_photo_6195168361302832224_y.jpg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.52 cm} x{3.04 cm} x{3.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{List and dictionary functions}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{string.split(sep)} & Splits the string into a list by the seperator, which by default is "". & \seqsplit{"CS1010S".split("1")} == {[}"CS", "0", "0S{]} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \seqsplit{dict.get(key}, value) & Returns the value that is paired to the key in the dictionary. If the key does not exist in the dictionary, then it will return the value instead. & d = \{"CS":10, 10: "S"\} ; d.get("CS", "NOT FUN") == 10; d.get("10", "GG CS") == "GG CS" \tn % Row Count 15 (+ 10) % Row 2 \SetRowColor{LightBackground} del dict{[}key{]} & Removes the key-value pair with the indicated key in the dictionary. Returns a KeyError if key is not found in the dict & d = \{"CS":10, 10: "S"\}; del d{[}10{]}; d = \{"CS": 10\} \tn % Row Count 23 (+ 8) % Row 3 \SetRowColor{white} \seqsplit{sep.join(iterable)} & Joins all the items in the iterable into a string, with the sep inbetween each item. & d = \{"CS":10, "10":"S"\}; " - ".join(d); "CS - 10" \tn % Row Count 29 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Passcode locking}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{def lock(obj, passcode): \newline % Row Count 1 (+ 1) o = obj.copy() \newline % Row Count 2 (+ 1) o.clear() \newline % Row Count 3 (+ 1) o{[}"locked"{]} = lambda x: o if x == passcode else False \newline % Row Count 5 (+ 2) def unlock(obj, passcode): \newline % Row Count 6 (+ 1) o = obj{[}"locked"{]} (passcode) \newline % Row Count 7 (+ 1) if o != False: \newline % Row Count 8 (+ 1) obj.clear() \newline % Row Count 9 (+ 1) obj.update(o)% Row Count 10 (+ 1) } \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}{Orders of Growth (OOG)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{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\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. \newline % Row Count 19 (+ 2) String slicing and concatenation takes O(n) time as well% Row Count 21 (+ 2) } \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}{Boolean Values}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{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 string, tuple, list, dict etc ("", (), {[}{]}, \{\}), value 0 and None all 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 8 (+ 6) } \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}{Checking data type}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1. type(value) == Type} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{2. isinstance(value, Type)} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}