\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{MrDeniz (papapadzul)} \pdfinfo{ /Title (computer-language-unit-4-selections.pdf) /Creator (Cheatography) /Author (MrDeniz (papapadzul)) /Subject (Computer Language - Unit 4 - Selections 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}{7F49FC} \definecolor{LightBackground}{HTML}{F7F3FE} \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{Computer Language - Unit 4 - Selections Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{MrDeniz (papapadzul)} via \textcolor{DarkBackground}{\uline{cheatography.com/68635/cs/17898/}}} \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}MrDeniz (papapadzul) \\ \uline{cheatography.com/papapadzul} \\ \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 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{multicols*}{2} \begin{tabularx}{8.4cm}{x{1.944 cm} x{3.024 cm} x{1.44 cm} p{0.792 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Comparison Operators}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{radius = 4} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Python Operator & Name & Example & \seqsplit{Result} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \textless{} & less than & radius \textless{} 0 & FALSE \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \textless{}= & less than or equal to & radius \textless{}= 0 & FALSE \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \textgreater{} & greater than & radius \textgreater{} 0 & TRUE \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \textgreater{}= & greater than or equal to & radius \textgreater{}= 0 & TRUE \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} == & equal to & radius == 0 & FALSE \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} != & not equal to & radius != 0 & TRUE \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Generating Random Numbers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{randint(a, b) \# random int between a and b \newline \newline 1 import random \newline 2 \newline 3 \# Generate random numbers \newline 4 \newline 5 number2 = random.randint(0, 9) \newline 6 \newline 7 \# Prompt the user to enter an answer \newline 8 answer = eval(input("What is "+ + " + " \newline 9 + str(number2) + "? ")) \newline 10 \newline 11 \# Display result \newline 12 print(number1, "+", number2, "=", answer, \newline 13 "is", number1 + number2 == answer)} \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}{If statements}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{One-way If statements} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A one-way if statement executes the statements if the condition is true.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Two-Way if-else Statements} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A two-way if-else statement.A two-way if-else statement decides which statements to execute based on whether the condition is true or false.} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Nested if and Multi-Way if-elif-else Statements} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}One if statement can be placed inside another if statement to form a nested if statement.} \tn % Row Count 10 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.4 cm} x{5.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Logical Operators}} \tn % Row 0 \SetRowColor{LightBackground} Operator & Description \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} not & logical negation \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} and & logical conjunction \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} or & logical disjunction \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Operator Precedence Chart}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{+, - (Unary plus and minus) \newline % Row Count 1 (+ 1) ** (Exponentiation) \newline % Row Count 2 (+ 1) not \newline % Row Count 3 (+ 1) *, /, //, \% (Multiplication, division, integer division, and remainder) \newline % Row Count 5 (+ 2) +, - (Binary addition and subtraction) \newline % Row Count 6 (+ 1) \textless{}, \textless{}=, \textgreater{}, \textgreater{}= (Comparison) \newline % Row Count 7 (+ 1) ==, != (Equality) \newline % Row Count 8 (+ 1) and \newline % Row Count 9 (+ 1) or \newline % Row Count 10 (+ 1) =, +=, -=, *=, /=, //=, \%= (Assignment operators)% Row Count 11 (+ 1) } \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}{one-way {\bf{if}}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1 number = eval(input("Enter an integer: ")) \newline 2 \newline 3 if : \newline 4 print("HiFive") \newline 5 \newline 6 if : \newline 7 print("HiEven")} \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}{two-way if-else}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1 import random \newline 2 \newline 3 \# 1. Generate two random single-digit integers \newline 4 number1 = random.randint(0, 9) \newline 5 number2 = random.randint(0, 9) \newline 6 \newline 7 \# 2. If number1 \textless{} number2, swap number1 with number2 \newline 8 if number1 \textless{} number2: \newline 9 number1, number2 = number2, number1 \# Simultaneous assignment \newline 10 \newline 11 \# 3. Prompt the student to answer "What is number1 - number2?" \newline 12 answer = eval(input("What is "+ str(number1) + " - " + \newline 13 str(number2) + "? ")) \newline 14 \newline 15 \# 4. Check the answer and display the result \newline 16 if number1 - number2 == answer: \newline 17 print("You are correct!") \newline 18 else: \newline 19 print("Your answer is wrong.\textbackslash{}n", number1, '-', \newline 20 number2, "is", number1 - number2, '.')} \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}{Nested if and Multi-Way if-elif-else}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1 year = eval(input("Enter a year: ")) \newline 2 zodiacYear = year \% 12 \newline 3 if zodiacYear == 0: \newline 4 print("monkey") \newline 5 elif zodiacYear == 1: \newline 6 print("rooster") \newline 7 elif zodiacYear == 2: \newline 8 print("lion") \newline 9 elif zodiacYear == 3: \newline 10 print("goat") \newline 11 elif zodiacYear == 4: \newline 12 print("rat") \newline 13 elif zodiacYear == 5: \newline 14 print("ox") \newline 15 elif zodiacYear == 6: \newline 16 print("tiger") \newline 17 elif zodiacYear == 7: \newline 18 print("rabbit") \newline 19 elif zodiacYear == 8: \newline 20 print("dragon") \newline 21 elif zodiacYear == 9: \newline 22 print("snake") \newline 23 elif zodiacYear == 10: \newline 24 print("horse") \newline 25 else: \newline 26 print("sheep")} \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}{ComputeBMI.py}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1 \# Prompt the user to enter weight in pounds \newline 2 weight = eval(input("Enter weight in pounds: ")) \newline 3 \newline 4 \# Prompt the user to enter height in inches \newline 5 height = eval(input("Enter height in inches: ")) \newline 6 \newline 7 KILOGRAMS\_PER\_POUND = 0.45359237 \# Constant \newline 8 METERS\_PER\_INCH = 0.0254 \# Constant \newline 9 \newline 10 \# Compute BMI \newline 11 weightInKilograms = weight {\emph{ KILOGRAMS\_PER\_POUND \newline 12 heightInMeters = height }} METERS\_PER\_INCH \newline 13 bmi = weightInKilograms / (heightInMeters * heightInMeters) \newline 14 \newline 15 \# Display result \newline 16 print("BMI is", format(bmi, ".2f")) \newline 17 if bmi \textless{} 18.5: \newline 18 print("Underweight") \newline 19 elif bmi \textless{} 25: \newline 20 print("Normal") \newline 21 elif bmi \textless{} 30: \newline 22 print("Overweight") \newline 23 else: \newline 24 print("Obese")} \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}{LeapYear.py}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1 year = eval(input("Enter a year: ")) \newline 2 \newline 3 \# Check if the year is a leap year \newline 4 isLeapYear = \newline 5 \newline 6 \newline 7 \# Display the result \newline 8 print(year, "is a leap year?", isLeapYear)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}