\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{leenmajz} \pdfinfo{ /Title (python-basics.pdf) /Creator (Cheatography) /Author (leenmajz) /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}{AD496E} \definecolor{LightBackground}{HTML}{F9F3F5} \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}{leenmajz} via \textcolor{DarkBackground}{\uline{cheatography.com/198109/cs/42043/}}} \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}leenmajz \\ \uline{cheatography.com/leenmajz} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 14th January, 2024.\\ Updated 17th January, 2024.\\ 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{5.2 cm} x{2.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Transforming Number Bases}} \tn % Row 0 \SetRowColor{LightBackground} Transform a number string of base a to a decimanl integer & int(string, a) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Transform an integer n to binary, octidecimal or hexidecimal & bin(n), oct(n), hex(n) \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.56 cm} x{3.44 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Generating Series of numbers}} \tn % Row 0 \SetRowColor{LightBackground} {[}0,a) & range(a) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {[}1,a) & range(1,a) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {[}a,b) skipping c & range(a,b,c) \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.52 cm} x{2.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} Input returns a string & \seqsplit{input('prompt')} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Tab character & \textbackslash{}t \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} New line character & \textbackslash{}n \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Quotation mark character & \textbackslash{}" \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} Backslash character & \textbackslash{}\textbackslash{} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} String index {[}a,b) with c steps & string{[}a:b:c{]} \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} Length including tabs, newlines,.. & len(string) \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} Converts a char into an order & ord("char") \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} Converts an order into a char & chr(number) \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.04 cm} x{2.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Methods}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Case Modification}}\{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{str.upper(), str.lower(), str.capitalize(), str.title() \{\{colspan=2\}\}} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{String Searching and Counting}}\{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Count occurrences & \seqsplit{str.count("s")} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} If not found, returns -1 & str.find("s") \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} If not found, returns ValueError & \seqsplit{str.index("s")} \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} finds last occurence & \seqsplit{str.rfind("s")} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Stripping Whitespace}} \{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{str.strip(), str.lstrip(), str.rstrip()\{\{colspan=2\}\}} \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Replacement and Modification}}\{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} Expand tabs & \seqsplit{str.expandtabs()} \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} Replace a with b & str.replace(a,b) \tn % Row Count 19 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Splitting and joining}}\{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 20 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{str.split(), " ".join(list)\{\{colspan=2\}\}} \tn % Row Count 21 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Alignment and formatting}}\{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 22 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{str.center(20, "-"), str.ljust(20, "-"), str.rjust(20, "-"), str.zfill(20)\{\{colspan=2\}\}} \tn % Row Count 25 (+ 3) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Prefix and suffix removal}} \{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 26 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{str.removeprefix("p"), str.removesuffix("s") \{\{colspan=2\}\}} \tn % Row Count 28 (+ 2) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Testing strings:}} \{\{ac\}\}(returns true or false) \{\{colspan=2\}\}} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{5.04 cm} x{2.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Methods (cont)}} \tn % Row 19 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{str.isalpha(), str.istitle()\{\{colspan=2\}\}} \tn % Row Count 1 (+ 1) % Row 20 \SetRowColor{white} Includes non-ASCII digits & \seqsplit{str.isnumeric()} \tn % Row Count 3 (+ 2) % Row 21 \SetRowColor{LightBackground} Only ASCII digits & str.isdigit() \tn % Row Count 5 (+ 2) % Row 22 \SetRowColor{white} Contains num or alpha or both & str.isalnum() \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.8 cm} x{3.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Basic Programming Constructs}} \tn % Row 0 \SetRowColor{LightBackground} Conditional Execution & if, elif, else \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Iterative Execution & while, while not \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Flags & active = True \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} & while active: \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Count-controlled loop & for \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{More stamenents}}\{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} Leaves the loop & break \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} returns to the beginning & continue \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} null operation & pass \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{7.12 cm} p{0.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{F Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{f"\{expression\} this is a string" \{\{colspan=2\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Break the statement into multiple lines & \textbackslash{} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Braces can be included using double braces & \{\{ \}\} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Expressions should not use comments using \#, nor you can use backslashes to escape in the expression} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.16 cm} x{3.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Adding or changing elements}} \{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{list.append(x), list.extend(list2), list.insert(i, x), list{[}i{]}=x \{\{colspan=2\}\}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Removing elements}}\{\{ac\}\} \{\{colspan=2\}\}} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} Removes the first occurence of x & list.remove(x) \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Removes and returns {[}i{]} element & list.pop(i) \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} Removes all elements from the list & list.clear() \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} Deletes {[}i{]} element & del list{[}i{]} \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Searching and counting}} \{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} Returns the index of first x occurence between {[}a,b) indices and returns "ValueError" if x is not found & list.index(x,a,b) \tn % Row Count 19 (+ 6) % Row 9 \SetRowColor{white} Counts the occurences of "x" & list.count("x") \tn % Row Count 21 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Sorting}}\{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 22 (+ 1) % Row 11 \SetRowColor{white} Modifies original list and retursn none, key = lambda, len, function\_name & list.sort(key=None, reverse=False) \tn % Row Count 26 (+ 4) % Row 12 \SetRowColor{LightBackground} Returns a new sorted list & sorted(list, key=None, reverse=False) \tn % Row Count 28 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{List membership}}\{\{ac\}\} \{\{colspan=2\}\}} \tn % Row Count 29 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{"x in list", "x not in list" \{\{colspan=2\}\}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4.16 cm} x{3.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Iterating through a list}} \{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 1 (+ 1) % Row 16 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{for x in list: \{\{colspan=2\}\}} \tn % Row Count 2 (+ 1) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{for i,x in enumerate(list): \{\{colspan=2\}\}} \tn % Row Count 3 (+ 1) % Row 18 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{[}expression for x in list if condition{]} \{\{colspan=2\}\}} \tn % Row Count 5 (+ 2) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{[}expression if condition else expression2 for x in list{]} \{\{colspan=2\}\}} \tn % Row Count 7 (+ 2) % Row 20 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{[}expression for x in {[}expression2 for y in list{]}{]} \{\{colspan=2\}\}} \tn % Row Count 9 (+ 2) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{for x,y in zip(list2,list2): \{\{colspan=2\}\}} \tn % Row Count 10 (+ 1) % Row 22 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Transposing the matrix}}\{\{ac\}\} \{\{colspan=2\}\}} \tn % Row Count 11 (+ 1) % Row 23 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{t\_matrix = zip(*matrix) \{\{colspan=2\}\}} \tn % Row Count 12 (+ 1) % Row 24 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Other operaters}}\{\{ac\}\} \{\{colspan=2\}\}} \tn % Row Count 13 (+ 1) % Row 25 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{list.reverse(), len(list), list.copy(), min(list), max(list), y = {[}{]}+x, y= x{[}:{]}, y=list(x) \{\{colspan=2\}\}} \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.08 cm} x{3.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sets}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Set creation}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{set = \{1,2,3\}, set() \{\{colspan=2\}\}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Adding/removing elements}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{set.add(a), set.update(set2), set.clear() \{\{colspan=2\}\}} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} Raises a KeyError if a is not found & set.remove(a) \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} Does not give key error & set.discard(a) \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} Removes and returns a, returns KeyError if a not found & set.pop(a) \tn % Row Count 13 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Set operations}} \{\{ac\}\}\{\{colspan=2\}\}} \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} Set1USet2 & set1.union(set2) \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} & set1|set2 \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} Set1 and Set2 & \seqsplit{set1.intersection(set2)} \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} & set1 \& set2 \tn % Row Count 19 (+ 1) % Row 12 \SetRowColor{LightBackground} All in Set1 but not in Set2 & \seqsplit{set1.difference(set2)} \tn % Row Count 21 (+ 2) % Row 13 \SetRowColor{white} & set1 - set2 \tn % Row Count 22 (+ 1) % Row 14 \SetRowColor{LightBackground} In Set1 and Set2 but not in both & \seqsplit{set1.symmertric\_difference(set2)} \tn % Row Count 24 (+ 2) % Row 15 \SetRowColor{white} & set1 \textasciicircum{} set2 \tn % Row Count 25 (+ 1) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Set comparison}}\{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 26 (+ 1) % Row 17 \SetRowColor{white} True if set1 is subset of set2 & \seqsplit{set1.issubset(set2)} \tn % Row Count 28 (+ 2) % Row 18 \SetRowColor{LightBackground} True if set1 is superset of set2 & \seqsplit{set1.issuperset(set2)} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4.08 cm} x{3.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sets (cont)}} \tn % Row 19 \SetRowColor{LightBackground} True if set1 has no elements in common with set2 & \seqsplit{set1.isdisjoint(set2)} \tn % Row Count 3 (+ 3) % Row 20 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Set membership}}\{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 4 (+ 1) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{x in set, x not in set\{\{colspan=2\}\}} \tn % Row Count 5 (+ 1) % Row 22 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Looping through sets}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 6 (+ 1) % Row 23 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{for x in set\{\{colspan=2\}\}} \tn % Row Count 7 (+ 1) % Row 24 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{statement for x in set\} \{\{colspan=2\}\}} \tn % Row Count 8 (+ 1) % Row 25 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Set updates}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 9 (+ 1) % Row 26 \SetRowColor{white} removes elements from set1 that are also in set2 & \seqsplit{set1.difference\_update(set2)} \tn % Row Count 12 (+ 3) % Row 27 \SetRowColor{LightBackground} keeps only the intersection & \seqsplit{set1.intersection\_update(set2)} \tn % Row Count 14 (+ 2) % Row 28 \SetRowColor{white} keeps only the symmetric difference & \seqsplit{set1.symmetric\_difference\_update(set2)} \tn % Row Count 16 (+ 2) % Row 29 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{More operations}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 17 (+ 1) % Row 30 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{set.copy(), len(set), min(set), max(set) \{\{colspan=2\}\}} \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{6 cm} x{2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Tuples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Tuple Creation}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{tuple = (1,2,3), single\_tuple = (1,), tuple() \{\{colspan=2\}\}} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Accessing elements}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} & tuple{[}i{]} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} {[}a,b) & tuple{[}a:b{]} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Tuple unpacking}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} unpacks elements into separate variables a,b,c & a,b,c = tuple \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Checking membership}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{x in tuple, x not in tuple\{\{colspan=2\}\}} \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Miscellaneous}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{len(tuple), tuple(sorted(my\_tuple))\{\{colspan=2\}\}} \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Named tuples}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{from collections import namedtuple \{\{colspan=2\}\}} \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Person = namedtuple('Person', {[}'name', 'age'{]} \{\{colspan=2\}\}} \tn % Row Count 17 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{alice = Person('Alice',30) \{\{colspan=2\}\}} \tn % Row Count 18 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{alice.name, alice.age \{\{colspan=2\}\}} \tn % Row Count 19 (+ 1) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Tuple methods}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 20 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{tuple.count(a), tuple.index(a) \{\{colspan=2\}\}} \tn % Row Count 21 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Concatenation}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 22 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{tuple1 + tuple2 \{\{colspan=2\}\}} \tn % Row Count 23 (+ 1) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Looping in a tuple}}\{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 24 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{for x in tuple: \{\{colspan=2\}\}} \tn % Row Count 25 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{for i, x in enumerate(tuple): \{\{colspan=2\}\}} \tn % Row Count 26 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{new\_tuple = tuple(expression for x in tuple) \{\{colspan=2\}\}} \tn % Row Count 28 (+ 2) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{for x in zip(list1,list2): \{\{colspan=2\}\}} \tn % Row Count 29 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Tuples are immutable. Once created, their elements cannot be changed or added.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.32 cm} x{3.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionary}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Dictionary Creation}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{dic = \{"key": "value"\}, dict(), dict(zip(list1,list2))\{\{colspan=2\}\}} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Accessing Values}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Returns "KeyError" if key is not found & dic{[}key{]} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} Returns a default\_value if key is not found & dic.get(key, default\_value) \tn % Row Count 9 (+ 3) % Row 5 \SetRowColor{white} Returns value, if key not present, adds the key with default\_value and returns default\_value & \seqsplit{dic.setdefault(key}, default\_value) \tn % Row Count 14 (+ 5) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Updating Values}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 15 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{dic{[}key{]}=new\_value \{\{colspan=2\}\}} \tn % Row Count 16 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Methods}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 17 (+ 1) % Row 9 \SetRowColor{white} returns a view of all keys & dic.keys() \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} returns a view of all values & dic.values() \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} returms a view of all key-value pairs & dic.items() \tn % Row Count 23 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Adding/removing elements}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 24 (+ 1) % Row 13 \SetRowColor{white} & dic{[}new\_key{]}=new\_value \tn % Row Count 26 (+ 2) % Row 14 \SetRowColor{LightBackground} removes and return the value deleted, KeyError if key not found & dic.pop(key) \tn % Row Count 29 (+ 3) % Row 15 \SetRowColor{white} removes and returns last key-value pair as a tuple, KeyError if dic is empty & dic.popitem() \tn % Row Count 33 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4.32 cm} x{3.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionary (cont)}} \tn % Row 16 \SetRowColor{LightBackground} deletes the key-value pair & del dic{[}key{]} \tn % Row Count 2 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Dictionary size}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 3 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{len(dic) \{\{colspan=2\}\}} \tn % Row Count 4 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{More methods}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 5 (+ 1) % Row 20 \SetRowColor{LightBackground} Checking membership & key in dic, key not in dic \tn % Row Count 7 (+ 2) % Row 21 \SetRowColor{white} Clearing dictionary & dic.clear() \tn % Row Count 8 (+ 1) % Row 22 \SetRowColor{LightBackground} Copying & dic.copy() \tn % Row Count 9 (+ 1) % Row 23 \SetRowColor{white} Adds new keys and updates existing keys & dic.update(dic2) \tn % Row Count 11 (+ 2) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Looping}} \{\{colspan=2\}\}\{\{ac\}\}} \tn % Row Count 12 (+ 1) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{for key in dic \{\{colspan=2\}\}} \tn % Row Count 13 (+ 1) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{for value in dic.values() \{\{colspan=2\}\}} \tn % Row Count 14 (+ 1) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{for key,value in dic.items():\{\{colspan=2\}\}} \tn % Row Count 15 (+ 1) % Row 28 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{key,value for key,value in dic.items\} \{\{colspan=2\}\}} \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Importing Modules}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Standard import}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{import module\_name} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{module\_name.function\_name()}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Alias import}}} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{import module\_name as a} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{a.func()} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Dynamic import}}} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{a = \_ \_ import \_ \_ ("module\_name")} \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{a.func()} \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Importing and renaming within a module}}} \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{from module\_name import function\_name as func} \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{func()} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Defining a function}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{def \seqsplit{function\_name(parameter1}, parameter2):} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{statement} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{return result} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Calling a function}}} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{print(function\_name(a,b))} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Packing arguments}}} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{def function\_name(*arg):} \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{function\_name(a,b,c,d)} \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Unpacking arguments}}} \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def func(a,b,c,d):} \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{func(*tuple)} \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Packing a dictionary}}} \tn % Row Count 13 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{def func(**kwargs):} \tn % Row Count 14 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func(key1 = value1, key2 = value2)} \tn % Row Count 15 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Global and local variable}}} \tn % Row Count 16 (+ 1) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{x = "global variable"} \tn % Row Count 17 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{def func():} \tn % Row Count 18 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\_\_\_\_\_global x \# Change x outside the func to be the x inside the func} \tn % Row Count 20 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\_\_\_\_\_x ="local variable"} \tn % Row Count 21 (+ 1) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Function documentation AKA Docstring}}} \tn % Row Count 22 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{def func():} \tn % Row Count 23 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\_\_\_\_\_" " "This is a docstring" " "} \tn % Row Count 24 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{ac\}\} {\bf{Help}}} \tn % Row Count 25 (+ 1) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{help(func)} \tn % Row Count 26 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lambda Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Basic}} \newline % Row Count 1 (+ 1) square = lambda x: x\textasciicircum{}2 \newline % Row Count 2 (+ 1) square(2) \newline % Row Count 3 (+ 1) {\bf{Default argument}} \newline % Row Count 4 (+ 1) lambda x, y = a: x*y% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}