\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{Lavanya.R (prl1007)} \pdfinfo{ /Title (python-data-structures.pdf) /Creator (Cheatography) /Author (Lavanya.R (prl1007)) /Subject (Python Data Structures 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}{5447FF} \definecolor{LightBackground}{HTML}{F4F3FF} \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 Data Structures Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Lavanya.R (prl1007)} via \textcolor{DarkBackground}{\uline{cheatography.com/159122/cs/33562/}}} \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}Lavanya.R (prl1007) \\ \uline{cheatography.com/prl1007} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 8th August, 2022.\\ Updated 8th August, 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*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Python Data Structures (Lavanya.R - 2247249)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way. Data structures are "containers" that organize and group data according to type.} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.} \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Access Items (Same for tuples)}} \tn % Row 0 \SetRowColor{LightBackground} Print the second item of the list: & print(thislist{[}1{]}) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Print the last item of the list: & print(thislist{[}-1{]}) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} A range of indexes by specifying where to start and where to end the range. & print(thislist{[}2:5{]}) \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} Returns the items from the beginning to, but NOT including, "kiwi" & print(thislist{[}:4{]}) \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} Returns the items from "cherry" to the end & print(thislist{[}2:{]}) \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} Returns the items from "orange" (-4) to, but NOT including "mango" (-1) & print(thislist{[}-4:-1{]}) \tn % Row Count 19 (+ 4) % Row 6 \SetRowColor{LightBackground} Check if "apple" is present in the list & if "apple" in thislist: print("Yes, 'apple' is in the fruits list") \tn % Row Count 23 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{thislist = {[}"apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.50347 cm} x{2.24273 cm} x{1.8308 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{List Methods}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{append()} & Adds an element at the end of the list & list.append (element) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{clear()} & Removes all the elements from the list & list.clear() \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{copy()} & Returns a copy of the list & list.copy() \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{count()} & Returns the number of elements with the specified value & \seqsplit{List.count(element)} \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{extend()} & Add the elements of a list (or any iterable), to the end of the current list & \seqsplit{List1.extend(List2)} \tn % Row Count 13 (+ 4) % Row 5 \SetRowColor{white} \seqsplit{index()} & Returns the index of the first element with the specified value & List.index(element{[},start{[},end{]}{]}) \tn % Row Count 17 (+ 4) % Row 6 \SetRowColor{LightBackground} \seqsplit{insert()} & Adds an element at the specified position & list.insert(\textless{}position, element) \tn % Row Count 20 (+ 3) % Row 7 \SetRowColor{white} pop() & Removes the element at the specified position & list.pop({[}index{]}) \tn % Row Count 23 (+ 3) % Row 8 \SetRowColor{LightBackground} \seqsplit{remove()} & Removes the first item with the specified value & \seqsplit{list.remove(element)} \tn % Row Count 26 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{reverse()} & Reverses the order of the list & \seqsplit{list\_name.reverse()} \tn % Row Count 29 (+ 3) % Row 10 \SetRowColor{LightBackground} \seqsplit{sort()} & Sorts the list & \seqsplit{List\_name.sort()} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.50347 cm} x{2.24273 cm} x{1.8308 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{List Methods (cont)}} \tn % Row 11 \SetRowColor{LightBackground} del() & Element to be deleted is mentioned using list name and index. & del list.{[}index{]} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{List Comprehension}} \tn % Row 0 \SetRowColor{LightBackground} Containing only the fruits with the letter "a" in the name. & newlist = {[}x for x in fruits if "a" in x{]} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Only accept items that are not "apple": & newlist = {[}x for x in fruits if x != "apple"{]} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Accept only numbers lower than 5: & newlist = {[}x for x in range(10) if x \textless{} 5{]} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Set the values in the new list to upper case: & newlist = {[}x.upper() for x in fruits{]} \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} Set all values in the new list to 'hello': & newlist = {[}'hello' for x in fruits{]} \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} Return "orange" instead of "banana": & newlist = {[}x if x != "banana" else "orange" for x in fruits{]} \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. \newline \newline fruits = {[}"apple", "banana", "cherry", "kiwi", "mango"{]} \newline newlist = {[}{]} \newline \newline for x in fruits: \newline if "a" in x: \newline newlist.append(x) \newline \newline print(newlist) \newline \newline The Syntax \newline newlist = {[}expression for item in iterable if condition == True{]} \newline The return value is a new list, leaving the old list unchanged.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tuples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A tuple is a collection of objects which ordered and immutable. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Add Items}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Since tuples are immutable, they do not have a build-in append() method, but there are other ways to add items to a tuple. \newline % Row Count 3 (+ 3) Convert the tuple into a list, add "orange", and convert it back into a tuple:% Row Count 5 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{thistuple = ("apple", "banana", "cherry") \newline y = list(thistuple) \newline y.append("orange") \newline thistuple = tuple(y)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Add tuple to a tuple.}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{You are allowed to add tuples to tuples, so if you want to add one item, (or many), create a new tuple with the item(s), and add it to the existing tuple \newline % Row Count 4 (+ 4) Create a new tuple with the value "orange", and add that tuple:% Row Count 6 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{thistuple = ("apple", "banana", "cherry") \newline y = ("orange",) \newline thistuple += y \newline \newline print(thistuple)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Access Items}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{You cannot access items in a set by referring to an index or a key. \newline % Row Count 2 (+ 2) But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword. \newline % Row Count 5 (+ 3) thisset = \{"apple", "banana", "cherry"\} \newline % Row Count 6 (+ 1) for x in thisset: \newline % Row Count 7 (+ 1) print(x)% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Once a set is created, you cannot change its items, but you can add new items.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tuple Methods}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{count()} & Returns the number of times a specified value occurs in a tuple \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{index()} & Searches the tuple for a specified value and returns the position of where it was found \tn % Row Count 5 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Remove Items}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Convert the tuple into a list, remove "apple", and convert it back into a tuple: \newline % Row Count 2 (+ 2) thistuple = ("apple", "banana", "cherry") \newline % Row Count 3 (+ 1) y = list(thistuple) \newline % Row Count 4 (+ 1) y.remove("apple") \newline % Row Count 5 (+ 1) thistuple = tuple(y)% Row Count 6 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The del keyword can delete the tuple completely: \newline \newline thistuple = ("apple", "banana", "cherry") \newline del thistuple \newline print(thistuple) \#this will raise an error because the tuple no longer exists} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Change Tuple Values}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. \newline % Row Count 3 (+ 3) But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.% Row Count 6 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{x = ("apple", "banana", "cherry") \newline y = list(x) \newline y{[}1{]} = "kiwi" \newline x = tuple(y) \newline \newline print(x)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sets}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements.% Row Count 2 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Set is define in \{ \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Add Items}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{To add one item to a set use the add() method. \newline % Row Count 1 (+ 1) Example \newline % Row Count 2 (+ 1) Add an item to a set, using the add() method: \newline % Row Count 3 (+ 1) thisset = \{"apple", "banana", "cherry"\} \newline % Row Count 4 (+ 1) thisset.add("orange") \newline % Row Count 5 (+ 1) print(thisset)% Row Count 6 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{To add items from another set into the current set, use the update() method. \newline \newline Example \newline Add elements from tropical into thisset: \newline \newline thisset = \{"apple", "banana", "cherry"\} \newline tropical = \{"pineapple", "mango", "papaya"\} \newline thisset.update(tropical) \newline print(thisset) \newline \newline {\bf{The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.09494 cm} x{3.88206 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Remove Set Items}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{remove()} & thisset.remove("banana") \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{discard()} & \seqsplit{thisset.discard("banana")} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} clear() & method empties the set \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} del keyword & will delete the set completely(del thisset) \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{We can also use the pop() method to remove an item, but this method will remove the last item. Remember that sets are unordered, so you will not know what item that gets removed. \newline \newline The return value of the pop() method is the removed item. \newline \newline thisset = \{"apple", "banana", "cherry"\} \newline x = thisset.pop() \newline print(x) \newline print(thisset)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Set Methods}} \tn % Row 0 \SetRowColor{LightBackground} add() & Adds an element to the set \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} clear() & Removes all the elements from the set \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} copy() & Returns a copy of the set \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} difference() & Returns a set containing the difference between two or more sets \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{difference\_update()} & Removes the items in this set that are also included in another, specified set \tn % Row Count 13 (+ 4) % Row 5 \SetRowColor{white} discard() & Remove the specified item \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} intersection() & Returns a set, that is the intersection of two other sets \tn % Row Count 18 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{intersection\_update()} & Removes the items in this set that are not present in other, specified set(s) \tn % Row Count 22 (+ 4) % Row 8 \SetRowColor{LightBackground} isdisjoint() & Returns whether two sets have a intersection or not \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} issubset() & Returns whether another set contains this set or not \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} issuperset() & Returns whether this set contains another set or not \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Set Methods (cont)}} \tn % Row 11 \SetRowColor{LightBackground} pop() & Removes an element from the set \tn % Row Count 2 (+ 2) % Row 12 \SetRowColor{white} remove() & Removes the specified element \tn % Row Count 4 (+ 2) % Row 13 \SetRowColor{LightBackground} \seqsplit{symmetric\_difference()} & Returns a set with the symmetric differences of two sets \tn % Row Count 7 (+ 3) % Row 14 \SetRowColor{white} \seqsplit{symmetric\_difference\_update()} & inserts the symmetric differences from this set and another \tn % Row Count 10 (+ 3) % Row 15 \SetRowColor{LightBackground} union() & Return a set containing the union of sets \tn % Row Count 12 (+ 2) % Row 16 \SetRowColor{white} update() & Update the set with the union of this set and others \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Dictionary}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Dictionary in Python is a collection of keys values, used to store data values like a map, which, unlike other data types which hold only a single value as an element.% Row Count 4 (+ 4) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Adding Items in Dictionary}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Adding an item to the dictionary is done by using a new index key and assigning a value to it: \newline % Row Count 2 (+ 2) {\bf{Example}} \newline % Row Count 3 (+ 1) thisdict = \{ \newline % Row Count 4 (+ 1) "brand": "Ford", \newline % Row Count 5 (+ 1) "model": "Mustang", \newline % Row Count 6 (+ 1) "year": 1964 \newline % Row Count 7 (+ 1) \} \newline % Row Count 8 (+ 1) thisdict{[}"color"{]} = "red" \newline % Row Count 9 (+ 1) print(thisdict)% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The {\bf{update()}} method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added. \newline \newline The argument must be a dictionary, or an iterable object with key:value pairs. \newline \newline {\bf{Example}} \newline Add a color item to the dictionary by using the update() method: \newline \newline thisdict = \{ \newline "brand": "Ford", \newline "model": "Mustang", \newline "year": 1964 \newline \} \newline thisdict.update(\{"color": "red"\})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.28482 cm} x{1.69218 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Removing Items in Dictionary}} \tn % Row 0 \SetRowColor{LightBackground} The {\bf{pop()}} method removes the item with the specified key name: & \seqsplit{thisdict.pop("model")} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} The {\bf{popitem()}} method removes the last inserted item & \seqsplit{thisdict.popitem()} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} The {\bf{del}} keyword removes the item with the specified key name: & del thisdict{[}"model"{]} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} The {\bf{del}} keyword can also delete the dictionary completely: & del thisdict \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} The {\bf{clear()}} method empties the dictionary & \seqsplit{thisdict.clear()} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{thisdict = \{ \newline "brand": "Ford", \newline "model": "Mustang", \newline "year": 1964 \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.59501 cm} x{2.05965 cm} x{1.92234 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Dictionary Methods}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{clear()} & Removes all the elements from the dictionary & \seqsplit{dictionary.clear()} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{copy()} & Returns a copy of the dictionary & \seqsplit{dictionary.copy()} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{fromkeys()} & Returns a dictionary with the specified keys and value & \seqsplit{dict.fromkeys(keys}, value) \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} get() & Returns the value of the specified key & \seqsplit{dictionary.get(keyname}, value) \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{items()} & Returns a list containing a tuple for each key value pair & \seqsplit{dictionary.items()} \tn % Row Count 15 (+ 4) % Row 5 \SetRowColor{white} \seqsplit{keys()} & Returns a list containing the dictionary's keys & \seqsplit{dictionary.keys()} \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} pop() & Removes the element with the specified key & \seqsplit{dictionary.pop(keyname}, defaultvalue) \tn % Row Count 21 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{popitem()} & Removes the last inserted key-value pair & \seqsplit{dictionary.popitem()} \tn % Row Count 24 (+ 3) % Row 8 \SetRowColor{LightBackground} \seqsplit{setdefault()} & Returns the value of the specified key. If the key does not exist: insert the key, with the specified value & \seqsplit{dictionary.setdefault(keyname}, value) \tn % Row Count 30 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.59501 cm} x{2.05965 cm} x{1.92234 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Dictionary Methods (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \seqsplit{update()} & Updates the dictionary with the specified key-value pairs & \seqsplit{dictionary.update(iterable)} \tn % Row Count 4 (+ 4) % Row 10 \SetRowColor{white} \seqsplit{values()} & Returns a list of all the values in the dictionary & \seqsplit{dictionary.values()} \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Python Modules}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Import our program as python module \newline % Row Count 1 (+ 1) -\textgreater{} Create file in notepad using .py extension \newline % Row Count 2 (+ 1) -\textgreater{} Upload it in sample data \newline % Row Count 3 (+ 1) -\textgreater{} Copy path of the uploaded file% Row Count 4 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Code:}} \newline from google.colab import files \newline !cp path /content} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}