\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{LoiVyen} \pdfinfo{ /Title (my-python-modules-reference.pdf) /Creator (Cheatography) /Author (LoiVyen) /Subject (My Python Modules Reference 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}{E3CE49} \definecolor{LightBackground}{HTML}{FBF8E8} \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{My Python Modules Reference Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{LoiVyen} via \textcolor{DarkBackground}{\uline{cheatography.com/163788/cs/34340/}}} \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}LoiVyen \\ \uline{cheatography.com/loivyen} \\ \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 8th February, 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Pandas}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`import pandas as pd`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{mydataset = \{ 'cars': {[}"BMW", "Volvo", "Ford"{]}, \{\{nl\}\}~~~~'passings': {[}3, 7, 2{]} \{\{nl\}\}\} \{\{nl\}\}myvar = pd.DataFrame(mydataset) print(myvar)} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`~~~~~~cars~~~~passings \{\{nl\}\}0 ~~~~BMW ~~~~~3 \{\{nl\}\}1 ~~~~Volvo ~~~7 \{\{nl\}\}2 ~~~~Ford ~~~~2`} \tn % Row Count 11 (+ 6) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{df = pd.read\_csv('data.csv') \{\{nl\}\}print(df)} \tn % Row Count 12 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{df = \seqsplit{pd.read\_csv(filepath\_or\_buffer="./testdata.csv"},sep=",",dtype=str)} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Pandas is a library used for working with data sets. \newline functions: analyzing, cleaning, exploring, and manipulating data. \newline https://pandas.pydata.org/docs/reference/ \newline https://pandas.pydata.org/docs/reference/api/pandas.read\_csv.html} \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}{PyPDF2}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`from PyPDF2 import PdfFileReader`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`def text\_extractor(path): \{\{nl\}\}~~~~with open(path, 'rb') as f: \{\{nl\}\}~~~~~~~~pdf = PdfFileReader(f)` \# get the first page` \{\{nl\}\}~~~~~~~~page = pdf.getPage(1) \{\{nl\}\}~~~~~~~~print("page : \textbackslash{}n ", page) \{\{nl\}\}~~~~~~~~print('\textbackslash{}n Page type: \{\}'.format(str(type(page)))) \{\{nl\}\}~~~~~~~~text = page.extractText() \{\{nl\}\}~~~~~~~~print("page text: ", text)`} \tn % Row Count 15 (+ 14) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`if \_\_name\_\_ == '\_\_main\_\_': \{\{nl\}\}~~~~path = 'filename.pdf' ` \# enter file for input ` \{\{nl\}\}~~~~text\_extractor(path)`} \tn % Row Count 19 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{extract metadata, extract text, split/merge pdfs, rotate pages, encryption: \newline https://www.blog.pythonlibrary.org/2018/06/07/an-intro-to-pypdf2/} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.2 cm} x{2.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{icecream}} \tn % Row 0 \SetRowColor{LightBackground} `from icecream import ic` & {\bf{output}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `def foo(i): \{\{nl\}\}~~~~return i + 333` & ic| foo(123): 456 \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} `d = \{'key': \{1: 'one'\}\} \{\{nl\}\}ic(d{[}'key'{]}{[}1{]})` & ic| d{[}'key'{]}{[}1{]}: 'one' \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `class klass(): \{\{nl\}\}~~~~attr = 'yep' \{\{nl\}\}ic(klass.attr)` & ic| klass.attr: 'yep' \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{to use instead of print and log for debugging \newline https://github.com/gruns/icecream} \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}{Extract Text From HTML Pages / Websites}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`conda install -c conda-forge readability-lxml`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`import requests \{\{nl\}\}from readability import Document \{\{nl\}\}response = \seqsplit{requests.get('http://example.com')} \{\{nl\}\}doc = Document(response.text) \{\{nl\}\}print(doc.title()) \{\{nl\}\} \{\{nl\}\}print(doc.summary())`} \tn % Row Count 6 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Example Domain \newline \textless{}html\textgreater{}\textless{}body\textgreater{}\textless{}div\textgreater{}\textless{}body id="readabilityBody"\textgreater{} \newline \textless{}div\textgreater{} \newline \textless{}h1\textgreater{}Example Domain\textless{}/h1\textgreater{} \newline \textless{}p\textgreater{}This domain is for use in illustrative examples in documents. You may use this \newline domain in literature without prior coordination or asking for permission.\textless{}/p\textgreater{} \newline \textless{}p\textgreater{}\textless{}a href="https://www.iana.org/domains/example"\textgreater{}More information...\textless{}/a\textgreater{}\textless{}/p\textgreater{} \newline \textless{}/div\textgreater{} \newline \textless{}/body\textgreater{} \newline \textless{}/div\textgreater{}\textless{}/body\textgreater{}\textless{}/html\textgreater{}} \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}{Beautiful Soup (XML extractor)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Beautiful Soup is a Python library for pulling data out of HTML and XML files.}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`conda install -c conda-forge beautifulsoup4`} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`from bs4 import BeautifulSoup \{\{nl\}\}html\_doc = str(doc.summary()) \{\{nl\}\}soup = BeautifulSoup(html\_doc, 'html.parser') \{\{nl\}\}print(html\_doc) `\# prints with tags` \{\{nl\}\}print(soup.prettify()) `\# formats string from html tags` \{\{nl\}\} \{\{nl\}\}soup = BeautifulSoup(html\_doc, features="lxml") \{\{nl\}\}print(soup.get\_text())`} \tn % Row Count 10 (+ 7) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\# BeautifulSoup ( html\_string, parser\_type), lxml is a python module} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Example Domain \newline This domain is for use in illustrative examples in documents. You may use this \newline domain in literature without prior coordination or asking for permission. \newline More information... \newline \newline https://www.crummy.com/software/BeautifulSoup/bs4/doc/} \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}{Keywords: assert try except}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`x = "goodbye"` \{\{nl\}\}if condition returns False, AssertionError is raised: \{\{nl\}\}`assert x == "hello"` \{\{nl\}\}{\bf{or}} to add a message to console: \{\{nl\}\}`assert x == "hello", "should be goodbye"`} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{try except block with AssertionError}}} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`x = 'hello' \{\{nl\}\}try: \{\{nl\}\}~~~~assert x == 'goodbye' \{\{nl\}\}except: \{\{nl\}\}~~~~print("An exception occurred")`\{\{nl\}\}(so that the program continues running after the error)} \tn % Row Count 10 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://www.w3schools.com/python/ref\_keyword\_assert.asp} \newline The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. \newline \newline {\bf{Try / Except / Catch differences}} \newline try contains the code that may raise exceptions or errors. \newline except is used to catch the exceptions and handle them. \newline catch code is executed only when the corresponding exception is raised.} \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}{One Line Loops}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{examples: }}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`for i in range(10): print(i)` \newline `print({[}i{\bf{2 for i in range(10) if i\%2==0{]})` \newline `for i in range(10): print(i}}2 if i\textless{}5 else 0)` \newline `print("Text: ", {[}token.text for token in doc{]})`} \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}{Formatting Strings with Variables}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Example:}}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`quantity = 3` \newline `itemno = 567` \newline `price = 49.95` \newline `myorder = "I will pay \{2\} dollars for \{0\} pieces of item \{1\}."` \newline `print(myorder.format(quantity, itemno, price))`} \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}{Read Write Print Files}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{open file and read lines with formatting}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`with open("textfile.txt", encoding = 'utf-8') as f: \{\{nl\}\}~~~~lines = f.readlines() `\# as a list ` \{\{nl\}\}~~~~docs = list(nlp.pipe(lines)) `} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{(to spaCy docs)}}\{\{nl\}\}`for text in docs:` \#iterate each list item `\{\{nl\}\}~~~~for firsts in text: `\# iterate each word in list (sentence) ` \{\{nl\}\}~~~~~~~~if firsts.is\_sent\_start: \{\{nl\}\}~~~~~~~~~~~~print(firsts.text)`} \tn % Row Count 13 (+ 8) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`doc = {[}nlp(txt.strip()) for txt in lines{]}` \{\{nl\}\}\# removes escape sequences} \tn % Row Count 15 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{or}}} \tn % Row Count 16 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`f = open("Handbooklist.txt", "r") \{\{nl\}\}print(f.read()) `} \tn % Row Count 18 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`f = open("demofile.txt", "r") \{\{nl\}\}print(f.readline()) ` \# read first line} \tn % Row Count 20 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`f = open("demo.txt", 'r', encoding='utf8') \{\{nl\}\}print(f.read()) ` \# \textasciicircum{} another way to strip} \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`f = open("demo.txt", "r") \{\{nl\}\}for x in f: \{\{nl\}\}~~~~print(x) ` \{\{nl\}\}~~~~ \# and/or \{\{nl\}\}~~~~`links.append(x.strip()) ` \# declare arr beforehand; links = {[} {]}} \tn % Row Count 27 (+ 5) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`f.close() ` \# best to close when done unless first option used} \tn % Row Count 29 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Write Files}} {\emph{('w' overwrites; 'a' appends file)}}} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Read Write Print Files (cont)}} \tn % Row 11 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`f = open("myfile.txt", "w") \{\{nl\}\}f.write(doclist) \{\{nl\}\}f.close()`} \tn % Row Count 2 (+ 2) % Row 12 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{List to New Text File: }}\{\{nl\}\}`with open(r'newfile.txt', 'w') as fp: \{\{nl\}\}~~~~for text in doclist: \{\{nl\}\}~~~~~~~~`\# write each item on a new line` \{\{nl\}\}~~~~~~~~fp.write("\%s\textbackslash{}n" \% text) \{\{nl\}\}~~~~print('Done')`} \tn % Row Count 10 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://www.w3schools.com/python/python\_file\_handling.asp} \newline https://pynative.com/python-write-list-to-file/} \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}{Type Casting}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Get Type from Variable}}\{\{nl\}\}`name = "freeCodeCamp"\{\{nl\}\}print("The variable, name is of type:", type(name))`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{output: \{\{nl\}\} The variable, name is of type: \textless{}class 'str'\textgreater{}} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Specify/Convert Variable Type}}} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`x = int(1)` ~~ \# x will be 1 \{\{nl\}\}`y = float("2.8")` ~~ \# y will be 2.8 \{\{nl\}\}`z = str(3)` ~~ \# z will be "3"} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular Expression (or re / regex)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`import re`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `re.sub(pattern, repl, string, count=0, flags=0)\{\{nl\}\} result = re.sub('abc', '', input) \{\{nl\}\} result = re.sub('abc', 'def', input) \{\{nl\}\} result = re.sub(r'\textbackslash{}s+', ' ', input) \{\{nl\}\}result = \seqsplit{re.sub('abc(def)ghi'}, r'\textbackslash{}1', input) ` & \{\{nl\}\}\# Delete pattern abc\{\{nl\}\}\# Replace pattern abc -\textgreater{} def\{\{nl\}\}\# Eliminate duplicate whitespaces using wildcards\{\{nl\}\}\# Replace a string with a part of itself \tn % Row Count 13 (+ 12) % Row 2 \SetRowColor{LightBackground} `re.search(pattern, string, flags=0)\{\{nl\}\}re.search("c", "abcdef")\{\{nl\}\}re.search("\textasciicircum{}c", "abcdef")\{\{nl\}\}re.search('\textasciicircum{}X', 'A\textbackslash{}nB\textbackslash{}nX', re.MULTILINE)` & \{\{nl\}\}\# Match\{\{nl\}\} \# No match (beginning of str)\{\{nl\}\} \# Match; multiline (beg of each line) \tn % Row Count 21 (+ 8) % Row 3 \SetRowColor{white} `re.match("c", "abcdef")` & \# No match \{\{nl\}\}\# (.match for beginning of string) \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{More Info:}} \newline https://docs.python.org/3/library/re.html \newline https://lzone.de/examples/Python\%20re.sub \newline https://www.w3schools.com/python/python\_regex.asp \newline \newline {\bf{Practice Regex:}} https://regex101.com/} \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}{Dictionary}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Create a dictionary}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`thisdict = \{ \{\{nl\}\} ~~~~"brand": "Ford", \{\{nl\}\}~~~~"model": "Mustang", \{\{nl\}\}~~~~"year": 1964, \{\{nl\}\}~~~~"year": 2020, \{\{nl\}\}~~~~"colors": {[}"red", "white", "blue"{]} \{\{nl\}\}\}`} \tn % Row Count 7 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`print(thisdict{[}"year"{]}) \{\{nl\}\}\#print dictionary key for 'year' \{\{nl\}\} \#(notice the overwritten output; duplicates are not allowed)`\{\{nl\}\}Output:} \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\textasciitilde{}\textasciitilde{}1964\textasciitilde{}\textasciitilde{}\{\{nl\}\}2020} \tn % Row Count 11 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`print(thisdict)`\{\{nl\}\}Output:} \tn % Row Count 12 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': {[}'red', 'white', 'blue'{]}\}} \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Alternative way to create dictionary (Constructor)}}} \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`thisdict = dict(brand = "Ford", electric = False, year = \{\{nl\}\}~~~~2020, colors = {[}"red", "white", "blue"{]})`} \tn % Row Count 19 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Alternate retrievals of key values}}} \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`x = thisdict.get("model") \{\{nl\}\}print(x)`\{\{nl\}\}\{\{nl\}\}Output:\{\{nl\}\}Mustang} \tn % Row Count 22 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{or to get dictionary key names and/or values\{\{nl\}\}} \tn % Row Count 23 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`x = thisdict.keys()`\{\{nl\}\}Output:\{\{nl\}\}dict\_keys({[}'brand', 'electric', 'year', 'colors'{]})\{\{nl\}\}\{\{nl\}\}`x = thisdict.values()`\{\{nl\}\}Output:\{\{nl\}\}dict\_values({[}'Ford', False, 2020, {[}'red', 'white', 'blue'{]}{]})\{\{nl\}\}\{\{nl\}\}`x = thisdict.items() \#get all keys \& values`} \tn % Row Count 29 (+ 6) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Make changes to dictionary}}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Dictionary (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`car{[}"colors"{]} = "red" \{\{nl\}\}print(x)`\{\{nl\}\}Output:\{\{nl\}\}dict\_values({[}'Ford', 'Mustang', 2020, 'red'{]}) \{\{nl\}\}\{\{nl\}\}or\{\{nl\}\}`thisdict.update(\{"year": 2022\})`} \tn % Row Count 4 (+ 4) % Row 14 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{check if key/entry exists:}}} \tn % Row Count 5 (+ 1) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`if "model" in thisdict: \{\{nl\}\}~~~~print("Yes, this key is in \{thisdict\} dictionary")`} \tn % Row Count 8 (+ 3) % Row 16 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Remove Items}}} \tn % Row Count 9 (+ 1) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`thisdict.pop("model") \#removes specified key item \{\{nl\}\} thisdict.popitem() \#removes last inserted* item\{\{nl\}\}del thisdict{[}"model"{]} \#to remove key item or... \{\{nl\}\}del thisdict \#to delete the entire dictionary`} \tn % Row Count 14 (+ 5) % Row 18 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Loop/Iterations on Dictionaries}}} \tn % Row Count 15 (+ 1) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`for x in thisdict: \{\{nl\}\}~~~~print(thisdict{[}x{]})\{\{nl\}\}~~~~\#print each value in dict per line \{\{nl\}\}\{\{nl\}\}for x in thisdict.values(): \{\{nl\}\}~~~~print(x) \{\{nl\}\}~~~~\#print values of dictionary \{\{nl\}\}\{\{nl\}\}for x in thisdict.keys(): \{\{nl\}\}~~~~print(x) \{\{nl\}\}~~~~\#print keys of dictionary \{\{nl\}\}\{\{nl\}\}for x, y in thisdict.items(): \{\{nl\}\}~~~~print(x) \{\{nl\}\}~~~~\#loop through both keys and values`} \tn % Row Count 26 (+ 11) % Row 20 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Nested Dictionary}}} \tn % Row Count 27 (+ 1) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`child1 = \{ \{\{nl\}\}~~~~"name" : "Emil", \{\{nl\}\}~~~~"year" : 2004 \{\{nl\}\}\} child2 = \{ \{\{nl\}\}~~~~"name" : "Tobias", \{\{nl\}\}~~~~"year" : 2007 \{\{nl\}\}\} child3 = \{ \{\{nl\}\}~~~~"name" : "Linus", \{\{nl\}\}~~~~"year" : 2011 \{\{nl\}\}\} \{\{nl\}\} \{\{nl\}\}myfamily = \{ \{\{nl\}\}~~~~"child1" : child1, \{\{nl\}\}~~~~"child2" : child2, \{\{nl\}\}~~~~"child3" : child3 \{\{nl\}\}\} \{\{nl\}\}print(myfamily{[}"child2"{]}{[}"name"{]}) \{\{nl\}\}` \{\{nl\}\}Output: \{\{nl\}\}Tobias} \tn % Row Count 40 (+ 13) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Dictionary (cont)}} \tn % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Other uses with Dictionary}} \{\{nl\}\}No. of components: `len(thisdict)` \{\{nl\}\}Confirm dict data type: `type(thisdict)` \{\{nl\}\}Make copy of dictionary: `mydict = thisdict.copy()`} \tn % Row Count 4 (+ 4) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://www.w3schools.com/python/python\_dictionaries.asp}} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{clear()}} Removes all the elements \newline {\bf{fromkeys()}} Returns dict w/ specified keys and value \newline {\bf{get()}} Returns value of specified key \newline {\bf{items()}} Returns a tupled list for each key value pair \newline {\bf{keys()}} Returns a list of dict keys \newline {\bf{pop()}} Removes element of specified key \newline {\bf{popitem()}} Removes last inserted key-value pair \newline {\bf{setdefault()}} Returns value of specified key \newline {\bf{update()}} Updates dict w/ specified key-value pairs \newline {\bf{values()}} Returns list of all values in dict} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}