\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{corisco} \pdfinfo{ /Title (strings-lists-tuples-and-dictionaries-in-python.pdf) /Creator (Cheatography) /Author (corisco) /Subject (strings, lists, tuples and dictionaries in python 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}{FFAA3B} \definecolor{LightBackground}{HTML}{FFF4E6} \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{strings, lists, tuples and dictionaries in python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{corisco} via \textcolor{DarkBackground}{\uline{cheatography.com/167022/cs/34954/}}} \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}corisco \\ \uline{cheatography.com/corisco} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 7th November, 2022.\\ Updated 5th November, 2022.\\ 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{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{STRINGS}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=30\}\} {\bf{Strings}} & string = "words" or 'words' \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} conect & string1+string2 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} multiply & string*3 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} compare ('if' or 'if not') & string1 == string2 (or != or \textgreater{} or \textless{}) \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} check ('if or 'if not') & 'substring' in string \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} position & string{[}0{]} or string{[}-1{]} or string{[}x{]} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} slicing & string{[}start:end:step{]} \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} & string{[}:3{]} or string{[}1:{]} \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} & string{[}::-1{]} inverted \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.68 cm} x{4.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Slicing strings}} \tn % Row 0 \SetRowColor{LightBackground} slicing: & string{[}start:end:step{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\emph{from start to end}} & string{[}2:5{]} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\emph{from start}} & string{[}:5{]} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} {\emph{to the end}} & string{[}2:{]} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} {\emph{jumping}} & string{[}2:5:2{]} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} {\emph{negative/inverted}} & string{[}::-1{]} \tn % Row Count 9 (+ 2) \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}{Modify strings}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{remove whitespaces}} before and after the string & string.strip() \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\emph{or remove any 'characters'}} & string.strip('ch') \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\emph{only before(left) the string}} & string.lstrip() \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\emph{only after(right) the string}} & string.rstrip() \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{replace string}} & string.replace("old value", "new value", counter) \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{counter is how many occurences from start, nothing is default and means every occurrence}}} \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} {\emph{use it to remove whitespaces inside the string}} & string.replace(" ", "") \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} {\bf{split strings}} create list from strings & \seqsplit{string.split(separator},maxsplit) \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} {\emph{separator is optional, whitespace is default}} & string.split(",") \tn % Row Count 23 (+ 3) % Row 9 \SetRowColor{white} {\emph{maxsplit is optional, all occurrences is default(-1)}} & string.split("-",1) \tn % Row Count 26 (+ 3) % Row 10 \SetRowColor{LightBackground} {\bf{find string}} & string.find("value",start,end) \tn % Row Count 28 (+ 2) % Row 11 \SetRowColor{white} {\emph{find the first occurrence of a substring in a string}} & \seqsplit{string.find("substring")} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Modify strings (cont)}} \tn % Row 12 \SetRowColor{LightBackground} {\emph{start and end are optional}} & \seqsplit{string.find("substring"},3,10) \tn % Row Count 2 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{returns the position of the substring}}} \tn % Row Count 3 (+ 1) % Row 14 \SetRowColor{LightBackground} {\emph{index and find are the same, except that when false find returns -1 and index returns error}} & \seqsplit{string.index("substring")} \tn % Row Count 8 (+ 5) % Row 15 \SetRowColor{white} {\bf{lowercase}} & string.lower() \tn % Row Count 9 (+ 1) % Row 16 \SetRowColor{LightBackground} {\bf{uppercase}} & string.upper() \tn % Row Count 10 (+ 1) % Row 17 \SetRowColor{white} {\bf{capitalize}} first char upper & string.capitalize() \tn % Row Count 12 (+ 2) % Row 18 \SetRowColor{LightBackground} {\bf{title}} first char of each word upper & string.title() \tn % Row Count 14 (+ 2) % Row 19 \SetRowColor{white} {\bf{join}} values of a list/dict/tuple/ into string & ' '.join(list) \tn % Row Count 17 (+ 3) % Row 20 \SetRowColor{LightBackground} {\bf{count}} the occurences of substring in a string & \seqsplit{string.count('substring')} \tn % Row Count 20 (+ 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}{Format strings}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\} {\bf{str.format}} & 'Hello, \{\} and \{\}'.format(string, string2) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} f-string (3.6+) & f'Hello, \{string\} and \{string2\}' \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\emph{Integer numbers into strings}}* & f'Hello, \{string:\_\}' \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} 'b' - binary & \{string:b\} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} 'c' - character (ASCII) & 'Hello, \{example:c\}'.format(example = 'String', ...) \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} 'd' - decimal & 'Hello, \{0:d\} and \{1\}.format(string,string2) \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} 'o' - octal & \{string:o\} \tn % Row Count 15 (+ 1) % Row 7 \SetRowColor{white} 'x' - hexadecimal, lowercase & 'Hello, \{0.x\} and \{0:X\}.format(string) \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} 'X' - hexadecimal, uppercase & \{string:X\} \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} 'n' - number, decimal in local language OS & \{string:n\} \tn % Row Count 22 (+ 3) % Row 10 \SetRowColor{LightBackground} {\bf{Floating-point}} & f'\{integer:f\}' (6 standard) \tn % Row Count 24 (+ 2) % Row 11 \SetRowColor{white} {\emph{round(a,2)}} & f'\{integer:.2f\}' (2 decimal digits) \tn % Row Count 26 (+ 2) % Row 12 \SetRowColor{LightBackground} 'e' ou 'E' - scientific notation (6 standard) & \{:e\}.format(999) \tn % Row Count 29 (+ 3) % Row 13 \SetRowColor{white} 'E' - scientific notation (6 standard) & f'\{999:.3E\}' \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Format strings (cont)}} \tn % Row 14 \SetRowColor{LightBackground} 'g' precisão \textgreater{}=1, round digits {\emph{p}} to significant digit & \{:g\}.format(12.1231235843) \tn % Row Count 3 (+ 3) % Row 15 \SetRowColor{white} 'n' - same as 'g', but local language OS & f'\{12.1231235843:n\}' \tn % Row Count 5 (+ 2) % Row 16 \SetRowColor{LightBackground} '\%' - multiply the number *100 and '\%' after & f'\{0.05:\%\}' \tn % Row Count 8 (+ 3) % Row 17 \SetRowColor{white} {\emph{two digits before '.' and two after}} & '\{:2.2\%\}'.format(0.05) \tn % Row Count 10 (+ 2) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Spacing}}} \tn % Row Count 11 (+ 1) % Row 19 \SetRowColor{white} '\textless{}' left align / '\textasciicircum{}' center align / '\textgreater{}' right align & '\{0:\textless{}16\}'.format(string) / f.'\{string:\textasciicircum{}16\}' / \{:\textgreater{}16\} \tn % Row Count 14 (+ 3) % Row 20 \SetRowColor{LightBackground} number of digits & \{:4) \tn % Row Count 15 (+ 1) % Row 21 \SetRowColor{white} {\emph{whitespaces if the string has less than 16..}} & \{string:16\} \tn % Row Count 18 (+ 3) % Row 22 \SetRowColor{LightBackground} {\emph{choose char instead of whitespace}} & \{*:16\} \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.32 cm} x{5.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{COLLECTIONS}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=15\}\}List & collection which is ordered and changeable. Allows duplicate members. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Tuple & collection which is ordered and unchangeable. Allows duplicate members. \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Dictionary & collection which is ordered and changeable. No duplicate members. \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Set & collection which is unordered, unchangeable, and unindexed. No duplicate members. \tn % Row Count 12 (+ 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}{LISTS}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\} {\bf{Lists}} & list = {[}"string", "string2", integer, Bool{]} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\emph{ordered, changeable, allow duplicates}} & list = {[}item1, item2, item3{]} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Lenght}} & len(list) \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} {\bf{Access}} {\emph{ {[}start index included:end index not{]}}} & list{[}1{]} / list{[}-1{]}, list{[}2:5{]}, list{[}:5{]} \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Check if Item exists}} & if "item" in list: \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} {\bf{Change items}} & list{[}3{]} = "new\_value" \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} {\emph{range of items}} & list{[}2:5{]} = "new\_value1", "new\_value2" \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} {\bf{insert(index,value)}} {\emph{without replacing any item}} & list.insert(2, "item") \tn % Row Count 18 (+ 3) % Row 8 \SetRowColor{LightBackground} {\bf{append()}} {\emph{add item to the end}} & list.append("item") \tn % Row Count 20 (+ 2) % Row 9 \SetRowColor{white} {\bf{extend()}} {\emph{add items from other list}} & list.extend(list2) \tn % Row Count 22 (+ 2) % Row 10 \SetRowColor{LightBackground} {\emph{works with tuples also}} & list.extend(tuple) \tn % Row Count 24 (+ 2) % Row 11 \SetRowColor{white} {\bf{remove()}} {\emph{remove first matching value}} & \seqsplit{list.remove("string")} \tn % Row Count 27 (+ 3) % Row 12 \SetRowColor{LightBackground} {\bf{pop()}} {\emph{remove specified index, and returns it}} & list.pop(1) \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{LISTS (cont)}} \tn % Row 13 \SetRowColor{LightBackground} {\bf{del()}} {\emph{remove specified index}} & list.del(3) \tn % Row Count 2 (+ 2) % Row 14 \SetRowColor{white} {\bf{clear()}} {\emph{empties the list}} & list.clear() \tn % Row Count 4 (+ 2) % Row 15 \SetRowColor{LightBackground} {\bf{sort()}} {\emph{sort the list alphanumerically}} & list.sort() \tn % Row Count 7 (+ 3) % Row 16 \SetRowColor{white} {\emph{sort descending}} & \seqsplit{list.sort(reverse=True)} \tn % Row Count 9 (+ 2) % Row 17 \SetRowColor{LightBackground} {\bf{reverse() }} {\emph{order}} & list.reverse() \tn % Row Count 11 (+ 2) % Row 18 \SetRowColor{white} {\bf{copy()}} make a copy & list2 = list.copy() \tn % Row Count 13 (+ 2) % Row 19 \SetRowColor{LightBackground} {\emph{or use list()}} & list3 = list(list2) \tn % Row Count 14 (+ 1) % Row 20 \SetRowColor{white} {\bf{Concatenate Lists}} & list3 = list1 + list2 \tn % Row Count 16 (+ 2) % Row 21 \SetRowColor{LightBackground} {\emph{or use extend()}} & list.extend(list2) \tn % Row Count 17 (+ 1) % Row 22 \SetRowColor{white} {\bf{count()}} returns the number of items* & list.count() / list.count('value') \tn % Row Count 19 (+ 2) % Row 23 \SetRowColor{LightBackground} {\bf{index()}} {\emph{finds the item and return its index}} & list.index('value') \tn % Row Count 22 (+ 3) % Row 24 \SetRowColor{white} {\bf{min() / max() / sum()}} & list.min() list.max() list.sum() \tn % Row Count 24 (+ 2) % Row 25 \SetRowColor{LightBackground} {\bf{enumerate(index, value)}} & for i, v in enumerate(list): /n "\{i\} : \{v\}' \tn % Row Count 27 (+ 3) % Row 26 \SetRowColor{white} {\bf{for loop}} & for x in list: \tn % Row Count 28 (+ 1) % Row 27 \SetRowColor{LightBackground} {\emph{through index}} & for x in range(len(list)): \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{LISTS (cont)}} \tn % Row 28 \SetRowColor{LightBackground} {\emph{while loop}} & while x \textless{}= len(list): /n i+=1 \tn % Row Count 2 (+ 2) % Row 29 \SetRowColor{white} {\emph{list compreehension}} & {[}print{[}x{]} for x in list{]} \tn % Row Count 4 (+ 2) \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}{TUPLES}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\} {\bf{Tuples}} & tuple = ("value1", int, bool ,"value1") \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\emph{are unordered, unchangeable, allow duplicates}} & tuple = (item1,) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Lenght}} & len(tuple) \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} {\bf{Access}} {\emph{{[}start index included:end index not{]}}} & tuple{[}1{]} / tuple{[}-1{]}, tuple{[}2:5{]}, tuple{[}:5{]} \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Check if Item exists}} & if "item" in tuple: \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} {\bf{Change items}} {\emph{tuples are unchangeable}} & list = list(tuple) \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} {\emph{convert tuple to list and back to tuple}} & tuple = tuple(list) \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} {\bf{Concatenate}} {\emph{add tuple to a tuple}} & tuple1 + tuple2 / tuple1 =+ tuple2 \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{Lists inside a tuple}} {\emph{are changeable}} & tuple = ({[}"value1","value2"{]}, item2, item3) \tn % Row Count 22 (+ 3) % Row 9 \SetRowColor{white} {\bf{count()}} {\emph{returns the number of items}} & tuple.count() / \seqsplit{tuple.count('value')} \tn % Row Count 25 (+ 3) % Row 10 \SetRowColor{LightBackground} {\bf{index()}} {\emph{finds the item and return its index}} & \seqsplit{tuple.index('value')} \tn % Row Count 28 (+ 3) % Row 11 \SetRowColor{white} {\bf{min() / max() / sum()}} & tuple.min() tuple.max() tuple.sum() \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{TUPLES (cont)}} \tn % Row 12 \SetRowColor{LightBackground} {\bf{enumerate(index, value)}} & for i, v in enumerate(tuple): /n "\{i\} : \{v\}' \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} {\bf{for loop}} & for x in tuple: \tn % Row Count 4 (+ 1) % Row 14 \SetRowColor{LightBackground} {\emph{through index}} & for x in range(len(tuple)): \tn % Row Count 6 (+ 2) % Row 15 \SetRowColor{white} {\emph{while loop}} & while x \textless{}= len(tuple): /n i+=1 \tn % Row Count 8 (+ 2) % Row 16 \SetRowColor{LightBackground} {\emph{list compreehension}} & {[}print{[}x{]} for x in tuple{]} \tn % Row Count 10 (+ 2) \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}{DICTIONARIES}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\} {\bf{Dictionaries}} & dict = \{"key":"value", "key2":"value2"\} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\emph{ordered, changeable, do {\bf{not}} allow duplicates}} & dict=\{"key1": bool,"key2":int, "key3": {[}list{]}\} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{dict cannot have same keys}}} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} {\bf{Lenght}} & len(dict) \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{Access}} {\emph{get the value of the "key"}} & value1 = dict{[}"key1"{]} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} dict.get("key","return if not found") & value4 = dict.get("key4", "Not found") \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{List of Keys}} & x = dict.keys() \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} {\bf{Check if key exists}} & if "keys" in dict: \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} {\emph{ if values exists}} & if "value1" in dict.values() \tn % Row Count 16 (+ 2) % Row 9 \SetRowColor{white} {\bf{Change values of a key}} & dict{[}"key"{]} = value \tn % Row Count 18 (+ 2) % Row 10 \SetRowColor{LightBackground} {\emph{using update()}} & dict.update(\{'key':'value'\}) \tn % Row Count 20 (+ 2) % Row 11 \SetRowColor{white} {\bf{Add}} & dict{[}"key"{]} = value \tn % Row Count 21 (+ 1) % Row 12 \SetRowColor{LightBackground} {\emph{also can use update()}} & dict.update(\{'key':'value'\}) \tn % Row Count 23 (+ 2) % Row 13 \SetRowColor{white} {\bf{Remove}} {\emph{.pop or popitem(removes the last key inserted)) }} & dict.pop("key") / dict.popitem() \tn % Row Count 27 (+ 4) % Row 14 \SetRowColor{LightBackground} {\emph{using del}} & del dict("key") \tn % Row Count 28 (+ 1) % Row 15 \SetRowColor{white} {\emph{del can delete the dictionary completely}} & del dict \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{DICTIONARIES (cont)}} \tn % Row 16 \SetRowColor{LightBackground} {\bf{clear}} {\emph{empties the dictionary}} & dict.clear() or dict = \{\} \tn % Row Count 2 (+ 2) % Row 17 \SetRowColor{white} {\bf{Copy}} & dict = dict2 / dict2 = dict.copy() / dict2 = dict(dict) \tn % Row Count 5 (+ 3) % Row 18 \SetRowColor{LightBackground} {\bf{items()}} & dict.items() \tn % Row Count 6 (+ 1) % Row 19 \SetRowColor{white} {\bf{keys()}} & dict.keys() \tn % Row Count 7 (+ 1) % Row 20 \SetRowColor{LightBackground} {\bf{values()}} & dict.values() \tn % Row Count 8 (+ 1) % Row 21 \SetRowColor{white} {\bf{for loop}} & for keys in dict: \tn % Row Count 9 (+ 1) % Row 22 \SetRowColor{LightBackground} {\emph{keys}} & for keys in dict.keys(): \tn % Row Count 11 (+ 2) % Row 23 \SetRowColor{white} {\emph{values}} & for values in dict.values(): \tn % Row Count 13 (+ 2) % Row 24 \SetRowColor{LightBackground} {\emph{keys and values}} & for keys, values in dict.items() \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}