\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{PaolaP (pao361)} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (PaolaP (pao361)) /Subject (Python Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{178CA3} \definecolor{LightBackground}{HTML}{F0F7F9} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{PaolaP (pao361)} via \textcolor{DarkBackground}{\uline{cheatography.com/140352/cs/29763/}}} \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}PaolaP (pao361) \\ \uline{cheatography.com/pao361} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 8th January, 2022.\\ Updated 19th November, 2021.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Rules}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Python relies on proper indentation. \newline % Row Count 1 (+ 1) For example: \newline % Row Count 2 (+ 1) age = 18 \newline % Row Count 3 (+ 1) if age \textgreater{}=18: \newline % Row Count 4 (+ 1) ~ print("Be sure to vote") \newline % Row Count 5 (+ 1) else: \newline % Row Count 6 (+ 1) ~ print("Sorry, too young")% Row Count 7 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Naming Rules}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A variable name: MUST begin with a letter or underscore(\_)} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{CANNOT contain spaces, punctuation or special characters others than the underscore} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{CANNOT begin with a number} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{CANNOT be the same as a reserved keyword in Python such as print, True, else, etc} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A variable name is case sensitive} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{built-in functions}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{print()} & this outputs something to the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{input()} & ask for input from the program user \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} str() & converts a variable to a string data type \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} int() & convert a variable to an int data type \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{float()} & convert a variable to a float(decimal) data type \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{round()} & rounds a number \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Comparison Operators}} \tn % Row 0 \SetRowColor{LightBackground} == & Equal to \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} != & Not equal to \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textgreater{} & Greater than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textless{} & Less than \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textgreater{}= & Greater than or equal to \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textless{}= & Less than or equal to \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Math Operators}} \tn % Row 0 \SetRowColor{LightBackground} + & Addition \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & Subtraction \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & Multiplication \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / & Division \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \% & division remainder \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} ** & Exponent \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.54747 cm} x{4.42953 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} str & string(characteres typically words, sentences) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} int & integer(0,5,133) \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} float & decimal number(1.23,623.664) \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} list & a collection of variables (mango, banana, oranges) \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} bool & boolean value (True, False) \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.9954 cm} x{3.9816 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Special Characters}} \tn % Row 0 \SetRowColor{LightBackground} \textbackslash{}n & new line \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textbackslash{}t & tab \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.64701 cm} x{4.32999 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{LOCAL/GLOBAL Variables}} \tn % Row 0 \SetRowColor{LightBackground} LOCAL & Variable created within a function and only can be used by the function that defines them \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{GLOBAL} & Variable defined outside of a function and can be accessed by any function without passing them to the function. Read-only and cannot be modified \tn % Row Count 8 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.3731 cm} x{1.69349 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Boolean Operators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{not}} x & x {\bf{and}} y & x {\bf{or}} y \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{try and except}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{try:}} \newline % Row Count 1 (+ 1) ~ {\emph{code statements}} \newline % Row Count 2 (+ 1) {\bf{except: }} \#{\emph{for all exceptions}} \newline % Row Count 3 (+ 1) ~ {\emph{code statements}} \newline % Row Count 4 (+ 1) \{\{bt\}\} \newline % Row Count 5 (+ 1) {\bf{try:}} \newline % Row Count 6 (+ 1) ~ {\emph{code statements}} \newline % Row Count 7 (+ 1) {\bf{except ValueError: }} \#{\emph{Specific error type}} \newline % Row Count 8 (+ 1) ~ {\emph{code statements}}% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Concatenate using "+" or "f"}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{combining strings}} \newline % Row Count 1 (+ 1) myName = "Paola" \newline % Row Count 2 (+ 1) print("Hello " + myName) \newline % Row Count 3 (+ 1) print(f"Hello \{myName\}") \newline % Row Count 4 (+ 1) {\bf{string and a numeric value}} \newline % Row Count 5 (+ 1) age= 22 \newline % Row Count 6 (+ 1) print("Your age: " + age) \newline % Row Count 7 (+ 1) print(f"Your age: \{age\}")% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Capital and lowercase letters}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{hello = "hello world" \newline \newline print(hello.upper()) \newline \# will print HELLO WORLD \newline print(hello.lower()) \newline \# will print hello world \newline print(hello.capitalize()) \newline \# will print Hello world} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Control loops}} \tn % Row 0 \SetRowColor{LightBackground} break & breaks out of your loop causing the program to move to the next line after the loop \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{continue} & while skip this round of the loop and go into the next loop iteration \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Statements}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{If Statement}} \newline % Row Count 1 (+ 1) if {\emph{expression}}: \newline % Row Count 2 (+ 1) ~{\emph{statements}} \newline % Row Count 3 (+ 1) elif {\emph{expression}}: \newline % Row Count 4 (+ 1) ~{\emph{statements}} \newline % Row Count 5 (+ 1) else: \newline % Row Count 6 (+ 1) ~{\emph{statements}} \newline % Row Count 7 (+ 1) {\bf{While Loop}} \newline % Row Count 8 (+ 1) while {\emph{expression}}: \newline % Row Count 9 (+ 1) ~{\emph{statements}} \newline % Row Count 10 (+ 1) {\bf{For Loop}} \newline % Row Count 11 (+ 1) for {\emph{var}} in {\emph{collection}}: \newline % Row Count 12 (+ 1) ~{\emph{statements}} \newline % Row Count 13 (+ 1) {\bf{Counting For Loop}} \newline % Row Count 14 (+ 1) for {\emph{i}} in range({\emph{start}}, {\emph{end}} {[}, {\emph{step}}{]}): \newline % Row Count 15 (+ 1) ~statements \newline % Row Count 16 (+ 1) ({\emph{start}} is included; {\emph{end}} is not)% Row Count 17 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{if statements}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{if myAge \textless{} 18: \newline print("Too young") \#If TRUE prints this \newline elif my Age \textless{}21: \newline print("Go ahead") \#If TRUE prints this \newline else: \newline print("Bye!") \#if FALSE prints this} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{While loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#while loops run as long as, or while, a certain condition is true \newline \newline while True: \newline \#do something \newline else: \newline \#do something \newline \newline \#Example: \newline current\_number = 1 \#set the first value \newline \#check the value of current\_number and see if it is less than or equal to 5 \newline while current\_number \textless{}=5: \newline \newline print(current\_number) \#print out the value of the variable \newline current\_number += 1 \#add one to the variable} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The loop will run again until the current\_value variable becomes 6 and then it will stop. Use break and continue to control loop} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{for loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{colors = {[}'red', 'green', 'blue'{]} \newline \#colors is a list data type \newline \newline for color in colors: \newline \#name each individual item color within the colors list so that you can output the individual variable \newline print(color)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{write() method example}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{**Opening in append mode will add the new data to the end of the file" \newline % Row Count 2 (+ 2) with open ("{\emph{filename.txt}}, "a") as File: \newline % Row Count 3 (+ 1) ~ File.write("Hello\textbackslash{}n")% Row Count 4 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.09494 cm} x{3.88206 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Read methods}} \tn % Row 0 \SetRowColor{LightBackground} read() & read the entire file and return its contents as a string \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{readlines()} & read the entire file and return its contents as a list \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{readline()} & read the next line in the file and returns its content as a string \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{read() adn readlines() work best for smaller files. readline() for larger files.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Definition}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Function named blocks of code that are designed to do a specific task \newline % Row Count 2 (+ 2) def {\emph{name}}({\emph{arg1}}, {\emph{arg2}}, ...): \newline % Row Count 3 (+ 1) {\emph{code statements}} \newline % Row Count 4 (+ 1) return {\emph{expr}}% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{return: stores the variable \newline It can be with arguments or without it} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions Example}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Function definition with NO arguments/parameters}} \newline % Row Count 2 (+ 2) def helloWorld(): \newline % Row Count 3 (+ 1) ~print("Hello, world!") \newline % Row Count 4 (+ 1) {\bf{Function definition WITH arguments/parameters}} \newline % Row Count 5 (+ 1) def helloUser({\emph{firstName}}): \newline % Row Count 6 (+ 1) ~ print("Hello", {\emph{firstName}}) \newline % Row Count 7 (+ 1) {\bf{Calling a function}} \newline % Row Count 8 (+ 1) helloWorld()% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.89586 cm} x{4.08114 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{LISTS/TUPLE}} \tn % Row 0 \SetRowColor{LightBackground} List {[} {]} & Collection of items in a particular order. List indexes start at 0 \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Tuple ( ) & It is a list but Unable to be changed \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists functions Example}} \tn % Row 0 \SetRowColor{LightBackground} fruits = & {[}'apple', 'banana' , 'orange'{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} print(fruits) & Output an entire list \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} print(fruits{[}2{]}) & Output an element in a list: orange \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} fruits{[}0{]} = 'grapes' & Modifying an element in a list: apple by grapes \tn % Row Count 8 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{fruits.append('pear')} & Adding an element to the end of a list \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} fruits.insert(0, 'mango') & adding a list element in a specific position \tn % Row Count 13 (+ 3) % Row 6 \SetRowColor{LightBackground} \seqsplit{fruits.remove('banana')} & removing a list element \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} fruits.pop(0) & removing a specific list element \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} fruits.pop() & removing the last list element \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} del fruits & removing an entire list \tn % Row Count 21 (+ 2) % Row 10 \SetRowColor{LightBackground} fruits.clear() & emptying a list \tn % Row Count 22 (+ 1) % Row 11 \SetRowColor{white} findApple = \seqsplit{(fruits.count("apple"))} & count for specific item \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{LightBackground} fruits. reverse() & reverse the order of list \tn % Row Count 26 (+ 2) % Row 13 \SetRowColor{white} fruits.sort() & sort the list. \seqsplit{fruits.sort(key=str.lower)} to make sure everything is in lowercase \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists functions Example (cont)}} \tn % Row 14 \SetRowColor{LightBackground} sorted\_fruits = sorted(fruits) & If you want the list to remain the same positions, you can use the sorted to create a copy of the sorted list without impacting the original list \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.14471 cm} x{3.83229 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Types of files}} \tn % Row 0 \SetRowColor{LightBackground} Text files & each line ends with a new line character (\textbackslash{}n) or a carriage return character (\textbackslash{}r) on Windows systems \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Binary files & Are intended to be read by other programs, not humans. common types are: program files, image files, audio files, video files, database files and compressed files. \tn % Row Count 10 (+ 6) \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}{File fuctions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{open({\emph{filename}}, mode)}} & {\bf{mode}} is an optional argument that specifies how you want to open the file. r = read, a = append, w = write, b = binary. \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} {\emph{filename}}.{\bf{close()}} & close an open file object \tn % Row Count 9 (+ 2) % Row 2 \SetRowColor{LightBackground} print({\emph{filename}}.{\bf{read()}}) & output the content of the file \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} {\bf{with}} open({\emph{filename}}) as {\emph{newfilename}}: & automatically close a file if an exception happens. Also, it allows to assign a name to the file object in the same line of code and ends with a colon: creating a code block \tn % Row Count 20 (+ 9) % Row 4 \SetRowColor{LightBackground} write() method & use write mode when you are creating a new file, not when you are working with an existing file of data, Open the file in append mode ("a") if you wish to add to an existing file. \tn % Row Count 29 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{A file path must be included if the file is not in the same directory as the Python program} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}