\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{Jordi Losantos (jlosantos)} \pdfinfo{ /Title (python-eso-ii-aula.pdf) /Creator (Cheatography) /Author (Jordi Losantos (jlosantos)) /Subject (Python ESO II AULA 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}{E67F43} \definecolor{LightBackground}{HTML}{FDF7F3} \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 ESO II AULA Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jordi Losantos (jlosantos)} via \textcolor{DarkBackground}{\uline{cheatography.com/139908/cs/29601/}}} \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}Jordi Losantos (jlosantos) \\ \uline{cheatography.com/jlosantos} \\ \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 25th February, 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Basics}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Spaces and indentation (tabs) are relevant parts of the code} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Instructions in a {\emph{block of code}} will have the same indentation} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A {\emph{block of code}} contains one or more lines of code inside it. The contained lines will be indented one more level than the container one.} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{A colon {\bf{:}} opens a new {\emph{block of code}} in the following line.} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{\#}} at the beginning of a line marks this line as non-executable. For example} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{\# This is a single line comment}}} \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To create multiple line comments, use three apostrophees in a row} \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{'''}}} \tn % Row Count 15 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{This is a}}} \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{Multiline comment}}} \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{'''}}} \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} There's no declaration of variables. When you use a new name in an assign sentence, that becomes a variable of the type of the value assigned to it & {\bf{a = 10}} creates a variable called {\emph{a}} that stores an {\emph{integer}} value \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} The decimal separator is the point & {\bf{a = 3.14}} creates a variable called {\emph{a}} that stores a decimal ({\emph{float}}) value \tn % Row Count 12 (+ 4) % Row 2 \SetRowColor{LightBackground} To transform a numerical value into a string you {\emph{cast}} it using the function {\bf{str()}} & {\bf{str(10)}} will create the string value {\bf{"10"}} \tn % Row Count 17 (+ 5) % Row 3 \SetRowColor{white} To transform a string value into an integer you {\emph{cast}} it using the function {\bf{int()}} & {\bf{int("10")}} will create the integer value {\bf{10}} \tn % Row Count 22 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{A variable contains a literal value of a certain type (integer, alphanumeric, decimal, boolean, etc) and it can be used to perform different operations or construct logical expressions} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.128 cm} x{2.736 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Basic input / output}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{print(a)}} & Prints the content of the variable called {\emph{a}} and opens a new line & \{\{nobreak\}\}{\bf{print}}("Helo world")\{\{nl\}\}{\bf{Hello word}}\{\{nl\}\}and opens a new line \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \{\{nobreak\}\}{\bf{print(a,end=",")}} & Prints the content of the variable called {\emph{a}} and then a comma without opening a new line & \{\{nobreak\}\}{\bf{print}}(name,end=",")\{\{nl\}\}{\bf{My name,}} \tn % Row Count 13 (+ 7) % Row 2 \SetRowColor{LightBackground} {\bf{input()}} & Reads a {\bf{string}} value form the keyboard & {\bf{a = input()}} will store in a variable called {\emph{a}} the value entered by the user as a {\bf{string}} \tn % Row Count 20 (+ 7) % Row 3 \SetRowColor{white} {\bf{int(input())}} & Reads a {\bf{string}} value form the keyboard, ant {\emph{casts}} it into an {\bf{integer}} & {\bf{a = int(input())}} will store in a variable called {\emph{a}} the value entered by the user casted as an {\bf{integer}} \tn % Row Count 28 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Flow control (more to be added along the course)}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{if}}~({\emph{\textless{}expr\textgreater{}}})~{\bf{:}} & Forks the execution stream according to the logical value of the expression {\emph{\textless{}expr\textgreater{}}} & \{\{nobreak\}\}{\bf{if}} (a == b){\bf{:}}\{\{nl\}\}~~\textless{}do something\textgreater{} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} {\bf{else:}} & As part of an {\emph{if .. else}} block, starts the block code to be executed if the {\emph{expresion}} was {\bf{False}} & \{\{nobreak\}\}{\bf{else:}}\{\{nl\}\}~~\textless{}do something esle\textgreater{} \tn % Row Count 15 (+ 8) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{elif}}~({\emph{\textless{}expr2\textgreater{}}}){\bf{:}} & Compound an {\emph{else:}} statement with a new {\emph{if}} statement & \{\{nobreak\}\}{\bf{elif}} (a\textless{}b){\bf{:}}\{\{nl\}\}~~\textless{}and another\textgreater{} \tn % Row Count 20 (+ 5) % Row 3 \SetRowColor{white} \{\{nobreak\}\}{\bf{while}}~({\emph{\textless{}expr\textgreater{}}}){\bf{:}} & Generates a {\emph{loop}} that will run as long as the expression {\emph{\textless{}expr\textgreater{}}} is {\bf{True}} & \{\{nobreak\}\}{\bf{while}} (a\textless{}10){\bf{:}}\{\{nl\}\}~~\textless{}do something\textgreater{}\{\{nl\}\}~~\textless{}update {\emph{a}}\textgreater{} \tn % Row Count 28 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.76 cm} x{3.42 cm} x{3.42 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Arithmetic operators}} \tn % Row 0 \SetRowColor{LightBackground} + & add & 12 + 5 returns 17 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & subtract & 12 - 5 returns 7 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & product & 12 * 5 returns 60 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / & decimal division & 12 / 5 returns 2.4 \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} // & division (whole numbers) & 12 // 5 returns 2 \tn % Row Count 6 (+ 2) % Row 5 \SetRowColor{white} \% & remainder of the division & 12 \% 5 returns 2 \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} ** & exponentiation & 12 ** 5 returns 248832 \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.76 cm} x{3.268 cm} x{3.572 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Comparison operators (logical)}} \tn % Row 0 \SetRowColor{LightBackground} \textless{} & less than & 12 \textless{} 5 evaluates as {\bf{False}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \textless{}= & less than or equal to & 12 \textless{}= 5 evaluates as {\bf{False}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} == & equal to & 12 == 5 evaluates as {\bf{False}} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \textgreater{}= & greater than or equal to & 12 \textgreater{}= 5 evaluates as {\bf{True}} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \textgreater{} & greater than & 12 \textgreater{} 5 evaluates as {\bf{True}} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} != & not equal to & 12 != 5 evaluates as {\bf{True}} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{In a comparison, the sign of equality ({\bf{=}}) can never be alone as it would be confused with the assignment of values ({\emph{a = 10}}). This is why the logical equality operator is a double sign of equality. Therefore {\bf{a=10}} means {\emph{assign the value 10 to the variable a}} and {\bf{a==10}} means {\emph{is the content of the variable a a number 10?}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To be added next course} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.596 cm} x{2.964 cm} x{3.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Maths}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{abs({\emph{arg}})}} & receives an {\bf{integer}} number as an argument and returns the integer absolute value & {\bf{abs(-12)}} returns 12 \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{math.fabs({\emph{arg}})}} & receives a {\bf{float}} as an argument and returns the {\bf{float}} absolute value & {\bf{math.fabs(-12.34) }} returns 12.34 \{\{nl\}\}{\bf{math.fabs(-12) }} returns 12.0 \tn % Row Count 12 (+ 6) % Row 2 \SetRowColor{LightBackground} {\bf{math.floor({\emph{arg}})}} & receives a {\bf{float}} as an argument and rounds it down to the nearest {\bf{integer}} & {\bf{math.floor(2.5)}} returns 2\{\{nl\}\}{\bf{math.floor(-3.4)}} returns -4 \tn % Row Count 18 (+ 6) % Row 3 \SetRowColor{white} {\bf{math.ceil({\emph{arg}})}} & receives a {\bf{float}} as an argument and rounds it up to the nearest {\bf{integer}} & {\bf{math.ceil(2.5)}} returns 3\{\{nl\}\}{\bf{math.ceil(-3.4)}} returns -3 \tn % Row Count 24 (+ 6) % Row 4 \SetRowColor{LightBackground} {\bf{math.pi}} & returns the value of Pi & {\bf{math.pi}} returns \seqsplit{3.141592653589793} \tn % Row Count 27 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{You will need to {\bf{import math}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.736 cm} x{2.812 cm} x{2.052 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Logical operators}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}\textless{}expr1\textgreater{}~{\bf{and}}~\textless{}expr2\textgreater{} & {\bf{True}} if and only if the two expressions are {\bf{True}} & (a\textgreater{}0)~{\bf{and}}~(a\textless{}5) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \{\{nobreak\}\}\textless{}expr1\textgreater{}~{\bf{or}}~\textless{}expr2\textgreater{} & {\bf{True}} if and only if at least one of the two expressions is {\bf{True}} & (a\textless{}0)~{\bf{or}}~(a\textgreater{}=5) \tn % Row Count 10 (+ 6) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{not}} \textless{}expr\textgreater{} & {\bf{True}} if and only if \textless{}expression\textgreater{} is {\bf{False}} & {\bf{not}}~(a==0) \tn % Row Count 14 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{By {\emph{logical}} we understand an expression or operation that can only take two different values: {\bf{True}} or {\bf{False}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}