\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{Redjumpman} \pdfinfo{ /Title (python-essentials.pdf) /Creator (Cheatography) /Author (Redjumpman) /Subject (Python Essentials 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}{0277BA} \definecolor{LightBackground}{HTML}{EFF6FA} \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 Essentials Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Redjumpman} via \textcolor{DarkBackground}{\uline{cheatography.com/35253/cs/17793/}}} \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}Redjumpman \\ \uline{cheatography.com/redjumpman} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 15th November, 2018.\\ Updated 13th November, 2018.\\ 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{tabularx}{17.67cm}{x{4.5549 cm} x{6.2419 cm} x{6.0732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{str.split(sep)} & Splits a string by the separator and returns a list of strings & "1,3,4,5".split(',') {\bf{-{}-\textgreater{}}} {[}"1", "2", "3"{]} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \seqsplit{str.strip()} & Removes leading and trailing whitespace & " hello ".strip() -{}-\textgreater{} "hello" \tn % Row Count 8 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{str.replace(old}, new) & Replaces all {\emph{old}} with {\emph{new}} in the string & "Hello \seqsplit{Moon".replace("Moon"}, "World") -{}-\textgreater{} "Hello World" \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} \seqsplit{str.join(iterable)} & concatenates a list of strings. & ", ".join({[}"1", "2", "3"{]}) -{}-\textgreater{} "1, 2, 3" \tn % Row Count 15 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{str.startswith(string)} & Returns True if str starts with {\emph{string}} & \seqsplit{"Hello".startswith("Hel")} -{}-\textgreater{} True \tn % Row Count 18 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{str.endswith(string)} & Returns True if str endswith {\emph{string}} & \seqsplit{"Hello".endswith("o")} -{}-\textgreater{} True \tn % Row Count 21 (+ 3) % Row 6 \SetRowColor{LightBackground} \seqsplit{str.lower()} & Returns a string with all characters in lowercase & \seqsplit{"ALLCAPS".lower()} -{}-\textgreater{} "allcaps" \tn % Row Count 25 (+ 4) % Row 7 \SetRowColor{white} \seqsplit{str.upper()} & Returns a string with all characters in uppercase & "loud \seqsplit{noises".upper()} -{}-\textgreater{} "LOUD NOISES" \tn % Row Count 29 (+ 4) % Row 8 \SetRowColor{LightBackground} \seqsplit{str.find(char)} & Returns the first index in the string where char is found, otherwise returns -1. & \seqsplit{"abracadbra".find("d")} -{}-\textgreater{} 6 \tn % Row Count 35 (+ 6) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.5549 cm} x{6.2419 cm} x{6.0732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Strings (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \seqsplit{str.count(char)} & Returns an integer of the number of occurances of char in str. & \seqsplit{"abracadbra".count("a")} -{}-\textgreater{} 4 \tn % Row Count 5 (+ 5) % Row 10 \SetRowColor{white} \seqsplit{str.format(*args}, **kwargs) & Formats a string according to your specifiers & "The \{\} costs \{\} \seqsplit{dollars.".format("movie"}, 10) -{}-\textgreater{} "The movie costs 10 dollars." \tn % Row Count 11 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Strings are immutable \newline You can concatenate strings together with the + operator \newline Strings are iterable and thus can be looped over. \newline Reverse a string with my\_string{[}::-1{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.5427 cm} x{6.5793 cm} x{6.748 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Lists (Assume my\_list = {[}1, 2, 3{]})}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{list.append(element)} & Appends an element to a list & \seqsplit{my\_list.append(4)} -{}-\textgreater{} {[}1, 2, 3, 4{]} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{list.extend(sequence)} & Extends a list by adding another list to the end & my\_list.extend({[}4,5,6{]}) -{}-\textgreater{} {[}1, 2, 3, 4, 5, 6{]} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{list.pop(index)} & Removes and returns an element in a list by index. & my\_list.pop(1) -{}-\textgreater{} 2 \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} \seqsplit{list.count(element)} & Counts the occurences of element in the list & \seqsplit{my\_list.count(3)} -{}-\textgreater{} 1 \tn % Row Count 14 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{list.index(element)} & Returns the index of the element in the list & \seqsplit{my\_list.index(3)} -{}-\textgreater{} 2 \tn % Row Count 17 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{list.clear()} & Empties out a list & \seqsplit{my\_list.clear()} -{}-\textgreater{} {[}{]} \tn % Row Count 19 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{list.remove(element)} & Removes an element from a list & \seqsplit{my\_list.remove(1)} -{}-\textgreater{} {[}2, 3{]} \tn % Row Count 22 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{list.sort()} & sorts a list & \seqsplit{my\_list.sort(reverse=True)} -{}-\textgreater{} {[}3, 2, 1{]} \tn % Row Count 25 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Lists are mutable \newline You can access an element in a list via the index: my\_list{[}index{]} \newline Update a value at a specific index with my\_list{[}index{]} = "new" \newline Create a copy of a list with slicing: my\_list{[}:{]} \newline Reverse a list with my\_list{[}::-1{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.2175 cm} x{6.4106 cm} x{6.2419 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Dictionaries (Assume d = \{"a": 1, "b": 2, "c": 3\})}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{dict.copy()} & Returns a {\bf{shallow}} copy of the dictionary & copied = d.copy() \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{dict.values()} & Returns a dictionary view of all the values & d.values() -{}-\textgreater{} dict\_view({[}1, 2, 3{]}) \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{dict.items()} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{dict.keys()} & Returns a dictionary view of all the keys & d.keys() -{}-\textgreater{} dict\_view({[}'b', 'a', 'c'{]}) \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{dict.get(key}, \seqsplit{default=None)} & Attempts to return the value of the key specified, otherwise it returns the default & d.get("X", "not valid") -{}-\textgreater{} "not valid" \tn % Row Count 16 (+ 6) % Row 5 \SetRowColor{white} \seqsplit{dict.pop(key}, default) & Pops a key. Returns a key error if default is not specified and the key dne. & d.pop('a') -{}-\textgreater{} 1 \tn % Row Count 22 (+ 6) % Row 6 \SetRowColor{LightBackground} \seqsplit{dict.popitem()} & Pops a random key value pair from the dictionary & d.popitem() -{}-\textgreater{} ("b", 2) \tn % Row Count 26 (+ 4) % Row 7 \SetRowColor{white} \seqsplit{dict.update(iterable\_pair)} & Updates the dictionary when given a iterable pairing. & d.update(\{"z": 4\}) -{}-\textgreater{} \{"a": 1, "b": 2, "c": 3\}\{\{nl\}\} d.update({[}('x', 3){]}) -{}-\textgreater{} \{"a": 1, "b": 2, "c": 3, "x": 3\} \tn % Row Count 34 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Assume the keys in a dictionary will be unordered. \newline \newline All keys {\bf{must}} be hashable. If something is immutable (strings, tuples, etc), then it can be a key. Things like sets and lists cannot be keys. \newline \newline Dictionaries are mutable \newline Access the value of a key with d{[}key{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7114 cm} x{6.5793 cm} x{6.5793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{File Operations}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{f.close()} & Closes an open file & f.close() \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{f.read(n)} & reads up to n characters in the file, otherwise the entire file if n is not specified. & f.read() -{}-\textgreater{} "Hello World\textbackslash{}n" \tn % Row Count 8 (+ 6) % Row 2 \SetRowColor{LightBackground} \seqsplit{f.readlines()} & Returns a list of strings. Each line in the file is put in a list & f.readlines() -{}-\textgreater{} {[}"Hello World\textbackslash{}n", "Goodbye World\textbackslash{}n"{]} \tn % Row Count 13 (+ 5) % Row 3 \SetRowColor{white} \seqsplit{f.write(content)} & Writes the content to the file & f.write("Hello World") \tn % Row Count 15 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{f.writelines(iterable)} & Writes a list of lines to the file & f.writelines({[}"hello world", "goodbye world"{]}) \tn % Row Count 19 (+ 4) % Row 5 \SetRowColor{white} \seqsplit{f.seek(offset}, from) & Moves the current position of the file & f.seek(5) -{}-\textgreater{} Move five characters ahead\{\{nl\}\} f.seek(-5, 0) -{}-\textgreater{} Start at the first character and move back 5. \tn % Row Count 27 (+ 8) % Row 6 \SetRowColor{LightBackground} \seqsplit{f.tell()} & Returns the current position in the file, by counted characters & f.tell() -{}-\textgreater{} 13 \tn % Row Count 32 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7114 cm} x{6.5793 cm} x{6.5793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{File Operations (cont)}} \tn % Row 7 \SetRowColor{LightBackground} \seqsplit{open(file\_path}, 'mode') & opens a file at the given file path & returns the raw, unread file. \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Always close the file {\bf{RIGHT}} after you read it. Not 50 lines later. \newline When using the with statement, you do not need to close the file. \newline readline() will return the first line. If you run it again, it returns the next line. \newline Each line ends in a "\textbackslash{}n" \newline Reading a file always returns a string or a list of strings.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.5549 cm} x{6.0732 cm} x{6.2419 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Sets (Assume s = \{1, 2, 3, 4\})}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{s.isdisjoint(other)} & Returns True if s and other do not share any elements. & s.isdisjoint(\{7, 9\}) -{}-\textgreater{} True \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \seqsplit{s.issubset(other)} & Returns True if all elements of s is in other. & s.issubset(\{1, 2, 3, 4, 5\}) -{}-\textgreater{} True \{\{nl\}\} s \textless{}= \{1, 2, 3, 4, 5\} -{}-\textgreater{} True \tn % Row Count 10 (+ 6) % Row 2 \SetRowColor{LightBackground} \seqsplit{s.issuperset(other)} & Return True if all elements other is in s. & s.issuperset(\{1, 4\}) -{}-\textgreater{} True \{\{nl\}\} s \textgreater{}= \{1, 4\} -{}-\textgreater{} True \tn % Row Count 15 (+ 5) % Row 3 \SetRowColor{white} \seqsplit{s.union(other)} & Combines all unique elements & s.union(\{2, 3, 7\}) -{}-\textgreater{} \{1, 2, 3, 4, 7\} \{\{nl\}\} s | \{2, 3, 7\} -{}-\textgreater{} \{1, 2, 3, 4, 7\} \tn % Row Count 21 (+ 6) % Row 4 \SetRowColor{LightBackground} \seqsplit{s.intersection(other)} & Set of elements common to both sets & s.intersection(\{1, 3, 5, 7\}) -{}-\textgreater{} \{1, 3\} \{\{nl\}\} s \& \{1, 3, 5\} -{}-\textgreater{} \{1, 3\} \tn % Row Count 27 (+ 6) % Row 5 \SetRowColor{white} \seqsplit{s.difference(other)} & Returns any elements that exists in s, but not in other. & s.difference(\{2, 3, 7\}) -{}-\textgreater{} \{1, 4\} \{\{nl\}\} s - \{2, 3, 7\} -{}-\textgreater{} \{1, 4\} \tn % Row Count 32 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.5549 cm} x{6.0732 cm} x{6.2419 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Sets (Assume s = \{1, 2, 3, 4\}) (cont)}} \tn % Row 6 \SetRowColor{LightBackground} \seqsplit{s.symmetric\_difference(other)} & Returns all the elements that are not shared between the sets & s.symmetric\_difference(\{2, 9\}) -{}-\textgreater{} \{1, 3, 4, 9\} \{\{nl\}\} s \textasciicircum{} \{2, 9\} -{}-\textgreater{} \{1, 3, 4, 9\} \tn % Row Count 6 (+ 6) % Row 7 \SetRowColor{white} s.copy() & Returns a shallow copy of the set. & s.copy() -{}-\textgreater{} \{1, 2, 3, 4\} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Frozen sets are immutable, but regular sets are mutable \newline Sets are {\bf{not}} ordered \newline sets only contain unique values \newline Create sets with curly brackets -{}-\textgreater{} \{1, 2, 3\} \newline Create empty sets {\bf{ONLY}} with set(). an empty \{\} is an empty dictionary.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \end{document}