\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 (cheat-sheet-for-python-topics.pdf) /Creator (Cheatography) /Author (srinivas.ram) /Subject (for Python Topics 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}{39A30B} \definecolor{LightBackground}{HTML}{F2F9EF} \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{for Python Topics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{srinivas.ram} via \textcolor{DarkBackground}{\uline{cheatography.com/183208/cs/38151/}}} \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 9th 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{\#Definition:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{A list is a mutable collection of ordered elements enclosed in square brackets {[}{]}. \{\{nl\}\}Lists can contain any type of data, including other lists.} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#Creating a List :}}} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Lists can be created using square brackets {[}{]} or the list() constructor.} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{ \#Using Square brackets:}}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}1, 2, 3{]}} \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using the list () constructor: }}} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = list({[}1, 2, 3{]}) \{\{nl\}\}myList = list(range(5)) \# a list with five items {[}0, 1, 2, 3, 4{]} \{\{nl\}\}myList = {[}i*2 for i in range(5){]} a list with five items {[}0, 2, 4, 6, 8{]}} \tn % Row Count 14 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Accessing Elements : }}} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Elements in a list can be accessed using indexing or slicing.} \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Accessing an element using indexing :}}} \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}1, 2, 3, 4, 5{]} \{\{nl\}\}Print(my\_list{[}0{]}) \# output: 1 accesses the first element in the list (1).} \tn % Row Count 21 (+ 3) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Accessing a slice of elements using slicing : }}} \tn % Row Count 23 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{print(my\_list{[}1:4{]})) \# Output: {[}2, 3, 4{]}} \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Some examples on indexing and slicing : }}} \tn % Row Count 25 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}'apple', 'banana', 'cherry'{]} \{\{nl\}\}print(my\_list{[}1{]}) \# Output: banana accesses the second element in the list ('banana'). \{\{nl\}\}print(my\_list{[}-1{]}) \# Output: cherry accesses the last element in the list ('cherry'). \{\{nl\}\}print(my\_list{[}1:{]}) \# Output: {[}'banana', 'cherry'{]} accesses all elements in the list from the second element to the end} \tn % Row Count 33 (+ 8) \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 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Modifying Elements : }}} \tn % Row Count 1 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Elements in a list can be modified using indexing or slicing. \{\{nl\}\}my\_list = {[}1, 2, 3{]} \{\{nl\}\} my\_list{[}0{]} = 4 \{\{nl\}\} print(my\_list) \# Output: {[}4, 2, 3{]}} \tn % Row Count 5 (+ 4) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Some Examples to modify elements using index and slice()}}} \tn % Row Count 7 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}'apple', 'banana', 'cherry'{]} \{\{nl\}\}my\_list{[}1{]} = 'orange' changes the second element in the list to 'orange'. \{\{nl\}\} my\_list.append('grape') adds 'grape' to the end of the list. \{\{nl\}\} my\_list.extend({[}'kiwi', 'watermelon'{]}) adds the list {[}'kiwi', 'watermelon'{]} to the end of the list. \{\{nl\}\} \seqsplit{my\_list.remove('cherry')} removes the element 'cherry' from the list. \{\{nl\}\} my\_list.pop(0) removes and returns the first element in the list ('apple').} \tn % Row Count 17 (+ 10) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{List Methods : }}} \tn % Row Count 18 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Lists have many built-in methods, including:} \tn % Row Count 19 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using append() method : }}} \tn % Row Count 20 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}1, 2, 3{]} \{\{nl\}\}my\_list.append(4) \{\{nl\}\}print(my\_list) \{\{nl\}\} \# Output: {[}1, 2, 3, 4{]}} \tn % Row Count 22 (+ 2) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using extend() method : }}} \tn % Row Count 23 (+ 1) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list.extend({[}5, 6{]}) \{\{nl\}\}print(my\_list) \{\{nl\}\}\# Output: {[}1, 2, 3, 4, 5, 6{]}} \tn % Row Count 25 (+ 2) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using insert() method : }}} \tn % Row Count 26 (+ 1) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list.insert(0, 0) \{\{nl\}\}print(my\_list) \{\{nl\}\}\# Output: {[}0, 1, 2, 3, 4, 5, 6{]}} \tn % Row Count 28 (+ 2) % Row 28 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using remove() method : }}} \tn % Row Count 29 (+ 1) % Row 29 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list.remove(3) \{\{nl\}\}print(my\_list) \{\{nl\}\}\# Output: {[}0, 1, 2, 4, 5, 6{]}} \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 30 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using pop() method : }}} \tn % Row Count 1 (+ 1) % Row 31 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list.pop(2) \{\{nl\}\}print(my\_list) \{\{nl\}\}\# Output: {[}0, 1, 4, 5, 6{]}} \tn % Row Count 3 (+ 2) % Row 32 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using sort() method : }}} \tn % Row Count 4 (+ 1) % Row 33 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list.sort() \{\{nl\}\}print(my\_list) \{\{nl\}\}\# Output: {[}0, 1, 4, 5, 6{]}} \tn % Row Count 6 (+ 2) % Row 34 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Using reverse() method : }}} \tn % Row Count 7 (+ 1) % Row 35 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list.reverse() \{\{nl\}\}print(my\_list) \{\{nl\}\}\# Output: {[}6, 5, 4, 1, 0{]}} \tn % Row Count 9 (+ 2) % Row 36 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Copying a List: }}} \tn % Row Count 10 (+ 1) % Row 37 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}'apple', 'banana', 'cherry'{]} \{\{nl\}\}new\_list = my\_list.copy()} \tn % Row Count 12 (+ 2) % Row 38 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Nested Lists: }}} \tn % Row Count 13 (+ 1) % Row 39 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}{[}1, 2{]}, {[}3, 4{]}{]}\{\{nl\}\}print(my\_list{[}0{]}{[}1{]}) \# Output: 2} \tn % Row Count 15 (+ 2) % Row 40 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{List Functions: }}} \tn % Row Count 16 (+ 1) % Row 41 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{sum(my\_list) \{\{nl\}\}all(my\_list) \{\{nl\}\}any(my\_list) \{\{nl\}\}enumerate(my\_list) \{\{nl\}\}zip(my\_list1, my\_list2)} \tn % Row Count 19 (+ 3) % Row 42 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{List Comprehensions :}}} \tn % Row Count 20 (+ 1) % Row 43 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{List comprehensions provide a concise way to create lists based on existing lists.} \tn % Row Count 22 (+ 2) % Row 44 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Creating a new list using list comprehension : }}} \tn % Row Count 24 (+ 2) % Row 45 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_list = {[}1, 2, 3, 4, 5{]} \{\{nl\}\}new\_list = {[}x * 2 for x in my\_list{]} \{\{nl\}\}print(new\_list) \# Output: {[}2, 4, 6, 8, 10{]} \{\{nl\}\}my\_list = {[}x for x in range(1, 6){]} \{\{nl\}\}even\_list = {[}x for x in range(1, 11) if x \% 2 == 0{]}} \tn % Row Count 29 (+ 5) % Row 46 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Advantage of Using Lists : }}} \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 Lists}} (cont)}} \tn % Row 47 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Lists are mutable, which makes them more flexible to use than tuples.} \tn % Row Count 2 (+ 2) \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{What are Tuples?}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{A tuple is an ordered, immutable collection of elements.\{\{nl\}\} In Python, tuples are created using parentheses () \{\{nl\}\}and the elements are separated by commas ,.} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Creating Tuples}}} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\# Create an empty tuple\{\{nl\}\}my\_tuple = ()\{\{nl\}\}\# Create a tuple with elements\{\{nl\}\}my\_tuple = (1, 2, 3)\{\{nl\}\}\# Create a tuple with a single element\{\{nl\}\}my\_tuple = (1,)\{\{nl\}\}\# Create a tuple without parentheses\{\{nl\}\}my\_tuple = 1, 2, 3} \tn % Row Count 11 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Accessing Elements}}} \tn % Row Count 12 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Tuples are ordered collections, \{\{nl\}\}So you can access individual elements using indexing.\{\{nl\}\}my\_tuple = ('apple', 'banana', 'cherry')} \tn % Row Count 15 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Access the first element}}} \tn % Row Count 16 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{print(my\_tuple{[}0{]}) \# Output: 'apple'} \tn % Row Count 17 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\# Access the last element}}} \tn % Row Count 18 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{print(my\_tuple{[}-1{]}) \# Output: 'cherry'} \tn % Row Count 19 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Immutability}}} \tn % Row Count 20 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Tuples are immutable, which means that \{\{nl\}\}you can't modify their contents after they're created.\{\{nl\}\}my\_tuple = (1, 2, 3)\{\{nl\}\}\# Trying to modify the first element will result in an error\{\{nl\}\}my\_tuple{[}0{]} = 4 \# TypeError: 'tuple' object does not support item assignment} \tn % Row Count 26 (+ 6) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Tuple Methods}}} \tn % Row Count 27 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Tuples have a few built-in methods that you can use:} \tn % Row Count 29 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{count()}}} \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{Tuples In Python}} (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Returns the number of times a specified value occurs in a tuple.\{\{nl\}\}my\_tuple = (1, 2, 2, 3, 2, 4)\{\{nl\}\}\# Count the number of times the value 2 appears\{\{nl\}\}print(my\_tuple.count(2)) \# Output: 3} \tn % Row Count 4 (+ 4) % Row 16 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{index()}}} \tn % Row Count 5 (+ 1) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Returns the index of the first occurrence of a specified value in a tuple.\{\{nl\}\}my\_tuple = (1, 2, 2, 3, 2, 4) \{\{nl\}\}\# Find the index of the first occurrence of the value 3\{\{nl\}\}print(my\_tuple.index(3)) \# Output: 3} \tn % Row Count 10 (+ 5) % Row 18 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Tuple Unpacking}}} \tn % Row Count 11 (+ 1) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{You can also "unpack" tuples, which allows \{\{nl\}\}you to assign the values in a tuple to separate variables.\{\{nl\}\}my\_tuple = ('John', 'Doe', 30) \{\{nl\}\}\# Unpack the tuple into separate variables \{\{nl\}\}first\_name, last\_name, age = my\_tuple\{\{nl\}\}print(first\_name) \# Output: 'John'\{\{nl\}\}print(last\_name) \# Output: 'Doe'\{\{nl\}\}print(age) \# Output: 30} \tn % Row Count 18 (+ 7) % Row 20 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Advantages of Tuples}}} \tn % Row Count 19 (+ 1) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Tuples are immutable, so they're useful for storing data that shouldn't be changed accidentally.\{\{nl\}\}Tuples are faster than lists, since they're smaller and can be stored more efficiently in memory.\{\{nl\}\}Tuples can be used as dictionary keys, while lists cannot.} \tn % Row Count 25 (+ 6) \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}{Dictionaries in Python}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Creating a dictionary}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\# Empty dictionary \{\{nl\}\}my\_dict = \{\} \{\{nl\}\}\# Dictionary with initial values\{\{nl\}\}my\_dict = \{"key1": "value1", "key2": "value2", "key3": "value3"\}\{\{nl\}\}\# Using the dict() constructor\{\{nl\}\}my\_dict = dict(key1="value1", key2="value2", key3="value3")} \tn % Row Count 6 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Accessing values}}} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\# Accessing a value by key\{\{nl\}\}my\_dict{[}"key1"{]} \# returns "value1"\{\{nl\}\}\# Using the get() method to avoid KeyError\{\{nl\}\}my\_dict.get("key1") \# returns "value1"\{\{nl\}\}my\_dict.get("key4") \# returns None\{\{nl\}\}\# Using the get() method with a default value\{\{nl\}\}my\_dict.get("key4", "default\_value") \# returns "default\_value"} \tn % Row Count 14 (+ 7) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Adding and updating values}}} \tn % Row Count 15 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\# Adding a new key-value pair \{\{nl\}\}my\_dict{[}"key4"{]} = "value4"\{\{nl\}\}\# Updating a value\{\{nl\}\}my\_dict{[}"key1"{]} = "new\_value1"\{\{nl\}\}\# Using the update() method to add/update multiple values\{\{nl\}\}my\_dict.update(\{"key5": "value5", "key6": "value6"\})} \tn % Row Count 20 (+ 5) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Removing values}}} \tn % Row Count 21 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\# Removing a key-value pair \{\{nl\}\}del my\_dict{[}"key1"{]}\{\{nl\}\}\# Using the pop() method to remove a key-value pair and return the valmy\_dict.pop("key2") \# returns "value2"ue\{\{nl\}\}\{\{nl\}\}\# Using the pop() method with a default value\{\{nl\}\}my\_dict.pop("key4", "default\_value") \# returns "default\_value"\{\{nl\}\}\# Using the clear() method to remove all key-value pairs\{\{nl\}\}my\_dict.clear()} \tn % Row Count 29 (+ 8) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Other methods}}} \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}{Dictionaries in Python (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\# Getting the number of key-value pairs \{\{nl\}\}len(my\_dict)\{\{nl\}\}\# Checking if a key exists in the dictionary\{\{nl\}\}"key1" in my\_dict\{\{nl\}\}\# Getting a list of keys\{\{nl\}\}my\_dict.keys()\{\{nl\}\}\# Getting a list of values\{\{nl\}\}my\_dict.values()\{\{nl\}\}\# Getting a list of key-value pairs as tuples\{\{nl\}\}my\_dict.items()} \tn % Row Count 7 (+ 7) % Row 10 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Advantages of Python dictionaries}}} \tn % Row Count 8 (+ 1) % Row 11 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Python dictionaries offer fast lookups, flexible key/value storage, \{\{nl\}\}dynamic resizing, efficient memory usage, and ease of use, making them a versatile\{\{nl\}\}and powerful data structure widely used in Python programming.} \tn % Row Count 13 (+ 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}{{\bf{Python strings}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Creating strings}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_string = "Hello, World!" \# double quotes\{\{nl\}\}my\_string = 'Hello, World!' \# single quotes\{\{nl\}\}my\_string = """Hello, World!""" \# triple quotes (for multiline strings)} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Accessing characters in a string}}} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_string = "Hello, World!"\{\{nl\}\}print(my\_string{[}0{]}) \# H\{\{nl\}\}print(my\_string{[}-1{]}) \# !} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Slicing strings}}} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_string = "Hello, World!"\{\{nl\}\}print(my\_string{[}0:5{]}) \# Hello\{\{nl\}\}print(my\_string{[}7:{]}) \# World!\{\{nl\}\}print(my\_string{[}:5{]}) \# Hello\{\{nl\}\}print(my\_string{[}::2{]}) \# Hlo ol!} \tn % Row Count 13 (+ 4) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String methods}}} \tn % Row Count 14 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_string = "Hello, World!"\{\{nl\}\}print(my\_string.upper()) \# HELLO, WORLD!\{\{nl\}\}print(my\_string.lower()) \# hello, world!\{\{nl\}\}print(my\_string.replace("Hello", "Hi")) \# Hi, World!\{\{nl\}\}print(my\_string.split(",")) \# {[}'Hello', ' World!'{]}\{\{nl\}\}print(my\_string.strip()) \# Hello, World! (remove whitespace)\{\{nl\}\}print(len(my\_string)) \# 13 (length of the string)} \tn % Row Count 23 (+ 9) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Concatenating strings}}} \tn % Row Count 24 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{str1 = "Hello"\{\{nl\}\}str2 = "World"\{\{nl\}\}print(str1 + " " + str2) \# Hello World} \tn % Row Count 26 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String formatting}}} \tn % Row Count 27 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{name = "John"\{\{nl\}\}age = 30\{\{nl\}\}print("My name is \{\} and I'm \{\} years old".format(name, age))\{\{nl\}\}\# My name is John and I'm 30 years old\{\{nl\}\}\# f-strings (Python 3.6+)\{\{nl\}\}print(f"My name is \{name\} and I'm \{age\} years old")\{\{nl\}\}\# My name is John and I'm 30 years old} \tn % Row Count 33 (+ 6) \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{Encoding and decoding strings}}} \tn % Row Count 1 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{my\_string = "Hello, World!"\{\{nl\}\}encoded\_string = \seqsplit{my\_string.encode("utf-8")} \# b'Hello, World!'\{\{nl\}\}decoded\_string = \seqsplit{encoded\_string.decode("utf-8")} \# Hello, World!} \tn % Row Count 5 (+ 4) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Advantages of strings in Python}}} \tn % Row Count 6 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Strings in Python have several advantages, including their flexibility,\{\{nl\}\}ease of use, and extensive library of built-in string methods,\{\{nl\}\}making it easy to manipulate and format text data for various purposes\{\{nl\}\} such as data analysis, web development, and automation tasks.} \tn % Row Count 12 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}