\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{srinivas.ram} \pdfinfo{ /Title (python-list-dictionaries-tuple-string-methods.pdf) /Creator (Cheatography) /Author (srinivas.ram) /Subject (Python List, dictionaries, tuple, string methods 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}{A34E2F} \definecolor{LightBackground}{HTML}{F9F3F2} \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 List, dictionaries, tuple, string methods Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{srinivas.ram} via \textcolor{DarkBackground}{\uline{cheatography.com/183208/cs/38138/}}} \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}srinivas.ram \\ \uline{cheatography.com/srinivas-ram} \\ \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 April, 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*}{3} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Lists}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{List}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{To Create a List: \{\{nl\}\} {\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\} print(thislist)}}\{\{nl\}\}output::\{\{nl\}\}{\bf{{[}'apple', 'banana', 'cherry'{]}}}} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{A list can contain different data types::}}} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\} {\bf{list1 = {[}"abc", 34, True, 40, "male"{]}}}} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{What is the data type of a list?}}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{mylist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\} print(type(mylist)) }}\{\{nl\}\} output::{\bf{ \textless{}class 'list'\textgreater{}}}} \tn % Row Count 11 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{List items can be accessed by referring to the index number:}}} \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\} print(thislist{[}1{]})}} \{\{nl\}\} output:: {\bf{banana}}} \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{To change the value of a specific item, refer to the index number:}}} \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\} thislist{[}1{]} = "blackcurrant"\{\{nl\}\} print(thislist)}} \{\{nl\}\} output:: {\bf{{[}'apple', 'blackcurrant', 'cherry'{]}}}} \tn % Row Count 21 (+ 4) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{To add an item to the end of the list, use the append() method:}}} \tn % Row Count 23 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\} thislist.append("orange") \{\{nl\}\} print(thislist)}} \{\{nl\}\} output:: {\bf{{[}'apple', 'banana', 'cherry', 'orange'{]}}}} \tn % Row Count 27 (+ 4) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{To append elements from another list to the current list, use the {\bf{extend()}} method.} \tn % Row Count 29 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{list\_A = {[}"apple", "banana", "cherry"{]} \{\{nl\}\} list\_B = {[}"mango", "pineapple", "papaya"{]} \{\{nl\}\} list\_A.extend(list\_B) \{\{nl\}\} print(list\_A)}} \{\{nl\}\} output::\{\{nl\}\} {\bf{{[}'apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya'{]}}}} \tn % Row Count 34 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Lists}} (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{remove()}} method removes the specified item.} \tn % Row Count 2 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{list\_A = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}list\_A.remove("banana") \{\{nl\}\}print(list\_A)}} \{\{nl\}\} output:: {\bf{{[}'apple', 'cherry'{]}}}} \tn % Row Count 5 (+ 3) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{pop()}} method removes the specified index\{\{nl\}\} with out idex it will remove last item} \tn % Row Count 7 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{list\_A = {[}"apple", "banana", "cherry"{]}\{\{nl\}\} list\_A.pop(1) \{\{nl\}\}print(list\_A)}} \{\{nl\}\} output:: {\bf{{[}'apple', 'cherry'{]}}}} \tn % Row Count 10 (+ 3) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The **del keyword removes the specified index\{\{nl\}\} Also delete the list completely} \tn % Row Count 12 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}del thislist{[}0{]} \{\{nl\}\}print(thislist)}}\{\{nl\}\}output::{\bf{{[}'banana', 'cherry'{]}}}} \tn % Row Count 15 (+ 3) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{clear()}} method empties the list.} \tn % Row Count 16 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}thislist.clear() \{\{nl\}\}print(thislist)}} \{\{nl\}\}output::{\bf{{[}{]}}}} \tn % Row Count 19 (+ 3) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{loop through the list items by using a for loop:}}} \tn % Row Count 21 (+ 2) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}for x in thislist: \{\{nl\}\}print(x)}} \{\{nl\}\}output::\{\{nl\}\}{\bf{apple \{\{nl\}\}banana \{\{nl\}\}cherry}}} \tn % Row Count 24 (+ 3) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{loop through the list items by referring to their index number.\{\{nl\}\}Use the {\bf{range()}} and {\bf{len()}} functions to create a suitable iterable.} \tn % Row Count 27 (+ 3) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}for i in range(len(thislist)): \{\{nl\}\}print(thislist{[}i{]})}}\{\{nl\}\}output::{\bf{\{\{nl\}\}apple\{\{nl\}\} banana\{\{nl\}\} cherry}}} \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Lists}} (cont)}} \tn % Row 26 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{loop through the list items by using a while loop.}}} \tn % Row Count 2 (+ 2) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}i = 0 \{\{nl\}\}while i \textless{} len(thislist): \{\{nl\}\}print(thislist{[}i{]}) i = i + 1 }}\{\{nl\}\}output :: {\bf{\{\{nl\}\}apple \{\{nl\}\}banana \{\{nl\}\}cherry}}} \tn % Row Count 6 (+ 4) % Row 28 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{List Comprehension offers the shortest syntax for looping through lists:}}} \tn % Row Count 8 (+ 2) % Row 29 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}{[}print(x) for x in thislist{]}}} \{\{nl\}\}output::{\bf{\{\{nl\}\}apple \{\{nl\}\}banana \{\{nl\}\}cherry}}} \tn % Row Count 11 (+ 3) % Row 30 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{List comprehension offers a shorter syntax \{\{nl\}\}when you want to create a new list \{\{nl\}\}based on the values of an existing list.} \tn % Row Count 14 (+ 3) % Row 31 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Without list comprehension}}\{\{nl\}\} fruits = {[}"apple", "banana", "cherry", "kiwi", "mango"{]} \{\{nl\}\}newlist = {[}{]} \{\{nl\}\}for x in fruits: \{\{nl\}\} if "a" in x: \{\{nl\}\} newlist.append(x) \{\{nl\}\}print(newlist)\{\{nl\}\}{\bf{With list comprehension}} you can do \{\{nl\}\}all that with only one line of code \{\{nl\}\}fruits = {[}"apple", "banana", "cherry", "kiwi", "mango"{]} \{\{nl\}\}newlist = {[}x for x in fruits if "a" in x{]} \{\{nl\}\}print(newlist)} \tn % Row Count 23 (+ 9) % Row 32 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{sort()}} method that will sort the list alphanumerically, ascending, by default:} \tn % Row Count 25 (+ 2) % Row 33 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"orange", "mango", "kiwi", "pineapple", "banana"{]} \{\{nl\}\}thislist.sort() \{\{nl\}\}print(thislist)}}\{\{nl\}\}output:: {\bf{\{\{nl\}\}{[}'banana', 'kiwi', 'mango', 'orange', 'pineapple'{]}}}} \tn % Row Count 29 (+ 4) % Row 34 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{To make a copy, one way is to use the built-in List method {\bf{copy().}}} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Lists}} (cont)}} \tn % Row 35 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{thislist = {[}"apple", "banana", "cherry"{]} \{\{nl\}\}mylist = thislist.copy() \{\{nl\}\}print(mylist)\{\{nl\}\}}}output::{\bf{{[}'apple', 'banana', 'cherry'{]}}}} \tn % Row Count 3 (+ 3) % Row 36 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{List count() Method}}\{\{nl\}\}Return the number of times the value "cherry" \{\{nl\}\}appears in the fruits list:} \tn % Row Count 6 (+ 3) % Row 37 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{fruits = {[}'apple', 'banana', 'cherry'{]} \{\{nl\}\}x = fruits.count("cherry")}}\{\{nl\}\}output:: \{\{nl\}\}{\bf{1}}} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{There are some list methods that will change the order, but in general: the order of the items will not change.} \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}{{\bf{ Dictionaries in Python}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Dictionaries}} are used to store data values in {\bf{key:value pairs.}}\{\{nl\}\}It is a collection which is ordered*, changeable and do not allow duplicates.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Create and print a dictionary:}}\{\{nl\}\}{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}print(thisdict)}}\{\{nl\}\}\{\{nl\}\}output \{\{nl\}\}{\bf{\{'brand': 'Ford', 'model': 'Mustang', 'year': 1964\}}}} \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{You can access the items of a dictionary by referring to \{\{nl\}\}its {\bf{key name}}, inside square brackets:} \tn % Row Count 12 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}x = thisdict{[}"model"{]} \{\{nl\}\}print(x)}}\{\{nl\}\}output::{\bf{Mustang}}} \tn % Row Count 15 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{keys() method}} will return a list of all the keys in the dictionary.} \tn % Row Count 17 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}x = thisdict.keys() \{\{nl\}\}print(x)}} \{\{nl\}\}output:: {\bf{dict\_keys({[}'brand', 'model', 'year'{]})}}} \tn % Row Count 21 (+ 4) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{values()}} method will return a list of all the values in the dictionary.} \tn % Row Count 23 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}x = thisdict.values() \{\{nl\}\}print(x) }}\{\{nl\}\}output::{\bf{dict\_values({[}'Ford', 'Mustang', 1964{]})}}} \tn % Row Count 27 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{we can change the value of a specific item by referring to its {\bf{key name:}}} \tn % Row Count 29 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}thisdict{[}"year"{]} = 2018 \{\{nl\}\}print(thisdict)}} \{\{nl\}\}output::{\bf{\{'brand': 'Ford', 'model': 'Mustang', 'year': 2018\}}}} \tn % Row Count 33 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{ Dictionaries in Python}} (cont)}} \tn % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{update()}} method will update the dictionary with \{\{nl\}\}the items from the given argument.} \tn % Row Count 2 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}thisdict.update(\{"year": 2020\}) \{\{nl\}\}print(thisdict)}} \{\{nl\}\}output::{\bf{\{'brand': 'Ford', 'model': 'Mustang', 'year': 2020\}}}} \tn % Row Count 7 (+ 5) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Adding an item to the dictionary}} is done \{\{nl\}\}by using a new index key and assigning a value to it:} \tn % Row Count 10 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{"brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}thisdict{[}"color"{]} = "red" \{\{nl\}\}print(thisdict)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{\{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'\}}}} \tn % Row Count 15 (+ 5) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{You can loop through a dictionary by using a {\bf{for loop}}.} \tn % Row Count 17 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}for x in thisdict: \{\{nl\}\}print(x)}} \{\{nl\}\}output:: \{\{nl\}\} {\bf{brand \{\{nl\}\}model \{\{nl\}\}year}}} \tn % Row Count 21 (+ 4) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Make a copy of a dictionary with the {\bf{copy()}} method:} \tn % Row Count 23 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{thisdict = \{ "brand": "Ford", "model": "Mustang", "year": 1964 \} \{\{nl\}\}mydict = thisdict.copy() \{\{nl\}\}print(mydict)}} \{\{nl\}\}output:: {\bf{\{'brand': 'Ford', 'model': 'Mustang', 'year': 1964\}}}} \tn % Row Count 27 (+ 4) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A dictionary can contain dictionaries, this is called {\bf{nested dictionaries}}.} \tn % Row Count 29 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{myfamily = \{ \{\{nl\}\} "child1" : \{ \{\{nl\}\}"name" : "Emil", \{\{nl\}\}"year" : 2004 \{\{nl\}\}\}, \{\{nl\}\}"child2" : \{ \{\{nl\}\}"name" : "Tobias", \{\{nl\}\}"year" : 2007 \{\{nl\}\}\}, \{\{nl\}\}"child3" : \{ \{\{nl\}\}"name" : "Linus", \{\{nl\}\}"year" : 2011 \{\{nl\}\}\} \{\{nl\}\}\} \{\{nl\}\}print(myfamily)}}\{\{nl\}\}output::\{\{nl\}\}{\bf{\{'child1': \{'name': 'Emil', 'year': 2004\}, \{\{nl\}\}'child2': \{'name': 'Tobias', 'year': 2007\}, \{\{nl\}\}'child3': \{'name': 'Linus', 'year': 2011\}\}}}} \tn % Row Count 39 (+ 10) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{ Dictionaries in Python}} (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{To access items from a nested dictionary}}, \{\{nl\}\}you use the name of the dictionaries, \{\{nl\}\}starting with the outer dictionary:} \tn % Row Count 3 (+ 3) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{myfamily = \{ \{\{nl\}\}"child1" : \{ \{\{nl\}\}"name" : "Emil", \{\{nl\}\}"year" : 2004 \{\{nl\}\}\}, \{\{nl\}\}"child2" : \{ \{\{nl\}\}"name" : "Tobias", \{\{nl\}\}"year" : 2007 \{\{nl\}\}\}, \{\{nl\}\}"child3" : \{ \{\{nl\}\}"name" : "Linus", \{\{nl\}\}"year" : 2011 \{\{nl\}\}\} \{\{nl\}\}\} \{\{nl\}\}print(myfamily{[}"child2"{]}{[}"name"{]})}} \{\{nl\}\}output::{\bf{Tobias}}} \tn % Row Count 10 (+ 7) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{setdefault() Method}}} \tn % Row Count 11 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{car = \{ \{\{nl\}\}"brand": "Ford", \{\{nl\}\}"model": "Mustang", \{\{nl\}\}"year": 1964 \{\{nl\}\}\} \{\{nl\}\}x = car.setdefault("model", "Bronco") \{\{nl\}\}print(x)}} \{\{nl\}\}output:: {\bf{Mustang}}} \tn % Row Count 15 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.}}} \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}{{\bf{Python Strings}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Strings}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Strings in python are surrounded by either single quotation marks, \{\{nl\}\}or double quotation marks.\{\{nl\}\}Example:: \{\{nl\}\}{\bf{print("Hello") \{\{nl\}\}print('Hello')}} \{\{nl\}\} output:: \{\{nl\}\} {\bf{Hello \{\{nl\}\}Hello}}} \tn % Row Count 6 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Assign String to a Variable}}} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{nl\}\}Example:: \{\{nl\}\}{\bf{a = "Hello" \{\{nl\}\}print(a)}} \{\{nl\}\} output:: \{nl\}\} {\bf{Hello}}} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Multiline Strings}}} \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{a = """Lorem ipsum dolor sit amet, \{\{nl\}\}consectetur adipiscing elit, \{\{nl\}\}sed do eiusmod tempor incididunt \{nl\}\} ut labore et dolore magna aliqua.""" \{\{nl\}\}print(a)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{Lorem ipsum dolor sit amet, \{\{nl\}\}consectetur adipiscing elit, \{\{nl\}\}sed do eiusmod tempor incididunt \{\{nl\}\}ut labore et dolore magna aliqua.}}} \tn % Row Count 18 (+ 8) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Strings are Arrays}}} \tn % Row Count 19 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}Get the character at position 1 (remember that the first character has the position 0): \{\{nl\}\}{\bf{a = "Hello, World!" \{\{nl\}\}print(a{[}1{]})}} \{nl\}\}output:: \{\{nl\}\}{\bf{e}}} \tn % Row Count 23 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Looping Through a String}}} \tn % Row Count 24 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{for x in "banana": \{\{nl\}\} print(x)}} \{\{nl\}\}output:: \{nl\}\}output:: \{\{nl\}\}**b \{\{nl\}\}a \{\{nl\}\}n \{\{nl\}\}a \{\{nl\}\}n \{\{nl\}\}a} \tn % Row Count 27 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String Length}}} \tn % Row Count 28 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The len() function returns the length of a string:\{\{nl\}\}Example:: \{\{nl\}\}{\bf{a = "Hello, World!" \{\{nl\}\}print(len(a))}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{13}}} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Strings}} (cont)}} \tn % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Check String}}} \tn % Row Count 1 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "The best things in life are free!" \{\{nl\}\}print("free" in txt)}} \{\{nl\}\}output:: \{nl\}\}{\bf{True}}} \tn % Row Count 4 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Use it in an if statement:}}} \tn % Row Count 5 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "The best things in life are free!" \{\{nl\}\}if "free" in txt: \{\{nl\}\}print("Yes, 'free' is present.")}} \{nl\}\}output:: \{\{nl\}\}{\bf{Yes, 'free' is present.}}} \tn % Row Count 9 (+ 4) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Check if NOT}}} \tn % Row Count 10 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "The best things in life are free!" \{\{nl\}\}print("expensive" not in txt)}} \{\{nl\}\}output:: \{nl\}\}{\bf{True}}\{\{nl\}\}{\bf{ using 'if'}} \{\{nl\}\}Example:: \{\{nl\}\}{\bf{txt = "The best things in life are free!" \{\{nl\}\}if "expensive" not in txt: \{\{nl\}\}print("No, 'expensive' is NOT present.")}} \{nl\}\}output:: \{\{nl\}\}{\bf{No, 'expensive' is NOT present.}}} \tn % Row Count 18 (+ 8) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Slicing}}} \tn % Row Count 19 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{b = "Hello, World!" \{\{nl\}\}print(b{[}2:5{]})}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{llo}}} \tn % Row Count 21 (+ 2) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Modify Strings to Upper Case}}} \tn % Row Count 22 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{a = "Hello, World!" \{\{nl\}\}print(a.upper())}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{HELLO, WORLD!}}} \tn % Row Count 25 (+ 3) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Modify Strings to Lower Case}}} \tn % Row Count 26 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{a = "Hello, World!" \{\{nl\}\}print(a.lower())}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{hello, world!}}} \tn % Row Count 29 (+ 3) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Remove Whitespace}}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Strings}} (cont)}} \tn % Row 25 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The strip() method removes any whitespace from the beginning or the end: \{\{nl\}\}Example:: \{\{nl\}\}{\bf{a = " Hello, World! " \{\{nl\}\}print(a.strip())}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{Hello, World!}}} \tn % Row Count 4 (+ 4) % Row 26 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Replace String}}} \tn % Row Count 5 (+ 1) % Row 27 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{a = "Hello, World!" \{\{nl\}\}print(a.replace("H", "J"))}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{Jello, World!}}} \tn % Row Count 8 (+ 3) % Row 28 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Split String}}} \tn % Row Count 9 (+ 1) % Row 29 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{a = "Hello, World!" \{\{nl\}\}b = a.split(",") \{\{nl\}\}print(b)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{{[}'Hello', ' World!'{]}}}} \tn % Row Count 12 (+ 3) % Row 30 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{String Concatenation}}} \tn % Row Count 13 (+ 1) % Row 31 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{a = "Hello" \{\{nl\}\}b = "World" \{\{nl\}\}c = a + b \{\{nl\}\}print(c)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{HelloWorld}}\{\{nl\}\} To add a space between them, add a " ": \{\{nl\}\}Example:: \{\{nl\}\}{\bf{a = "Hello" \{\{nl\}\}b = "World" \{\{nl\}\}c = a + " " + b \{\{nl\}\}print(c)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{Hello World}}} \tn % Row Count 19 (+ 6) % Row 32 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Format - Strings}}} \tn % Row Count 20 (+ 1) % Row 33 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Use the format() method to insert numbers into strings: \{\{nl\}\}Example:: \{\{nl\}\}{\bf{age = 30 \{\{nl\}\}txt = "My name is Srinivas, and I am \{\}" \{\{nl\}\}print(txt.format(age))}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{My name is Srinivas, and I am 30}}\{\{nl\}\}You can use index numbers \{0\} to be sure the \{\{nl\}\}arguments are placed in the correct placeholders: \{\{nl\}\}Example:: \{\{nl\}\}{\bf{quantity = 3 \{\{nl\}\}itemno = 567 \{\{nl\}\}price = 49.95 \{\{nl\}\}myorder = "I want to pay \{2\} dollars for \{0\} pieces of item \{1\}." \{\{nl\}\}print(myorder.format(quantity, itemno, price))}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{I want to pay 49.95 dollars for 3 pieces of item 567}}} \tn % Row Count 33 (+ 13) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Strings}} (cont)}} \tn % Row 34 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Escape Characters}}} \tn % Row Count 1 (+ 1) % Row 35 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The escape character allows you to use double quotes \{\{nl\}\}when you normally would not be allowed: \{\{nl\}\}Example:: \{\{nl\}\}{\bf{txt = "Iam \textbackslash{}"Important\textbackslash{}" in this line." \{\{nl\}\}print(txt)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{Iam "Important" in this line}}} \tn % Row Count 6 (+ 5) % Row 36 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Capitalize() Method In Strings}}} \tn % Row Count 7 (+ 1) % Row 37 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The first character is converted to upper case, and \{\{nl\}\}the rest are converted to lower case: \{\{nl\}\}Example:: \{\{nl\}\}{\bf{txt = "python is FUN!" \{\{nl\}\}x = txt.capitalize() \{\{nl\}\}print (x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{Python is fun!}}} \tn % Row Count 12 (+ 5) % Row 38 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String count() Method}}} \tn % Row Count 13 (+ 1) % Row 39 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Return the number of times the value "apple" appears in the string: \{\{nl\}\}Example:: \{\{nl\}\}{\bf{txt = "I love apples, apple are my favorite fruit" \{\{nl\}\}x = txt.count("apple") \{\{nl\}\}print(x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{2}}} \tn % Row Count 18 (+ 5) % Row 40 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String endswith() Method}}} \tn % Row Count 19 (+ 1) % Row 41 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "Hello, welcome to my world." \{\{nl\}\}x = txt.endswith(".") \{\{nl\}\}print(x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{True}}} \tn % Row Count 22 (+ 3) % Row 42 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String find() Method}}} \tn % Row Count 23 (+ 1) % Row 43 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "Hello, welcome to my world." \{\{nl\}\}x = txt.find("welcome") \{\{nl\}\}print(x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{7}}} \tn % Row Count 26 (+ 3) % Row 44 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String isalnum() Method}}} \tn % Row Count 27 (+ 1) % Row 45 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "Company12" \{\{nl\}\}x = txt.isalnum() \{\{nl\}\}print(x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{True}}} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Python Strings}} (cont)}} \tn % Row 46 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String isalpha() Method}}} \tn % Row Count 1 (+ 1) % Row 47 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{txt = "CompanyX" \{\{nl\}\}x = txt.isalpha() \{\{nl\}\}print(x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{True}}} \tn % Row Count 4 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Strings in python are surrounded by either single quotation marks, or double quotation marks.} \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}{{\bf{Tuples In Python}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Tuple}}:} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{nl\}\}Tuples are used to store multiple items in a single variable. \{\{nl\}\}A tuple is a collection which is ordered and unchangeable. \{\{nl\}\}Tuples are written with round brackets. \{\{nl\}\}{\bf{Example}}:: \{\{nl\}\}{\bf{thistuple = ("apple", "banana", "cherry") \{\{nl\}\}print(thistuple).}}\{\{nl\}\}output::{\bf{('apple', 'banana', 'cherry')}}} \tn % Row Count 8 (+ 7) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Tuple Items}}} \tn % Row Count 9 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Tuple items are ordered, unchangeable, and allow duplicate values. \{\{nl\}\}Tuple items are indexed, the first item has index {[}0{]}, \{\{nl\}\} the second item has index {[}1{]} etc.} \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Ordered}}} \tn % Row Count 14 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{When we say that tuples are ordered, \{\{nl\}\}it means that the items have a defined order, \{\{nl\}\}and that order will not change.} \tn % Row Count 17 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Unchangeable}}} \tn % Row Count 18 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Tuples are unchangeable, meaning that we cannot change, \{\{nl\}\}add or remove items after the tuple has been created.} \tn % Row Count 21 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Allow Duplicates}}} \tn % Row Count 22 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{thistuple = ("apple", "banana", "cherry", "apple", "cherry") \{\{nl\}\}print(thistuple)}}\{\{nl\}\}output:: \{\{nl\}\}{\bf{('apple', 'banana', 'cherry', 'apple', 'cherry')}}} \tn % Row Count 26 (+ 4) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Create Tuple With One Item}}} \tn % Row Count 27 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{To create a tuple with only one item, \{\{nl\}\}you have to add a comma after the item, \{\{nl\}\}otherwise Python will not recognize it as a tuple.{\bf{\{\{nl\}\}thistuple = ("apple",) \{\{nl\}\}print(type(thistuple)) \{\{nl\}\}\#NOT a tuple \{\{nl\}\}thistuple = ("apple") \{\{nl\}\}print(type(thistuple)) \{\{nl\}\}output:: \{\{nl\}\}\textless{}class 'tuple'\textgreater{} \{\{nl\}\}\textless{}class 'str'\textgreater{}}}} \tn % Row Count 34 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Tuples In Python}} (cont)}} \tn % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Access Tuple Items}}} \tn % Row Count 1 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Print the second item in the tuple:\{\{nl\}\}{\bf{thistuple = ("apple", "banana", "cherry") \{\{nl\}\}print(thistuple{[}1{]})\{\{nl\}\}output::\{\{nl\}\}banana}}} \tn % Row Count 4 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Change Tuple Values}}} \tn % Row Count 5 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Once a tuple is created, you cannot change its values as they are called immutable.\{\{nl\}\}But there is a workaround. You can convert the tuple into a list, \{\{nl\}\}change the list, and convert the list back into a tuple.{\bf{\{\{nl\}\}x = ("apple", "banana", "cherry") \{\{nl\}\}y = list(x) \{\{nl\}\}y{[}1{]} = "kiwi" \{\{nl\}\}x = tuple(y) \{\{nl\}\}print(x)}}\{\{nl\}\}output::\{\{nl\}\}{\bf{("apple", "kiwi", "cherry")}}} \tn % Row Count 13 (+ 8) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{ Add tuple to a tuple.}}} \tn % Row Count 14 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}Create a new tuple with the value "orange", and add that tuple: {\bf{\{\{nl\}\}thistuple = ("apple", "banana", "cherry") \{\{nl\}\}y = ("orange",) \{\{nl\}\}thistuple += y \{\{nl\}\}print(thistuple)}}\{\{nl\}\}output:: \{\{nl\}\}{\bf{('apple', 'banana', 'cherry', 'orange')}}} \tn % Row Count 20 (+ 6) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Unpacking a Tuple}}} \tn % Row Count 21 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{When we create a tuple, we normally assign values to it. \{\{nl\}\}This is called "packing" a tuple: \{\{nl\}\} Packing a tuple: \{\{nl\}\}{\bf{fruits = ("apple", "banana", "cherry") \{\{nl\}\}print(fruits)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{('apple', 'banana', 'cherry')}}\{\{nl\}\}But, in Python, we are also allowed to extract the values back into variables. \{\{nl\}\}This is called "unpacking":\{\{nl\}\}unpack Tuple \{\{nl\}\}{\bf{fruits = ("apple", "banana", "cherry") \{\{nl\}\}(green, yellow, red) = fruits \{\{nl\}\}print(green) \{\{nl\}\}print(yellow) \{\{nl\}\}print(red)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{apple \{\{nl\}\}banana \{\{nl\}\}cherry}}} \tn % Row Count 33 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Tuples In Python}} (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Using Asterisk}}*} \tn % Row Count 1 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{If the number of variables is less than the number of values, \{\{nl\}\}you can add an * to the variable name and the values \{\{nl\}\}will be assigned to the variable as a list:\{\{nl\}\}\{\{nl\}\}Example:: \{\{nl\}\}{\bf{fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")}} \{\{nl\}\}{\bf{(green, yellow, *red) = fruits}} \{\{nl\}\}{\bf{print(green) \{\{nl\}\}print(yellow) \{\{nl\}\}print(red)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{apple \{\{nl\}\}banana \{\{nl\}\}{[}'cherry', 'strawberry', 'raspberry'{]}}}} \tn % Row Count 11 (+ 10) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Loop Through a Tuple}}} \tn % Row Count 12 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{You can loop through the tuple items by using a for loop.\{\{nl\}\}Example:: \{\{nl\}\}{\bf{thistuple = ("apple", "banana", "cherry") \{\{nl\}\}for x in thistuple: \{\{nl\}\} print(x)}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{apple \{\{nl\}\}banana \{\{nl\}\}cherry}}} \tn % Row Count 17 (+ 5) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Loop Through the Index Numbers In Tuples}}} \tn % Row Count 18 (+ 1) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Use the range() and len() functions to create a suitable iterable.\{\{nl\}\}Example:: \{\{nl\}\}{\bf{thistuple = ("apple", "banana", "cherry") \{\{nl\}\}for i in range(len(thistuple)): \{\{nl\}\} print(thistuple{[}i{]})}} \{\{nl\}\}output:: \{\{nl\}\}{\bf{apple \{\{nl\}\}banana \{\{nl\}\}cherry}}} \tn % Row Count 24 (+ 6) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Using a While Loop In Tuples}}} \tn % Row Count 25 (+ 1) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{nl\}\}Example:: \{\{nl\}\}{\bf{thistuple = ("apple", "banana", "cherry") \{\{nl\}\}i = 0 \{\{nl\}\} while i \textless{} len(thistuple): \{\{nl\}\} print(thistuple{[}i{]}) \{\{nl\}\} i = i + 1 }}\{\{nl\}\}output:: {\bf{\{\{nl\}\}apple \{\{nl\}\}banana \{\{nl\}\}cherry }}} \tn % Row Count 30 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Tuples In Python}} (cont)}} \tn % Row 28 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Join Two Tuples}}} \tn % Row Count 1 (+ 1) % Row 29 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{To join two or more tuples you can use the + operator:\{\{nl\}\}Example:: \{\{nl\}\}{\bf{tuple1 = ("a", "b" , "c") \{\{nl\}\}tuple2 = (1, 2, 3) \{\{nl\}\} tuple3 = tuple1 + tuple2 \{\{nl\}\} print(tuple3)}} \{\{nl\}\} output:: \{\{nl\}\}{\bf{('a', 'b', 'c', 1, 2, 3)}}} \tn % Row Count 6 (+ 5) % Row 30 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Multiply Tuples}}} \tn % Row Count 7 (+ 1) % Row 31 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{fruits = ("apple", "banana", "cherry") \{\{nl\}\}mytuple = fruits * 2 \{\{nl\}\} print(mytuple)}} \{\{nl\}\} output:: \{\{nl\}\}{\bf{('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')}}} \tn % Row Count 11 (+ 4) % Row 32 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Tuple count() Method}}} \tn % Row Count 12 (+ 1) % Row 33 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\{\{nl\}\}Example:: \{\{nl\}\}{\bf{thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5) \{\{nl\}\}x = thistuple.count(5) \{\{nl\}\} print(x)}} \{\{nl\}\} output:: \{\{nl\}\}{\bf{2}}} \tn % Row Count 15 (+ 3) % Row 34 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Tuple index() Method}}} \tn % Row Count 16 (+ 1) % Row 35 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Example:: \{\{nl\}\}{\bf{thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5) \{\{nl\}\}x = thistuple.index(8) \{\{nl\}\} print(x)}} \{\{nl\}\} output:: \{\{nl\}\}{\bf{3}}} \tn % Row Count 19 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{When creating a tuple with only one item, remember to include a comma after the item, otherwise it will not be identified as a tuple.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}