\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{BioBoost} \pdfinfo{ /Title (pythomaniac.pdf) /Creator (Cheatography) /Author (BioBoost) /Subject (pythomaniac 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}{A30000} \definecolor{LightBackground}{HTML}{FCF7F7} \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{pythomaniac Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{BioBoost} via \textcolor{DarkBackground}{\uline{cheatography.com/218260/cs/48167/}}} \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}BioBoost \\ \uline{cheatography.com/bioboost} \\ \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 6th June, 2026.\\ 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{2.66 cm} x{2.736 cm} x{2.204 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{`\{\{lang-python\}\}def my\_function(first: int, second: str) -\textgreater{} float:`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `sum: Callable{[}{[}int, int{]}, int{]} = lambda x, y: x + y` & Lambda function & \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} `def foo(first: int, *rest: tuple) -\textgreater{} None:` & Variable function args (rest is a tuple) & Call as `foo(1, 2, 3)` \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} `def foo(first: int, **options: dict) -\textgreater{} None:` & Variable function args (options is a dict) & Call as `foo(1, sec=2, third=3)` \tn % Row Count 14 (+ 4) \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}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`mylist = {[}{]}`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`mylist.append()`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`for x in mylist:`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`if elem in mylist:`} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} `word\_len = {[}len(words) for word in words if word != "something"{]}` & List \seqsplit{comprehension} \tn % Row Count 7 (+ 3) % Row 5 \SetRowColor{white} `mylist{[}start:stop:step{]}` & List slice \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.812 cm} x{2.28 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Modules}} \tn % Row 0 \SetRowColor{LightBackground} `import my\_module` & Expects a \seqsplit{`my\_module.py`} & Usage: \seqsplit{`my\_module.some\_func()`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `from my\_module import some\_func` & Import specific from module & Usage: `my\_func()` \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} `from my\_module import *` & Not recommended & \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} `import my\_module as mine` & Alias & Usage: \seqsplit{`mine.my\_func()`} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{Each .py file is considered a module \newline \newline Modules are initialized only once at first encountered import. If imported again somewhere else, it will not be loaded anew. This is why local variables act as singletons.} \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}{Instance variables versus Class variables}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Dude: \newline count: int = 0 \newline \newline def \_\_init\_\_(self, name: str) -\textgreater{} None: \newline self.name = name \newline Dude.count = Dude.count + 1 \newline \newline print(Dude.count) \# 0 \newline dude = Dude("ThaDude") \newline print(Dude.count) \# 1 \newline king = Dude("King") \newline print(Dude.count) \# 2 \newline print(dude.count) \# 2 \newline print(king.count) \# 2 \newline \newline king.count = 666 \newline print(Dude.count) \# 2 \newline print(dude.count) \# 2 \newline print(king.count) \# 666 - king just got an instance variable count that takes precedence Dude.count} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{When no variable can be found with the given name on the instance, a fallback happens to the class variable. However when we assign a value as in `king.count = 2` we are creating an instance variable for that object. This is also called Name Shadowing} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Type Annotations}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`from typing import Callable`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`from typing import Generator`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `None | float` & Multiple possible types \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime. \newline \newline Adding type annotations is a huge advantage for anyone using your modules.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} `\%` & Modulo \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `**` & Power \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `+` & Addition, string concat, list concat \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} `"5" * 3` & `"555"` \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} `{[}5{]} * 3` & `{[}5, 5, 5{]}` \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} `//` & Floor division \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} `not`, `and`, `or` & Logical operators \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} `is`, `is not` & Identity operators \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} `in`, `not in` & Membership operators \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} `\&`, `|`, `\textasciitilde{}`, `\textasciicircum{}`, `\textgreater{}\textgreater{}`, `\textless{}\textless{}` & Bitwise operators \tn % Row Count 12 (+ 2) % Row 10 \SetRowColor{LightBackground} `x if condition else y` & Ternary operator \tn % Row Count 14 (+ 2) \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}{Useful Packages}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{dacite} & This module simplifies creation of data classes (PEP 557) from dictionaries. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{requests} & Requests is a simple, yet elegant, HTTP library. \tn % Row Count 5 (+ 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}{Main}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def main(): \newline print("Hello World") \newline \newline if \_\_name\_\_ == '\_\_main\_\_': \newline main()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{main() is only called when file is ran directly. \newline \newline If the file is imported into another module, `main()` is not called.} \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}{Closures}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{from typing import Callable \newline \newline def outer\_func(message: str) -\textgreater{} Callable{[}{[}{]}, None{]}: \newline def inner\_func() -\textgreater{} None: \newline print(message) \newline return inner\_func \newline \newline \# Calling \newline do\_print = outer\_func("Hello world") \newline print("Now we actually print ...") \newline do\_print()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{While the function `outer\_function()` completed, the `message` was rather preserved but hidden and attached to the code. \newline Decorators make heavy use of this.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Exception Handling}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`try:\{\{nl\}\} pass\{\{nl\}\}except Exception as e:\{\{nl\}\} pass\{\{nl\}\}finally:\{\{nl\}\} pass`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`e.add\_note("Some} info")` & Add some extra context info to exception \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `raise e` or `raise` & Reraise the exception \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `raise NewError from e` & Chained exception; indicate that NewError was caused by e \tn % Row Count 9 (+ 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}{Nested Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def outer\_func(message: str) -\textgreater{} None: \newline def inner\_func() -\textgreater{} None: \newline print(message) \newline \newline inner\_func()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Nested functions can access variables of enclosing scopes, but they are readonly. Can be circumvented using `nonlocal` keyword.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Loops}} \tn % Row 0 \SetRowColor{LightBackground} `for x in range(5):` & `range` returns iterator \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `break`, `continue` & Loop control \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `while, for:\{\{nl\}\} pass\{\{nl\}\}else:` & Alternative if loop condition false from beginning \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}{Classes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Dude: \newline def \_\_init\_\_(self, name: str) -\textgreater{} None: \newline self.name = name \newline \newline def introduce(self) -\textgreater{} str: \newline return f"Hello, I am \{self.name\}" \newline \newline my\_object = Dude("ThaDude") \newline print(my\_object.introduce())} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.2 cm} x{4.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Leftovers}} \tn % Row 0 \SetRowColor{LightBackground} `""`, `None`, `{[}{]}` and `\{\}` & These are all consider `False` in a logical expression \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.88 cm} x{3.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Input / Output}} \tn % Row 0 \SetRowColor{LightBackground} `print("Hello, \%s" \% name)` & C++ like printf() \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `print(f"Hello, \{name\}")` & In-place expressions \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `astring = input()` & Read till newline \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `anumber = int(input())` & Expects exactly 1 int \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `a, b = map(int, input().split())` & Expects exactly 2 ints \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`array = input().split()`} \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sets}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`a = set({[}"a", "b"{]})`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`a.intersection(b)`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`a.difference(b)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`a.union(b)`} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{No duplicate entries} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.4 cm} x{3.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{JSON}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`import json`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `json\_obj = \seqsplit{json.loads(json\_string)`} & Convert string to data object \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `json\_string = \seqsplit{json.dumps(json\_obj)`} & Convert data object to string \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Python supports a similar data serialization method called `pickle` which can be used in exactly the same way.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Code Introspection}} \tn % Row 0 \SetRowColor{LightBackground} `help()` & Show help on class, function, method, ... \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `dir()` & Return all the properties and methods, even built-in properties which are default for all object. \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{`hasattr()`} & Returns True if the specified object has the specified attribute \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} `id()` & Returns a unique id for the specified object. \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} `type()` & Returns the type of the specified object \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} `repr()` & Returns the canonical string representation of the object. \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{`callable()`} & Return whether the object is callable (i.e., some kind of function). \tn % Row Count 18 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{`issubclass()`} & Return whether 'cls' is derived from another class or is the same class. \tn % Row Count 21 (+ 3) % Row 8 \SetRowColor{LightBackground} \seqsplit{`isinstance()`} & Return whether an object is an instance of a class or of a subclass thereof. \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Remember that in python everything is an object.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.96 cm} x{3.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} `book = \{ "hello": 55 \}` & Initialize \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `book{[}"test"{]} = 22` & Assign element \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`for key,value in book.items():`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `del book{[}"test"{]}` & No return value \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} `book.pop("test")` & Returns value \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`if hello in book:`} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Key / Value storage. Key can also be anything} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Packages}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Needs `\_\_init\_\_.py` file} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `\_\_all\_\_ = {[} "public\_module" {]}` & Override exports and keep certain modules internal \tn % Row Count 4 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{A directory that get's its own namespace containing modules and other packages.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular Expressions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`import re`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`pattern = re.compile(r"...")`} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Todo: add search, replace, matching, ...} \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}{Generators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{import random \newline from typing import Generator \newline \newline def lottery() -\textgreater{} Generator{[}int, None, None{]}: \newline for i in range(6): \newline yield random.randint(1, 43) \newline yield random.randint(1, 20) \# Joker \newline \newline \# Usage: \newline for number in lottery(): \newline print(number)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Functions that returns an iterable set of items one at a time (yield).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Debugging}} \tn % Row 0 \SetRowColor{LightBackground} `s` & Execute the current line, stop at the first possible occasion \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `c` & Continue execution, only stop when a breakpoint is encountered. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `h` & Print help \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `p` & Print something like a variable \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Just add `breakpoint()` somewhere in code and when execution comes to this point you will be dropped to pdb, an interactive source code debugger.} \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}{Decorators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def simple\_decorator(func): \newline def wrapper(*args, **kwargs): \newline print("Before the function call") \newline func(*args, **kwargs) \newline print("After the function call") \newline return wrapper \newline \newline @simple\_decorator \newline def greet(name: str): \newline print(f"Hello there \{name\}!") \newline \newline greet("King") \newline \newline \# @decorator replaces this: \newline decorate = simple\_decorator(greet) \newline decorate("King")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are typically applied to functions, and they play a crucial role in enhancing or modifying the behavior of functions. \newline \newline When you create a decorator, the wrapper function (inside the decorator) is a closure. It retains access to the function being decorated and any additional state or arguments defined in the decorator function.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}