\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{luckytime} \pdfinfo{ /Title (python-121.pdf) /Creator (Cheatography) /Author (luckytime) /Subject (python 121 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 121 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{luckytime} via \textcolor{DarkBackground}{\uline{cheatography.com/214087/cs/46599/}}} \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}luckytime \\ \uline{cheatography.com/luckytime} \\ \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 18th June, 2025.\\ 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*}{4} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Files}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{file = open("data.txt") \# open file \newline % Row Count 1 (+ 1) contents = file.read() \# whole file as string \newline % Row Count 2 (+ 1) lines = file.readlines() \# all lines as list \newline % Row Count 3 (+ 1) line = file.readline() \# one line \newline % Row Count 4 (+ 1) for line in file: \# loop lines \newline % Row Count 5 (+ 1) line = line.strip() \# remove \textbackslash{}n and spaces \newline % Row Count 6 (+ 1) parts = line.split(",") \# split by comma \newline % Row Count 7 (+ 1) file.close() \# close file% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{annoying for loops}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{\# -{}-{}- Compact Python Cheat Sheet with Examples -{}-{}- \newline % Row Count 2 (+ 2) \# 1. In-place list update \newline % Row Count 3 (+ 1) def \seqsplit{threshold\_filter(readings}, threshold): \newline % Row Count 4 (+ 1) for i in range(len(readings)): \newline % Row Count 5 (+ 1) readings{[}i{]} = 0 if readings{[}i{]} \textless{} 0 else (threshold if readings{[}i{]} \textless{}= threshold else readings{[}i{]}) \newline % Row Count 8 (+ 3) r = {[}-3, 0, 2, 8, 15{]} \newline % Row Count 9 (+ 1) threshold\_filter(r, 5) \newline % Row Count 10 (+ 1) print(r) \# {[}0, 5, 5, 8, 15{]} \newline % Row Count 11 (+ 1) \# 2. Count words in order \newline % Row Count 12 (+ 1) def word\_counter(s): \newline % Row Count 13 (+ 1) counts = \{\} \newline % Row Count 14 (+ 1) for w in s.lower().split(): \newline % Row Count 15 (+ 1) counts{[}w{]} = counts.get(w, 0) + 1 \newline % Row Count 16 (+ 1) return counts \newline % Row Count 17 (+ 1) print(word\_counter("Hi there hi There")) \# \{'hi': 2, 'there': 2\} \newline % Row Count 19 (+ 2) \# 3. Sum list until threshold (while loop) \newline % Row Count 20 (+ 1) def sum\_list\_until(nums, t): \newline % Row Count 21 (+ 1) i, total = 0, 0 \newline % Row Count 22 (+ 1) while i \textless{} len(nums) and total \textless{} t: \newline % Row Count 23 (+ 1) total += nums{[}i{]} \newline % Row Count 24 (+ 1) i += 1 \newline % Row Count 25 (+ 1) return i, total \newline % Row Count 26 (+ 1) print(sum\_list\_until({[}5,6,6,4{]}, 17)) \# (3, 17) \newline % Row Count 27 (+ 1) \# 4. Running total with labels \newline % Row Count 28 (+ 1) def units\_available(data): \newline % Row Count 29 (+ 1) total = 0 \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{annoying for loops (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{ return {[}(total:=total+change, label) for change, label in data{]} \newline % Row Count 2 (+ 2) print(units\_available({[}(15,"Mon"), (-5,"Tue"), (0,"Wed"){]})) \newline % Row Count 4 (+ 2) \# {[}(15, 'Mon'), (10, 'Tue'), (10, 'Wed'){]} \newline % Row Count 5 (+ 1) \# 5. Index courses by keys \newline % Row Count 6 (+ 1) def index\_course(grades): \newline % Row Count 7 (+ 1) d = \{\} \newline % Row Count 8 (+ 1) for i, entry in enumerate(grades): \newline % Row Count 9 (+ 1) d.setdefault(entry{[}'course\_id'{]}, {[}{]}).append(i) \newline % Row Count 11 (+ 2) return d \newline % Row Count 12 (+ 1) grades = {[} \newline % Row Count 13 (+ 1) \{'student\_id':1,'course\_id':'cosc121','grade':'A'\}, \newline % Row Count 15 (+ 2) \{'student\_id':2,'course\_id':'cosc122','grade':'B'\}, \newline % Row Count 17 (+ 2) \{'student\_id':3,'course\_id':'cosc121','grade':'C'\} \newline % Row Count 19 (+ 2) {]} \newline % Row Count 20 (+ 1) \seqsplit{print(index\_course(grades))} \# \{'cosc121':{[}0,2{]}, 'cosc122':{[}1{]}\} \newline % Row Count 22 (+ 2) \# 6. Dict comprehension \newline % Row Count 23 (+ 1) words = {[}'hello', 'world'{]} \newline % Row Count 24 (+ 1) word\_lengths = \{w: len(w) for w in words\} \newline % Row Count 25 (+ 1) print(word\_lengths) \# \{'hello': 5, 'world': 5\} \newline % Row Count 26 (+ 1) \# 7. List comprehension with condition \newline % Row Count 27 (+ 1) thresholded = {[}x if x \textgreater{} 5 else 5 for x in {[}-2,4,8,10{]}{]} \newline % Row Count 29 (+ 2) print(thresholded) \# {[}5, 5, 8, 10{]} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{annoying for loops (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{\# 8. Using enumerate \newline % Row Count 1 (+ 1) for i, v in enumerate({[}'a','b','c'{]}): \newline % Row Count 2 (+ 1) print(i, v) \newline % Row Count 3 (+ 1) \# 0 a \newline % Row Count 4 (+ 1) \# 1 b \newline % Row Count 5 (+ 1) \# 2 c \newline % Row Count 6 (+ 1) \# 9. Sorted dict keys output \newline % Row Count 7 (+ 1) my\_dict = \{'b': 2, 'a': 1, 'c': 3\} \newline % Row Count 8 (+ 1) for k in sorted(my\_dict): \newline % Row Count 9 (+ 1) print(k, my\_dict{[}k{]}) \newline % Row Count 10 (+ 1) \# a 1 \newline % Row Count 11 (+ 1) \# b 2 \newline % Row Count 12 (+ 1) \# c 3% Row Count 13 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{exceptions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{except ZeroDivisionError:} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{except ValueError:} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{except IndexError:} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{except TypeError:} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.92248 cm} x{1.51052 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{list slicing}} \tn % Row 0 \SetRowColor{LightBackground} nums = {[}10, 20, 30, 40, 50, 60{]} & nums{[}1:4{]} {[}20, 30, 40{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} nums{[}:3{]}. {[}10, 20, 30{]} & nums{[}-2:{]} last 2 items \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} nums{[}:-1{]} all but last & nums{[}::2{]} every 2nd item \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{nums{[}::-1{]}. reverse} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.75083 cm} x{1.68217 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{If statment}} \tn % Row 0 \SetRowColor{LightBackground} if num \% 2 == 0: \#even & if num \% 2 != 0: \#odd \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} 6 / 2 \#3.0 & 7 // 2 \#3 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} 7 \% 2 \#1 & 3 ** 2 \#9 \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{matplotlib}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{import matplotlib.pyplot as plt \newline % Row Count 1 (+ 1) import numpy as np \newline % Row Count 2 (+ 1) \# ======= BASIC LINE PLOT ======= \newline % Row Count 3 (+ 1) x = np.linspace(0, 10, 100) \newline % Row Count 4 (+ 1) y = np.sin(x) \newline % Row Count 5 (+ 1) plt.plot(x, y, label='sin(x)') \# Line plot \newline % Row Count 6 (+ 1) plt.title("Sine Curve") \# Title \newline % Row Count 7 (+ 1) plt.xlabel("x-axis") \# X label \newline % Row Count 8 (+ 1) plt.ylabel("y-axis") \# Y label \newline % Row Count 9 (+ 1) plt.grid(True) \# Show grid \newline % Row Count 10 (+ 1) plt.legend() \# Show legend \newline % Row Count 12 (+ 2) plt.xlim(0, 10) \# Limit x-axis \newline % Row Count 14 (+ 2) plt.ylim(-1, 1) \# Limit y-axis \newline % Row Count 16 (+ 2) plt.xticks({[}0, 5, 10{]}) \# Custom x ticks \newline % Row Count 18 (+ 2) plt.yticks({[}-1, 0, 1{]}) \# Custom y ticks \newline % Row Count 20 (+ 2) \seqsplit{plt.savefig("sine\_plot.png")} \# Save to file \newline % Row Count 22 (+ 2) plt.show() \# Show the plot \newline % Row Count 24 (+ 2) \# ======= SCATTER PLOT ======= \newline % Row Count 25 (+ 1) x = np.random.rand(50) \newline % Row Count 26 (+ 1) y = np.random.rand(50) \newline % Row Count 27 (+ 1) plt.scatter(x, y) \newline % Row Count 28 (+ 1) plt.title("Random Scatter") \newline % Row Count 29 (+ 1) plt.show() \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{matplotlib (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{\# ======= BAR CHART ======= \newline % Row Count 1 (+ 1) categories = {[}'A', 'B', 'C'{]} \newline % Row Count 2 (+ 1) values = {[}10, 20, 15{]} \newline % Row Count 3 (+ 1) plt.bar(categories, values) \newline % Row Count 4 (+ 1) plt.title("Bar Chart") \newline % Row Count 5 (+ 1) plt.show() \newline % Row Count 6 (+ 1) \# ======= HISTOGRAM ======= \newline % Row Count 7 (+ 1) data = np.random.randn(1000) \newline % Row Count 8 (+ 1) plt.hist(data, bins=30) \newline % Row Count 9 (+ 1) plt.title("Histogram") \newline % Row Count 10 (+ 1) plt.show() \newline % Row Count 11 (+ 1) \# ======= CLEARING / CLOSING ======= \newline % Row Count 12 (+ 1) plt.clf() \# Clear current figure (if needed) \newline % Row Count 13 (+ 1) plt.close() \# Close the plot window% Row Count 14 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{For Loops}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{for i in range(5): print(i) \# 0 1 2 3 4 \newline % Row Count 2 (+ 2) for i in range(2,6): print(i) \# 2 3 4 5 \newline % Row Count 3 (+ 1) for i in range(1,10,3): print(i) \# 1 4 7 \newline % Row Count 4 (+ 1) lst={[}'a','b','c'{]} \newline % Row Count 5 (+ 1) for i in range(len(lst)): print(i,lst{[}i{]}) \# 0 a 1 b 2 c \newline % Row Count 7 (+ 2) for item in lst: print(item) \# a b c \newline % Row Count 9 (+ 2) for i,item in enumerate(lst): print(i,item) \# 0 a 1 b 2 c% Row Count 11 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{switching while loop to for loop}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{\# For loops \newline % Row Count 1 (+ 1) for op1 in range(0, value1): \newline % Row Count 2 (+ 1) for op2 in range(value1, value2, -1): \newline % Row Count 3 (+ 1) print(f"\{op1:3\} x \{op2:3\} = \{op1*op2:3\}") \newline % Row Count 4 (+ 1) \# Equivalent while loops \newline % Row Count 5 (+ 1) op1 = 0 \newline % Row Count 6 (+ 1) while op1 \textless{} value1: \newline % Row Count 7 (+ 1) op2 = value1 \newline % Row Count 8 (+ 1) while op2 \textgreater{} value2: \newline % Row Count 9 (+ 1) print(f"\{op1:3\} x \{op2:3\} = \{op1*op2:3\}") \newline % Row Count 10 (+ 1) op2 -= 1 \newline % Row Count 11 (+ 1) op1 += 1% Row Count 12 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{annoying for loop on dict}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{\# for key, value in dict.items() \newline % Row Count 1 (+ 1) d = \{'a':1,'b':2\} \newline % Row Count 2 (+ 1) for k,v in d.items(): print(k,v) \newline % Row Count 3 (+ 1) \# Before: d = \{'a':1,'b':2\} \newline % Row Count 4 (+ 1) \# After: \newline % Row Count 5 (+ 1) \# a 1 \newline % Row Count 6 (+ 1) \# b 2 \newline % Row Count 7 (+ 1) \# for word in string.split() \newline % Row Count 8 (+ 1) s = " hello world " \newline % Row Count 9 (+ 1) for w in s.strip().split(): print(w) \newline % Row Count 10 (+ 1) \# Before: " hello world " \newline % Row Count 11 (+ 1) \# After: \newline % Row Count 12 (+ 1) \# hello \newline % Row Count 13 (+ 1) \# world \newline % Row Count 14 (+ 1) \# for i in range(len(list)) \newline % Row Count 15 (+ 1) lst = {[}'x','y'{]} \newline % Row Count 16 (+ 1) for i in range(len(lst)): print(i,lst{[}i{]}) \newline % Row Count 17 (+ 1) \# Before: {[}'x','y'{]} \newline % Row Count 18 (+ 1) \# After: \newline % Row Count 19 (+ 1) \# 0 x \newline % Row Count 20 (+ 1) \# 1 y \newline % Row Count 21 (+ 1) \# for i in range(start, stop, step) \newline % Row Count 22 (+ 1) for i in range(1,6,2): print(i) \newline % Row Count 23 (+ 1) \# Before: range(1,6,2) \newline % Row Count 24 (+ 1) \# After: \newline % Row Count 25 (+ 1) \# 1 \newline % Row Count 26 (+ 1) \# 3 \newline % Row Count 27 (+ 1) \# 5 \newline % Row Count 28 (+ 1) \# for i, val in enumerate(list) \newline % Row Count 29 (+ 1) lst = {[}'a','b'{]} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{annoying for loop on dict (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{for i,v in enumerate(lst): print(i,v) \newline % Row Count 1 (+ 1) \# Before: {[}'a','b'{]} \newline % Row Count 2 (+ 1) \# After: \newline % Row Count 3 (+ 1) \# 0 a \newline % Row Count 4 (+ 1) \# 1 b% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Sets and Dictionaries}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{\# Sets from list and operations \newline % Row Count 1 (+ 1) lst={[}1,2,2,3,4,4{]} \newline % Row Count 2 (+ 1) s=set(lst) \# \{1,2,3,4\} \newline % Row Count 3 (+ 1) s.add(5) \# \{1,2,3,4,5\} \newline % Row Count 4 (+ 1) s.remove(2) \# \{1,3,4,5\} \newline % Row Count 5 (+ 1) s.discard(6) \# \{1,3,4,5\} no error \newline % Row Count 6 (+ 1) print(3 in s) \# True \newline % Row Count 7 (+ 1) s2=\{3,4,5,6\} \newline % Row Count 8 (+ 1) print(s.union(s2)) \# \{1,3,4,5,6\} \newline % Row Count 9 (+ 1) \seqsplit{print(s.intersection(s2))\#} \{3,4,5\} \newline % Row Count 10 (+ 1) print(s.difference(s2)) \# \{1\} \newline % Row Count 11 (+ 1) print(s.issubset(s2)) \# False \newline % Row Count 12 (+ 1) for x in s: print(x) \# 1 3 4 5 (any order) \newline % Row Count 13 (+ 1) \# Dictionaries and methods \newline % Row Count 14 (+ 1) d=\{"a":1,"b":2\} \newline % Row Count 15 (+ 1) print(d.keys()) \# dict\_keys({[}'a','b'{]}) \newline % Row Count 16 (+ 1) print(d.values()) \# dict\_values({[}1,2{]}) \newline % Row Count 17 (+ 1) print(d.items()) \# dict\_items({[}('a',1),('b',2){]}) \newline % Row Count 19 (+ 2) print(d.get("a")) \# 1 \newline % Row Count 20 (+ 1) print(d.get("z",0)) \# 0 default if missing \newline % Row Count 21 (+ 1) d{[}"c"{]}=3 \newline % Row Count 22 (+ 1) for k in d: print(k) \# a b c \newline % Row Count 23 (+ 1) for v in d.values(): print(v) \# 1 2 3 \newline % Row Count 24 (+ 1) for k,v in d.items(): print(k,v) \# a 1 b 2 c 3% Row Count 25 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{2.02547 cm} x{1.40753 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} fruits = {[}"apple", "banana", "cherry"{]} & fruits{[}0{]} \#apple \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} fruits{[}-1{]} \#cherry & len(fruits) \#3 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} fruits.append("kiwi") & fruits.insert(1, "pear") \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{fruits.remove("banana")} & fruits.pop() \#removes last \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} fruits.sort() & sorted(fruits) \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}