\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{Randomgirlll13} \pdfinfo{ /Title (python-3-basics.pdf) /Creator (Cheatography) /Author (Randomgirlll13) /Subject (Python 3 Basics 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 3 Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Randomgirlll13} via \textcolor{DarkBackground}{\uline{cheatography.com/186328/cs/38938/}}} \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}Randomgirlll13 \\ \uline{cheatography.com/randomgirlll13} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 7th June, 2023.\\ Updated 7th June, 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}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Python Basics}} \tn % Row 0 \SetRowColor{LightBackground} Single Line Comments & \#This is a comment on a single line \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Doc String: These appear right after a function definition & Def foo: \{\{nl\}\} '''this is a docstring, this will usually contain information about what is within the function''' \tn % Row Count 9 (+ 7) % Row 2 \SetRowColor{LightBackground} Arithmetic Operations: \{\{nl\}\} + is addition \{\{nl\}\} - is subtraction \{\{nl\}\} * is multiplication \{\{nl\}\} / is division \{\{nl\}\} \% is modulus (remainder) \{\{nl\}\} ** is exponentiation & Addition: result = 1 + 3 \{\{nl\}\} Subtraction: result = 1 - 3 \{\{nl\}\} Multiplication: result = 1 * 3 \{\{nl\}\} Division: result = 1 / 3 \{\{nl\}\} Modulus: result = 1 \% 3 \{\{nl\}\} Exponentiation: result = 1 ** 3 \tn % Row Count 19 (+ 10) % Row 3 \SetRowColor{white} Plus-Equals Operator += & counter = 0 \{\{nl\}\} counter += 10 \{\{nl\}\} This is Equivalent to: \{\{nl\}\} counter = 0 \{\{nl\}\} counter = counter + 10 \{\{nl\}\} **Note: This also works with strings \tn % Row Count 27 (+ 8) % Row 4 \SetRowColor{LightBackground} Variables: Unlike in many other programming languages, python does not require you to declare a type before assigning a value to the variable & user\_name = 'Randomgirlll13' \{\{nl\}\} id\_number = 2345 \{\{nl\}\} user\_verified = True \{\{nl\}\} \{\{nl\}\} user\_float = 14 \{\{nl\}\} user\_float = 14.55 \tn % Row Count 35 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Python Basics (cont)}} \tn % Row 5 \SetRowColor{LightBackground} String Concatenation & first = 'good' \{\{nl\}\} second = 'morning' \{\{nl\}\} sentence = first + last + '!' \tn % Row Count 5 (+ 5) % Row 6 \SetRowColor{white} Function print(): This outputs information to the user in the format of text & print('Hello World!') \{\{nl\}\} print(100 +300) \{\{nl\}\} print(14.555556) \tn % Row Count 9 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Loops}} \tn % Row 0 \SetRowColor{LightBackground} Break Keyword & In a loop, the break keyword will escape the loop \{\{nl\}\} \#Example \{\{nl\}\} nums = {[}0, 2, -3, 5, 7{]} \{\{nl\}\} for num in nums: \{\{nl\}\} if (num \textless{} 0): \{\{nl\}\} break \{\{nl\}\} this will only run to -3 before breaking out of the loop \tn % Row Count 10 (+ 10) % Row 1 \SetRowColor{white} List Comprehension & This is a concise way of creating lists. \{\{nl\}\} Syntax: list\_name = {[}Expression for Item in List \textless{}if Confiditonal\textgreater{}{]} \{\{nl\}\} The expressions can be anything. \{\{nl\}\} A list comprehension will ALWAYS return a list \tn % Row Count 19 (+ 9) % Row 2 \SetRowColor{LightBackground} For Loops & \#Example \{\{nl\}\} nums = {[}1,2,3,4,5{]} \{\{nl\}\} for num in nums: \{\{nl\}\} print(num) \tn % Row Count 23 (+ 4) % Row 3 \SetRowColor{white} Continue Keyword & The continue Keyword is inside a loop to skip the remaining code within the loop and begin the next loop iteration \tn % Row Count 28 (+ 5) % Row 4 \SetRowColor{LightBackground} Loops with range() function & Using the range() function, we can have a for loop that performs an action a specific number of times \{\{nl\}\} \#Example \{\{nl\}\} for i in range(3): \{\{nl\}\} print(i) \#Prints 0, 1, 2 \tn % Row Count 36 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Loops (cont)}} \tn % Row 5 \SetRowColor{LightBackground} While Loops & A while loop will repeatedly execute a code block as long as the condition is True \{\{nl\}\} hungry = True \{\{nl\}\} while hungry: \{\{nl\}\} print('I'm Hungry) \{\{nl\}\} hungry = False \tn % Row Count 8 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Modules}} \tn % Row 0 \SetRowColor{LightBackground} Importing Python Modules & The keyword import can be used to import python modules. \{\{nl\}\} \#Example \{\{nl\}\} import module \{\{nl\}\} module.function() \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} Module importing from file & To import from a file, provided it is in the same folder as the current file you are writing, you can import it as follows \{\{nl\}\} import filename \tn % Row Count 13 (+ 7) % Row 2 \SetRowColor{LightBackground} Aliasing with 'as' Keyword & The 'as' keyword can give an alias to a python module or function \{\{nl\}\} \#example \{\{nl\}\} from matplotlib import pyplot as plt \{\{nl\}\} plt.plot(x,y) \tn % Row Count 20 (+ 7) % Row 3 \SetRowColor{white} Random Module & The random module offers methods that simulate non-deterministic behavior in selecting numbers from a range \tn % Row Count 25 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Files}} \tn % Row 0 \SetRowColor{LightBackground} Python File Object & A python file object is created with the open() function. You can associate the file object with a variable using the with and as keywords \{\{nl\}\} with open('somefile.txt') as file\_object: \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} Python Read Method & After having opened a file with open(), call the .read() method to return the entire file contents as a Python string. \tn % Row Count 13 (+ 5) % Row 2 \SetRowColor{LightBackground} Python Readline Method & If you only want to read one line, use Readline() on the file object. This will extract one single line of text at a time \tn % Row Count 18 (+ 5) % Row 3 \SetRowColor{white} Python Readlines Method & Instead of getting a single string of text, readlines will return a list of strings representing individual lines in the file \tn % Row Count 23 (+ 5) % Row 4 \SetRowColor{LightBackground} Python Write to File & By default, all files opened are only for reading. To write to a file, you must open the file with a 'w' argument, then you can use the .write() method to write the file. \{\{nl\}\} **Note If the file already exists, all prior content will be overwritten. \{\{nl\}\} \{\{nl\}\} Example \{\{nl\}\} with open('text.txt', 'w') as text: \{\{nl\}\} text.write('This is example text') \tn % Row Count 38 (+ 15) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Files (cont)}} \tn % Row 5 \SetRowColor{LightBackground} Python Append to File & Since writing to an existing file will overwrite it, to keep the original contents, we can write to a file using append instead. To appead we pass it an 'a' argument in place of a 'w' \tn % Row Count 8 (+ 8) % Row 6 \SetRowColor{white} Class \seqsplit{csv.DictWriter} & the csv module implements classes to read and write data in CSV format. \{\{nl\}\} This has a class DictWrite which operates like a normal writer but will map a dictionary onto output rows. The keys of the dictionary are column names while values are actual data. \{\{nl\}\} \{\{nl\}\} csv.DictWriter constructor takes two arguments. first is the open file handler that CSV is written to. second is 'fieldnames', this is a list of field names that the CSV is going to handle. \tn % Row Count 27 (+ 19) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Control Flow Operations}} \tn % Row 0 \SetRowColor{LightBackground} Else If Statements & \#elif Statement \{\{nl\}\} pet\_type = 'fish' \{\{nl\}\} if pet\_type == 'dog': \{\{nl\}\} print("You have a dog.") \{\{nl\}\} elif pet\_type == 'fish': \{\{nl\}\} print("You have a fish.") \{\{nl\}\} else: \{\{nl\}\} print("Not Sure!") \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} Or Operator & True or True \#Evaluates to True \{\{nl\}\} True or False \#Evaluates to True \{\{nl\}\} False or False \#Evaluates to False \{\{nl\}\} 1 \textless{} 2 or 3 \textless{} 1 \#Evaluates to True \tn % Row Count 15 (+ 6) % Row 2 \SetRowColor{LightBackground} Equal Operator & == \{\{nl\}\} Used to compare two values, variables or expressions to determine if they are the same. \{\{nl\}\} if they are the same, it returns True. Otherwise, it will return False \tn % Row Count 22 (+ 7) % Row 3 \SetRowColor{white} Not Equals Operator & != \{\{nl\}\} This is used to compare two values, variables or expressions to see if they are not the same. \{\{nl\}\} If they are not the same, it returns True. Otherwise, it will return False \tn % Row Count 30 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Control Flow Operations (cont)}} \tn % Row 4 \SetRowColor{LightBackground} Comparison Operators & \textless{} \#Less than \{\{nl\}\} \textgreater{} \#Greater than \{\{nl\}\} \textless{}= \#less than or equal to \{\{nl\}\} \textgreater{}= \#Greater than or equal to \tn % Row Count 4 (+ 4) % Row 5 \SetRowColor{white} And Operator & True and True \#Evaluates to True \{\{nl\}\} True and False \#Evaluates to False \{\{nl\}\} False and False \#Evaluates to False \{\{nl\}\} 1 == 1 and 1 \textless{} 2 \#Evaluates to True \tn % Row Count 11 (+ 7) % Row 6 \SetRowColor{LightBackground} Not Operator & not True \#Evaluates to False \{\{nl\}\} not False \#Evaluates to True \{\{nl\}\} not 1 \textgreater{} 2 \#Evaluates to True \tn % Row Count 15 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} Functions & If a task will need to be performed multiple times, it is good practice to have it done within a function. \{\{nl\}\} In python these are defined with the 'def' keyword and then the name of your function \{\{nl\}\} \#Example \{\{nl\}\} def my\_function(x): \tn % Row Count 10 (+ 10) % Row 1 \SetRowColor{white} Function Parameters & Some functions require input to provide data to their code. These are known as parameters. A function can have no parameters, one parameter, or multiple parameters \{\{nl\}\} \#Examples \{\{nl\}\} def zero\_function(): \{\{nl\}\} def one\_function(number): \{\{nl\}\} def three\_function(age, height, weight): \tn % Row Count 21 (+ 11) % Row 2 \SetRowColor{LightBackground} Calling Functions & To call a function you simply have the name of the function and the arguments it needs \{\{nl\}\} \#Example \{\{nl\}\} zero\_function() \{\{nl\}\} one\_function(44) \tn % Row Count 27 (+ 6) % Row 3 \SetRowColor{white} Variable Scope & When it comes to the scope of variables, those not within a function are typically global variables, those within a function are local variables to that function and can be utilized in that function only. \{\{nl\}\} If you want to get a value back from a function you can use the special keyword return \tn % Row Count 39 (+ 12) \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}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} Syntax of Dictionaries in Python & example\_dictionary = \{"elem1": 1, "elem2": 2\} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Dictionary Value Types & In Python, the 'Value' type can be anything, the 'key' type must be a mutable data type \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} Accessing and Writing data in a dictionary & Values can be accessed by placing the key within square brackets next to the dictionary name \{\{nl\}\} print(example\_dictionary{[}"elem1"{]} \{\{nl\}\} To write a new value to a key it is the same syntax as accessing but with an = sign and what you'd like the new value to be \{\{nl\}\} example\_dictionary{[}"elem1"{]} = 3 \tn % Row Count 24 (+ 16) % Row 3 \SetRowColor{white} Merging Dictionaries with .update() & If two dictionaries need to be combined you can use the .update() function \{\{nl\}\} dict1 = \{1 : 'one'\} \{\{nl\}\} dict2 = \{2 : "two"\} \{\{nl\}\} dict1.update(dict2) \{\{n1\}\} //dict1 is now \{1 : 'one", 2 : 'two"\} \tn % Row Count 34 (+ 10) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Dictionaries (cont)}} \tn % Row 4 \SetRowColor{LightBackground} Dictionary key-value Methods & if you want to look the keys, values, or both of the dictionary, there are methods \{\{nl\}\} .keys() will return a list of the keys \{\{nl\}\} .values() will return a list of the values \{\{nl\}\} .items will return a list of tuples containing the key -value pairs \tn % Row Count 13 (+ 13) % Row 5 \SetRowColor{white} Dictionary get() Method & The get() method will return the value of a key if it exists otherwise it will return None if no default value is given for the key \tn % Row Count 20 (+ 7) % Row 6 \SetRowColor{LightBackground} Dictionary .pop() Method & .pop() will remove a key from a dictionary and return that keys value \tn % Row Count 24 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Classes}} \tn % Row 0 \SetRowColor{LightBackground} Instantiate Python Class & class Example: \{\{nl\}\} ""This is an empty class"" \{\{nl\}\} pass \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Python Class Variable & Class variables are defined locally within the class and outside of all methods. They have the same value for every instance of the class \{\{nl\}\} they can be access with instance.variable or class\_name.variable syntax \tn % Row Count 12 (+ 9) % Row 2 \SetRowColor{LightBackground} Python repr Method & The Python \_\_repr\_\_() method is used to tell Python what the string representation of the class should be. It only has one parameter, self, and it returns a string \tn % Row Count 19 (+ 7) % Row 3 \SetRowColor{white} Python Class Methods & In Python, methods are functions that are defined as part of a class. Common pract is that the first argument of any method that is part of a class is the actual object calling the method. This argument is usually called self \tn % Row Count 29 (+ 10) % Row 4 \SetRowColor{LightBackground} Python init Method & In Python, the \_\_init\_\_ method is used to initalize a newly created object. It will be called every time the class is instantiated \{\{nl\}\} class Animal: \{\{nl\}\} def \_\_init\_\_ (self, voice): \{\{nl\}\} self.voice = voice \{\{nl\}\} cat = Animal('Meow') \tn % Row Count 39 (+ 10) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Classes (cont)}} \tn % Row 5 \SetRowColor{LightBackground} Python type() function & the type() function will return the data type of the argument that was passed to it \tn % Row Count 4 (+ 4) % Row 6 \SetRowColor{white} Python dir() function & In Python, the dir() function, with no arguments, returns a list of all the attributes in current scope \{\{nl\}\} With an object as argument, dir() will try tor return all valid object attributes \tn % Row Count 12 (+ 8) % Row 7 \SetRowColor{LightBackground} \_main\_ in Python & In Python, \_\_main\_\_ is an identified used to reference the current file context \tn % Row Count 16 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} List Syntax & primes = {[}1,2,3,5,7,11{]} \{\{nl\}\} empty\_list = {[}{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Adding Lists Together & items = {[}'cake', 'cookie', 'pie'{]} \{\{nl\}\} total\_items = items + {[}'tart', 'cheesecake'{]} \{\{nl\}\} print(total\_items) \#Result: {[}'cake', 'cookie', 'pie', 'tart', 'cheesecake'{]} \tn % Row Count 11 (+ 9) % Row 2 \SetRowColor{LightBackground} Lists: Data Types & Lists can contain multiple types of data types within one list \tn % Row Count 14 (+ 3) % Row 3 \SetRowColor{white} List Method .append() & numbers = {[}11, 333, 44{]} \{\{nl\}\} numbers.append(22) \{\{nl\}\} print(numbers) \{\{nl\}\} \#Result: {[}11, 333, 44, 22{]} \tn % Row Count 19 (+ 5) % Row 4 \SetRowColor{LightBackground} List Indexing & In Python, Lists start at zero for the first index spot \{\{nl\}\} \#Example \{\{nl\}\} names = {[}'Lauren', 'Maria', 'Bailey'{]} \{\{nl\}\} 'Maria' is in the first index spot and 'Lauren' is in the zero index spot \tn % Row Count 28 (+ 9) % Row 5 \SetRowColor{white} Negative List Indexing & In Python, You can also access list elements using negative indices. \{\{nl\}\} \#Example \{\{nl\}\} names = {[}'Kim', 'Ashley', 'Hailey', 'Ginny'{]} \{\{nl\}\} names{[}-1{]} \# 'Ginny' \{\{nl\}\} names {[}-4{]} \# 'Kim' \tn % Row Count 37 (+ 9) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 6 \SetRowColor{LightBackground} List Method .remove() & This will remove the first occurrence of an element from a list in python \tn % Row Count 3 (+ 3) % Row 7 \SetRowColor{white} List Method .count() & This will return the number of times a certain element shows up in the list \tn % Row Count 6 (+ 3) % Row 8 \SetRowColor{LightBackground} Determining List Length & The len() function can be used to determine the number of items found in a list \{\{nl\}\} \#Example \{\{nl\}\} sack = {[}2, 4, 5, 6{]} \{\{nl\}\} size = len(sack) \{\{nl\}\} print(sack) \# 4 \tn % Row Count 13 (+ 7) % Row 9 \SetRowColor{white} List Method .sort() & This Method will sort the contents of the list in either ascending order (numerical lists), or alphabetical order (string lists) \tn % Row Count 19 (+ 6) % Row 10 \SetRowColor{LightBackground} List Slicing & This allows for only a portion of the list to be returned \{\{nl\}\} \#example \{\{nl\}\} tools = {[}'hammer', 'ruler', 'pen'{]} \{\{nl\}\} tools\_slice = tools{[}1:3{]} \# {[}'ruler', 'pen'{]} \{\{nl\}\} **Note: The original list will remain unaltered \tn % Row Count 29 (+ 10) % Row 11 \SetRowColor{white} Sorted() Function & This will take a list as the functions argument and will return a new sorted list without altering the original list \tn % Row Count 34 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 12 \SetRowColor{LightBackground} List Method .insert() & This allows us to add an element to a specific index into the list \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} List Method .pop() & This allows us to remove an element from the list and also return it \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} Escaping Characters & Backslashes (\textbackslash{}) are used to escape characters in Python Strings \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} In Syntax & The in syntax is used to determine if a letter or substring exists within a string. This will return True or False \{\{nl\}\} \#Example \{\{nl\}\} sentence = "Creating this has been a lot of work" \{\{nl\}\} print("work" in sentence) \#True \tn % Row Count 14 (+ 11) % Row 2 \SetRowColor{LightBackground} Indexing and Slicing Strings & Using the same notation as lists, you can index strings \{\{nl\}\} You can also get a substring from a string using slicing, the notation is string\_name{[}start:end{]} \tn % Row Count 21 (+ 7) % Row 3 \SetRowColor{white} Iterate Strings & To iterate through a string, utilize the for … in notation \{\{nl\}\} \#Example \{\{nl\}\} str = "hello" \{\{nl\}\} for c in str: \{\{nl\}\} print(c) \tn % Row Count 27 (+ 6) % Row 4 \SetRowColor{LightBackground} String len() function & This function can be used to determine the length of a string among other objects \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Strings (cont)}} \tn % Row 5 \SetRowColor{LightBackground} String Concatenation & To combine two strings, simply use the + operator \tn % Row Count 3 (+ 3) % Row 6 \SetRowColor{white} String Immutability & In python, strings are considered immutable, meaning once it has been defined, it cannot be changed \tn % Row Count 8 (+ 5) % Row 7 \SetRowColor{LightBackground} String .format() & This replaces empty braces (\{\}) placeholders in a string with the arguments passed. \{\{nl\}\} If keywords are specified within the placeholders, they are replaced with the corresponding named arguments \{\{nl\}\} \#Example \{\{nl\}\} msg1 = 'Mary had \{\} glasses of water and John had \{\} glasses of juice.' \{\{nl\}\} msg1.format(3, 2) \tn % Row Count 22 (+ 14) % Row 8 \SetRowColor{white} String Method .lower() & This will convert a string to all lowercase letters \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{LightBackground} String Method .strip() & This will remove characters from the beginning and end of a string. You can specify what characters to remove \tn % Row Count 30 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Strings (cont)}} \tn % Row 10 \SetRowColor{LightBackground} String Method .title() & This will return a string in title case \tn % Row Count 2 (+ 2) % Row 11 \SetRowColor{white} String Method .split() & This will split a string into a list of items based on arguments. \{\{nl\}\} If no arguments are passed, it uses white space, otherwise it will split based on whatever the argument passed is \tn % Row Count 11 (+ 9) % Row 12 \SetRowColor{LightBackground} String Method .find() & This will return the index of the first occurence of the string argument passed. If nothing is found, it will return -1 \tn % Row Count 17 (+ 6) % Row 13 \SetRowColor{white} String Method .replace() & This will replace the first occurrence of the first string argument with the second string argument \tn % Row Count 22 (+ 5) % Row 14 \SetRowColor{LightBackground} String Method .upper() & This will make the string all uppercase \tn % Row Count 24 (+ 2) % Row 15 \SetRowColor{white} String Method .join() & This concatenation a list of strings together with the desired delimiter \tn % Row Count 28 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}