\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{Milly} \pdfinfo{ /Title (python-cheat-sheet.pdf) /Creator (Cheatography) /Author (Milly) /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}{E39197} \definecolor{LightBackground}{HTML}{FBF1F2} \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}{Milly} via \textcolor{DarkBackground}{\uline{cheatography.com/25796/cs/6927/}}} \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}Milly \\ \uline{cheatography.com/milly} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 12th February, 2016.\\ Updated 12th May, 2016.\\ 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}{p{0.79632 cm} x{4.18068 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Commands/ Functions}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{print()} & displays information of the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{input()} & receives info from the the user \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{float()} & converts a value to decimal \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} int() & integer; coverts a value to an integer \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} str() & string; coverts a value to a string \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \# & Comment; no effect \tn % Row Count 11 (+ 1) \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}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} variable & something that can change \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string & a list of characters \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Integer number & Whole number/ counting number \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} Float number & The number in decimal \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} Syntax & Grammar/ Structure of language \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} Modulo & Find the remainder \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} Boolean & True/False \tn % Row Count 9 (+ 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}{Example}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Print (2) – integer \newline % Row Count 1 (+ 1) Print (2.5) – float \newline % Row Count 2 (+ 1) Print ("Hello") – string \newline % Row Count 3 (+ 1) Print (mystr) – variable \newline % Row Count 4 (+ 1) Print (str,"Hi",2,1.0) – commas \newline % Row Count 5 (+ 1) mystr = "Hi" \newline % Row Count 6 (+ 1) mystr – name \newline % Row Count 7 (+ 1) "Hi" – value can change \newline % Row Count 8 (+ 1) print (int(1.5)) – 1 \newline % Row Count 9 (+ 1) print (int("2")) – 2 \newline % Row Count 10 (+ 1) print(float(1)) - 1.0 anything to a float% Row Count 11 (+ 1) } \tn \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}{Math}} \tn % Row 0 \SetRowColor{LightBackground} == & equal to \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} != & no equal to \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textless{} & less than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textgreater{} & more than \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textless{}= & less than or equal to \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textgreater{}= & more than or equal to \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \% & Modulo, Find the remainder \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Addition}} \tn % Row 0 \SetRowColor{LightBackground} string + string & combines the strings together \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string + number & crash \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} number + number & math (addition) \tn % Row Count 6 (+ 2) \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}{Multiplication and Exponents}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{string*string} & CRASH! \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{string*number} & combines the string multiple times \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{number*number} & Multiply (Math) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} string ** number & CRASH! \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} string ** string & CRASH! \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} number ** number & Exponent (Math) \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Naming Convention}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Rule for giving name \newline % Row Count 1 (+ 1) - letter \newline % Row Count 2 (+ 1) - numbers \newline % Row Count 3 (+ 1) - underscore\_ \newline % Row Count 4 (+ 1) Valid name \newline % Row Count 5 (+ 1) - \_myStr \newline % Row Count 6 (+ 1) - my3 \newline % Row Count 7 (+ 1) - Hello\_name \newline % Row Count 8 (+ 1) Invalid name \newline % Row Count 9 (+ 1) - 3my="hi" -{}- cannot start with number \newline % Row Count 10 (+ 1) - first name="hi" \newline % Row Count 11 (+ 1) - first-name% Row Count 12 (+ 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}{Reverse word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("please enter a word.") \newline \newline """ \newline letter\_num = 0 \newline reverse = ' ' \newline while letter\_num \textless{} len(word) : \newline reverse = word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline """ \newline reverse = ' ' \newline for letter in word: \newline reverse = letter + reverse \newline print ("Reverse: ",reverse)} \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}{Countdown Machine}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline user\_number = input("Please enter a number") \newline number = int(user\_number) \newline countdown\_string = "" \newline while number \textgreater{} 0: \newline countdown\_string = countdown\_string + str(number) \newline number = number - 1 \newline print (countdown\_string)} \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}{List}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{shoppinglist = {[}'coke zero', 'bacon', 'water', 'jelly', 'gummy bears'{]} \newline \newline print (shoppinglist) \newline \newline print (shoppinglist{[}0{]}) \newline \newline list\_num =0 \newline while list\_num \textless{} len(shoppinglist): \newline print ("List:", shoppinglist{[}list\_num{]}) \newline list\_num = list\_num + 1 \newline \newline for item in shoppinglist: \newline print (item) \newline \newline numbers = range (1,6) \newline for num in numbers: \newline print (num) \newline \newline \# a string is a list of characters, letters, numbers etc. \newline \newline mystr = "hello" \newline for letter in mystr: \newline print (letter) \newline \newline shoppinglist = {[}'coke zero', 'bacon', 'water', 'jelly', 'gummy bears'{]} \newline num = 0 \newline for w in shoppinglist: \newline num = num + 1 \newline print (num) \newline \newline shoppinglist = {[}'coke zero', 'bacon', 'water', 'jelly', 'gummy bears'{]} \newline num = 0 \newline for w in shoppinglist: \newline num = num + 1 \newline print (num)} \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}{Finding area of a circle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline user\_radius = input("Please enter the radius of the circle:") \newline radius = float(user\_radius) \newline pi = 3.1415 \newline \newline area = (pi{\emph{radius}}*2) \newline \newline print ("The area of the circle is", area)} \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}{Calculator program}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def calc (num1, num2, operation): \newline if operation == "sum": \newline return sum (num1,num2) \newline elif operation == "product": \newline return product (num1,num2) \newline elif operation == "diff": \newline return diff (num1,num2) \newline elif operation == "div": \newline return div (num1,num2) \newline \# use if/elif/else to check what operation to do \newline \# call the correct function and return the answer \newline def sum (a, b): \newline return a+b \newline \# calculate the sum of a and b \newline \# return the answer \newline def product (a, b): \newline return a*b \newline \# calcualate the product of a and b \newline \# return the answer \newline def diff (a, b): \newline return a-b \newline \# calculate the difference between a and b \newline \# return the answer \newline def div (a, b): \newline if b!=0: \newline return a/b \newline else: \newline return ("Error") \newline \# calculate the division of a and b \newline \# return the answer \newline \newline print (calc (10, 0, "div")) \newline print (calc (1, 2,"sum")) \# output should be 3 \newline print (calc (4, 2, "diff")) \# output should be 2 \newline print (calc (9, 3, "div" )) \# output should be 3 \newline print (calc (2 ,12, "product" )) \# output should be 24} \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}{Finding the area of the triangle and its prism}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a function \newline \newline \#name: areaofTriangle \newline \#parameters: base height \newline \#return: area \newline \newline user\_base = float(input ('Enter the base of the triangle: ')) \newline user\_height = float(input('Enter the height of the triangle:')) \newline \newline def areaOfTriangle (base, height): \newline return 0.5{\emph{base}}height \#or 1/2 \newline \newline \#functioncall \newline \newline print ('The area of the triangle is', \seqsplit{areaOfTriangle(user\_base}, user\_height)) \newline \newline \#write function compute volume of prism \newline \newline \#name: volumeOfPrism \newline \#parameters: base, height, prism\_height \newline \#return volume \newline \newline def volumeOfPrism (base,height,prism\_height): \newline \# area {\emph{ prism\_height \newline volume = areaOfTriangle (base,height) }} prism\_height \newline return areaOfTriangle (base,height) * prism\_height \newline \newline user\_prism\_height = float(input('Enter the height of the prism:')) \newline print ('The area of the prism is', volumeOfPrism (user\_base,user\_height,user\_prism\_height))} \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}{Maximum}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# write a function that returns the largest of two values \newline \# name: max2 \newline \# arguments: num1, num2 \newline \# return: the largest value \newline def max2 (num1,num2): \newline maxvalue = num1 \newline \newline if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline return maxvalue \newline \newline print (max2(3,4)) \newline \newline \# write a function that returns the largest of three values \newline \# name: max3 \newline \# arguments: num1, num2, num3 \newline \# return: the largest value \newline \newline def max3 (num1,num2,num3): \newline maxvalue = num1 \newline \newline if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline \newline if num3 \textgreater{} maxvalue: \newline maxvalue = num3 \newline \newline return maxvalue \newline \newline print (max3(3,4,8)) \newline \newline \# write a function that returns the largest number in a list \newline \# name: maxlist \newline \# argument: list \newline \# returns the largest value in the list \newline \newline \newline def maxlist (list): \newline maxvalue = list{[}0{]} \newline for item in list: \newline if item \textgreater{} maxvalue: \newline maxvalue = item \newline \newline return maxvalue \newline \newline list = {[}1,2,3,6,19,50,2,4,5{]} \newline print (maxlist(list))} \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}{1. Multiplying number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Receive input from the user as a float, and print out half of that number. e.g. user enters 12.5, print out 6.25 \newline \newline user\_input = input("Please enter a number:") \newline number = float(user\_input) \newline finalnumber = 0.5*number \newline print(finalnumber)} \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}{2. Output}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#What is the output of the following code: \newline y = True \newline print (not y or 2\textless{}3) \newline \#output is True} \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}{3. Error}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{message = "hello" \newline if (len(message) \textgreater{}5) \newline print ("Message too long") \newline else: \newline print ("Message is good") \newline \newline line 3 has an error because it has no indent} \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}{4. Divisible by 3}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#create a program to receive a number from the user and determine if that number \newline \#is divisible by 3 \newline user\_input = input("Please enter a number:") \newline number = int(user\_input) \newline if number \%3 == 0: \newline print(number,"is divisible by 3") \newline else: \newline print (number, "is not divisible by 3")} \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}{5. Even Number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# print all the even numbers from 1 to 100 using a while loop \newline num = 2 \newline while num \textless{}= 100: \newline print (num) \newline num = num+2} \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}{6. Output 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#What is the output of the following code? \newline \newline condition = True \newline number = 5 \newline \newline if condition == False: \#False \newline number = number {\emph{* 2 \newline elif number \textless{}5: \#False \newline number = number }} 2 \newline elif condition == True: \#True \newline number = number\%2 \#5\%2=1 \newline else: \newline number = number/2 \newline print (number) \# output = 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}{7. my list (method 1)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Given a list called mylist, print all elements from the list using a loop \newline mylist = {[}"Milly","Prim","Pizza"{]} \newline for item in mylist: \newline print (item)} \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}{7. List: while loop (method 2)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#while loop solution \newline mylist = {[}1,2,3,4,5{]} \newline num = 0 \newline while num \textless{} len(mylist): \newline print (mylist{[}num{]}) \newline num = num+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}{9. Multiplication Table}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Write a function called multiplicationTable that asks the user for a number and \newline \#computes its multiplication table. \newline \newline def multiplicationTable (): \newline user\_input = input ("Enter a number:") \newline num = int(user\_input) \newline count=1 \newline \newline while count \textless{}= 10: \newline print (num,"{\emph{",count,"=",num}}count) \newline count = count+1 \newline \newline \#function call \newline multiplicationTable()} \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}{1) Multiply 5}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Write a program that receives input from the user, converts it to an integer, \newline \#and print the product of the integer and 5 \newline \newline user\_input = input("Please enter a number.") \newline user\_input = int(user\_input) \newline product = user\_input * 5 \newline print (product)} \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}{2) Output}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#What is the output of the following code? \newline x = False \newline print (x and True or 1 == 1) \newline \#False and True = False \newline \# False or True = True \newline \newline \# output is True} \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}{3) Error 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#condisder the following code \newline \newline def doubleValue(value): \newline return value*2 \newline print (doubleValue(4)) \newline \newline \#line 2 has error because it is not indented} \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}{4) Types of number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a program that receives a number from the user and determines if that \newline \#number is negative,zero or positive. \newline \newline user\_input = int(input("Please enter a number:")) \newline if user\_input \textgreater{} 0: \newline print (user\_input,"is positive") \newline elif user\_input == 0: \newline print (user\_input,"is zero") \newline else: \newline print (user\_input,"is negative")} \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}{5) Even numbers while loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# write a program that prints all the even numbers from -100 to -1 using a \newline \#while loop \newline \newline num = -100 \newline while num \textless{}= -2: \newline print (num) \newline num = num + 2} \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}{6) Output 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#What is the output of the following code: \newline \newline condition = 2\textless{}3 \#True \newline number = 3 \newline \newline if condition != True: \#True != True ;False \newline number = number {\emph{* 2 \newline elif number \textless{}= 0: \#3 \textless{}= 0 ;False \newline number = number }} 3 \newline elif number \textgreater{} 3: \# 3\textgreater{}3 ; False \newline number = number\%10 \newline else: \#everything is False do must do else \newline number = number - 1 * 2 \# number - 2; 3-2=1; 1 \newline \newline print (number) \# 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}{8) 0......}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#complete the program below by filling in the blank \newline \# Expected output of program \newline \# 0 \newline \# 01 \newline \# 012 \newline \# 0123 \newline \# 01234 \newline \newline mystring = "" \newline count = 0 \newline while count \textless{}= 4: \newline mystring = mystring +str(count) \newline print (mystring) \newline count = count + 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}{9) Area of Ellipse}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Write a function called areaOfEllipse () that computes the area of an ellipse \newline \#using the equation pi{\emph{r1}}r2 \newline \# The function should be given 2 parameters (radius1 and radius 2) and should \newline \#return the area \newline \newline def areaOfEllipse (radius1,radius2): \newline pi = 3.1415 \newline area = pi{\emph{radius1}}radius2 \newline return area \newline \#function call \newline area1 = areaOfEllipse(2,3) \newline print(area1)} \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}{11) Even and Odd}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Write a program that repeatly receives positive integers from the user. When \newline \# the user enters a negative integer, exit the loop and print how many of the \newline \# numbers entered were even and odd. \newline \newline evencount = 0 \newline oddcount = 0 \newline \newline while True: \newline user\_input = int(input("Enter a number:")) \newline if user\_input \textless{} 0: \newline print ("Evencount=", evencount) \newline print ("Oddcount=",oddcount) \newline break \newline elif user\_input \textgreater{} 0: \newline if user\_input \% 2 == 0: \newline evencount = evencount + 1 \newline else: \newline oddcount = oddcount + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}