\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{pizzajza} \pdfinfo{ /Title (python-cheat-sheet.pdf) /Creator (Cheatography) /Author (pizzajza) /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}{E8909F} \definecolor{LightBackground}{HTML}{FCF1F3} \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}{pizzajza} via \textcolor{DarkBackground}{\uline{cheatography.com/25795/cs/6925/}}} \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}pizzajza \\ \uline{cheatography.com/pizzajza} \\ \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 13th 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}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} Syntax & Grammar/Structure of language \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Variable & Hold a value and can be change \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Boolean & True/False \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} String & A list of character such as number, letter and symbols \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} Integer number & whole number/counting number \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} Float number & The number in decimal \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} List & use {[} {]} square brackets \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} Comment & hashtags/triple quotes \tn % Row Count 12 (+ 1) % Row 8 \SetRowColor{LightBackground} Character & Letters, numbers, spaces \tn % Row Count 13 (+ 1) % Row 9 \SetRowColor{white} \seqsplit{Conditional} & Like if/else \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} Modulo & Find the remainder(ex. 33\%10==3) \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{if/elif/else} & conditional \tn % Row Count 19 (+ 2) % Row 12 \SetRowColor{LightBackground} Loop & Something that repeats \tn % Row Count 20 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 20 (+ 0) % Row 14 \SetRowColor{LightBackground} Parameter & Something you give to the function \tn % Row Count 22 (+ 2) % Row 15 \SetRowColor{white} Argument & Something you give to the function \tn % Row Count 24 (+ 2) % Row 16 \SetRowColor{LightBackground} Function call & When we call the function by its name, so that it shows the code \tn % Row Count 27 (+ 3) % Row 17 \SetRowColor{white} Constants & Data stored in memory that cannot be changed after declaration \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary (cont)}} \tn % Row 18 \SetRowColor{LightBackground} Syntax error & Computer doesn't understand the instruction because it's typed incorrectly \tn % Row Count 3 (+ 3) % Row 19 \SetRowColor{white} Compile & Run the program \tn % Row Count 4 (+ 1) % Row 20 \SetRowColor{LightBackground} \seqsplit{Statements} & Instructions that do not evaluate to any value \tn % Row Count 6 (+ 2) % Row 21 \SetRowColor{white} Length & The length of the string \tn % Row Count 7 (+ 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}{Exam Review 1}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{1. Converting between different data types: \newline % Row Count 1 (+ 1) word = str(3) \#converts 3 to a string "3" \newline % Row Count 2 (+ 1) num = int("3.5") \#converts "3.5" to integer 3 \newline % Row Count 3 (+ 1) num = float("3") \#converts "3" to a float 3.0 \newline % Row Count 4 (+ 1) 2. Printing values: \newline % Row Count 5 (+ 1) print("hello","there") \#displays hello there \newline % Row Count 6 (+ 1) print("hello" + "there") \#displays hellothere \newline % Row Count 7 (+ 1) 3. Combining strings (concatenation) \newline % Row Count 8 (+ 1) "hi" + "there" == "hithere" \newline % Row Count 9 (+ 1) "hi" * 5 =="hihihihihi" \newline % Row Count 10 (+ 1) 4.Comments \newline % Row Count 11 (+ 1) \# hashtag - everything after \# is a comment no code \newline % Row Count 13 (+ 2) """ \newline % Row Count 14 (+ 1) Double quote/quotation marks - multi-line comment, everything in between three double quotes is a comment \newline % Row Count 17 (+ 3) """ \newline % Row Count 18 (+ 1) 5. Comparing Values: \newline % Row Count 19 (+ 1) when you compare two values, the result is a boolean (true or false) ex. 2==3 is false \newline % Row Count 21 (+ 2) == is equal to \newline % Row Count 22 (+ 1) != is not equal to \newline % Row Count 23 (+ 1) \textless{} less than \newline % Row Count 24 (+ 1) \textless{}= less than or equal to \newline % Row Count 25 (+ 1) \textgreater{} greater than \newline % Row Count 26 (+ 1) \textgreater{}= greater than or equal to \newline % Row Count 27 (+ 1) and \newline % Row Count 28 (+ 1) or \newline % Row Count 29 (+ 1) not \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Exam Review 1 (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{true or anything is always true \newline % Row Count 1 (+ 1) false and anything is always false \newline % Row Count 2 (+ 1) True and False must be in capital letter% Row Count 3 (+ 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}{Exam Review 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1. Forever while loop: \newline while True: \#forever \newline user\_input = input('Enter a number:') \newline number = int(user\_input) \newline print('The number squared is', number**2) \newline \newline 2. Conditional while loop: \newline count = 0 \# start at zero \newline while count \textless{}10:\#loop while count is less than 10 \newline print (count) \#will print numbers 0-9 \newline count = count + 1 \#must increase count \newline \newline 3. Decision making/ conditional statements: \newline if 3\textless{}2: \#if statement must compare two booleans \newline print('3 is less than 2') \newline elif 4\textless{}2: \#can have 0 or more \newline print('4 is less than 2') \newline elif 5\textless{}2: \newline print(' 5 is less than 2') \newline else: \#can have 0 or 1 else at the end \newline print('none of the above are True') \newline \newline 4. Lists \newline mylist = {[}2,3,4,5{]} \#create a list \newline \#select an item from a list \newline print(mylist{[}0{]}) \#selects first item and displays 2 \newline \newline \#len() determines the length of the list \newline print(len(mylist)) \#displays 4 \newline \newline mylist.append(5) \#add an item to the end of the list \newline \#it will print {[}2,3,4,5,5{]} \newline \newline 5. while loop with list: \newline thelist = {[}4,3,2,1,0{]} \newline index = \#start at the first item \newline while index \textless{} len(thelist): \newline print(thelist{[}index{]}) \#prints each item \newline index = index + 1 \newline \newline 6. for loop with list \newline forlist = {[}3,4,5,2,1{]} \newline for item in forlist: \newline print(item) \newline \newline 7.Range() \newline \#creates a list of numbers from 0 to the specified number{]} \newline numberlist = range(5) \newline \#is the same as creating the following list \newline numberlist2 = {[}0,1,2,3,4{]} \newline \newline for num in range(100): \newline print(num) \#prints all numbers from 0-99 \newline for num in range(5,50): \newline print(num) \#prints all numbers from 5-49 \newline \newline 8. Functions \newline \#function with no parameters/arguments \newline \#and no return value \newline \#return is optional if you do not return a value \newline def nameOfFunction(): \newline print('This function has no parameters') \newline print('This function has no return value') \newline return \#no value, just exits the function \newline \#function call \newline nameOfFunction() \newline \newline \#function with 1 parameter/argument \newline def testFunction(param): \newline print('This function has 1 parameter') \newline print(param) \newline \#function call \newline testfunction('this is the parameter value') \newline \newline \#function with 2 parameters and a return value \newline def function3(param1, param2): \newline print('This function has 2 parameters') \newline return param1 + param2 \#return value \newline \#function call and store the result in a variable \newline returnValue = function3(2,3) \newline print(returnValue)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.9954 cm} x{3.9816 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Conditionals}} \tn % Row 0 \SetRowColor{LightBackground} if else & If the statement is true then do command under then else do command under else. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} elif & Similar to if else, but elif allows for more conditions (The keyword 'elif' is short for 'else if') \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} for loop & For loop will loop though every element of the set of elements \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} while loop & Loop Contains 3 basic parts: 1. initial value 2. ending condition 3. update \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} while true & Loops forever \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} Boolean & True or anything = always True \tn % Row Count 15 (+ 1) % Row 6 \SetRowColor{LightBackground} Boolean & False and anything = always False \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} Boolean & True/False always write in capital letter \tn % Row Count 19 (+ 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}{Sample Exam 1}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Practice exam \newline \newline \# 1. Make the loop not to go forever \newline gameover = 0 \newline while (gameover == 0): \newline print("hello") \newline gameover = 1 \# or break \newline \newline \# 2. Make each number of the list mylist printed out on a seperate line \newline mylist = {[}1,2,3,4,5{]} \newline for number in mylist: \newline print(number) \newline \newline \# 3. Make the program prints out the fifth character from the variable myword \newline myword = "hellothere" \newline print(myword{[}4{]}) \newline \newline \# 4. Write a program that receives input from the user in a loop. Convert the input to an integer and print out that integer multiplied by 10. \newline while True: \newline user\_input = input("Please enter a number:") \newline user\_input1 = int(user\_input) \newline user\_input2 = user\_input1*10 \newline print(user\_input2) \newline \newline \# 5. Write a program that uses a while loop to print out each item in the following list: \newline wlist = {[}2,4,5,6,7,8{]} \newline listnum = 0 \newline while listnum \textless{} len(wlist): \newline print(wlist{[}listnum{]}) \newline listnum = listnum + 1 \newline \newline \# 6. Write a program that uses a for loop to print out each item in the following list: \newline forlist = {[}'hi', 'hello', 'goodbye'{]} \newline for item in forlist: \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}{Sample Exam 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Sample Exam 2 \newline \newline \# 1. Receive input from the user as a float , and print out half of that number. \newline user\_input = input("Please enter a number:") \newline print(float(user\_input/2)) \newline \# 2. What is the putput of the following code: \newline y = True \newline print(not y or 2 \textless{} 3) \newline print(True) \newline \# 3. Consider the following code: \newline message = "hello" \newline if (len(message)\textgreater{} 5): \newline print ("Message is too long") \newline else: \newline print("Message is good") \newline \# line 3 has an error because there is no indentation. \newline \# 4. Program to receive a number from teh user and determine if that number is divisible by 3. \newline \# 9 is divisible by 3 \newline \# 7 is not divisble by 3 \newline user\_input = input("Please enter a number;") \newline if user\_input\%3 == 0: \newline print(user\_input, "is divisible by 3") \newline else: \newline print(user\_input, "is not divisible by 3") \newline \# 5. 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 \newline \# 6. What is the output of the following code: \newline condition = True \newline number = 5 \newline \newline if condition == False: \newline number = number {\emph{* 2 \newline elif number \textless{} 5: \newline number = number }} 2 \newline elif condition == true: \newline number = number \% 2 \newline else: \newline number = number /2 \newline print(number) \newline \# 7 . Given a list called mylist, print all the elements from the list using a loop \newline \#for loop solution \newline mylist = {[} 1,2,3,4,5{]} \newline for number in mylist: \newline print(number) \newline \#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 \newline \# 8. Use a for loop a print the following \newline \#0 \newline \#01 \newline \#012 \newline \#0123 \newline \#01234 \newline mystring = "" \newline for number in range{[}5{]}: \#{[}0,1,2,3,4,5{]} \newline mystring = mystring +str(number) \newline print(mystring) \newline \# 9. Write a function called multiplicationTable that asks the user for a user for a number and computes its multiplication table. example: If the users enters 5, the output should be \newline \# Enter a number: 5 \newline \# 5{\emph{1 = 5 \newline \# 5}}2 = 10 \newline def multiplicationTable(): \newline user\_input = input("Enter a number:") \newline num = int(user\_input) \newline count = 1 \newline while count \textless{}= 10: \newline print(num, "{\emph{", count, "=", num}}count) \newline count = count+1 \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}{Sample Exam 3}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Sample Exam 3 \newline \#1. Write a program that receives input from the user, converts it to an integer, and prints the product of the integer and 5 \newline user\_input = input("Enter a number:") \newline user\_input = int(user\_input) \newline print(user\_input{\emph{5) \newline \#2. What is the output of the following code: \newline x = False \newline print(x and True or 1==1) \newline \#output is True \newline \#3. no indentation \newline \#4. Write a program that receives a number from the user and determines if that number is zero or positive \newline \#output of the program \newline \#4 is positive, 0 is zero, -8 is negative \newline user\_input = int(input("enter a number:")) \newline if user\_input\textgreater{}0: \newline print(unser\_input, "is positive") \newline elif user\_input\textless{}0: \newline print (user\_input, "is negative") \newline else: \newline print(user\_input, "is zero") \newline \#5. Write a program that prints all the even numbers from -100 to -1 using while loop \newline mynum = -100 \newline while mynum \textless{}-1: \newline print(mynum) \newline mynum = mynum +2 \newline \#7. Given a list called mylist, write a program that prints all the items in the list using a loop \newline mylist = {[}'cokezero', 'bacon', 'pepsi'{]} \newline for item in mylist: \newline print(item) \newline \#8. Complete the program below by filling in the blank: \newline \#Expected output of the program: \newline \#0 \newline \#01 \newline \#012 \newline \#0123 \newline \#01234 \newline mystring ="" \newline count = 0 \newline while count \textless{} 5: \newline mystring = mystring + str(count) \newline print(mystring) \newline count = count + 1 \newline \#9. Write a unction called areaOfEllipse() that computes the area of an ellipse using the equation: pi}}r{\emph{r \newline \#The function should be given two parameterss( radius 1 and radius2) and should return the area \newline def areaOfEllipse(radius1, radius2): \newline pi = 3.1415 \newline area = pi}}radius1*radius2 \newline return area \newline \#function call \newline area1 = areaOfEllipse(2,3) \newline print(area1) \newline \#11.Wrie a program that repeatly receives positive integers from the user. When the user enters a negative integer, exit the loop and orint how many of the numbers entered were even and odd. \newline evenCount = 0 \newline oddCount = 0 \newline while True: \newline num = int(input("Enter a positive integer:")) \newline if num \textless{} 0 : \newline print("Even numbers:", evenCount) \newline print("Odd numbers:", oddCount) \newline break \newline else: \newline if(num\%2) == 0 \newline evenCount = evenCount + 1 \newline else: \newline oddCount = oddCount + 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}{Basic Math Operation}} \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} + & Add \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} - & Subtract \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} * & Multiply \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} ** & Exponent \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} / & Divide and quotient is float \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} // & Divide and quotient is integer \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} \% & Modulo, Find the remainder(33\%10==3) \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Addition}} \tn % Row 0 \SetRowColor{LightBackground} string + string & Combine togehter \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string + number & CRASH! \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number + number & Addition (Math) \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Multiplication and Exponents}} \tn % Row 0 \SetRowColor{LightBackground} string * number & Combine that string \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string * string & CRASH! \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number * number & Multiply (Math) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} string ** string & CRASH! \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} number ** number & Exponent (Math) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} string ** number & CRASH! \tn % Row Count 6 (+ 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}{Function}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{print()} & Show information that you want on the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} int() & Change number to be number integer \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{float()} & Change number to be decimal number \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{input()} & Gain information from user \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} str() & A list of number, letter and symbols \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} len() & The length of the string \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \# & Comment, no effect \tn % Row Count 11 (+ 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) - floating point \newline % Row Count 2 (+ 1) Print ("Hello) - string \newline % Row Count 3 (+ 1) Print (mystr) - variable \newline % Row Count 4 (+ 1) Print (mystr,"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% Row Count 10 (+ 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}{Naming Convention}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Rule for giving name \newline - letter \newline - number (number cannot be the first letter) \newline - underscore\_ \newline - can start with letters or underscores ONLY \newline - no spaces \newline \newline Valid name \newline - myStr \newline - my3 \newline - Hello\_there \newline \newline Invalid name \newline - 3my = "hi" (cannot start with number) \newline - first name = "hi" (no spaces allowed) \newline - first-name (dashes are not accepted)} \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}{Area of circle Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Ask the user for a radius of a circle \newline \newline while True: \newline user\_radius = input("Please enter the radius of the circle:") \newline radius = float(user\_radius) \newline pi = 3.1415 \newline area = (pi{\emph{radius}}*2) \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}{Area of triangle \& Volume of prism Python}} \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 def areaOfTriangle (base, height): \newline return 1/2{\emph{ user\_base}} user\_height \newline \newline \#function call \newline print('The area of the triangle is',areaOfTriangle (user\_base, user\_height)) \newline \newline \newline \#write function compute volume of prism \newline \newline \#name : volumeOfPrism \newline \# parameters : b,h,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 \newline return volume \newline \newline user\_prism\_height = float(input('Enter the prism height:')) \newline print ('The volume 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}{Convert to binary Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = ' ' \newline \newline while user\_number != '0' : \newline user\_number = input ("Enter a number to convert to binary") \newline number = int(user\_number) \newline binary\_string = ' ' \newline \newline while (number \textgreater{} 0): \newline remainder = number\%2 \newline binary\_string = str(remainder) + binary\_string \newline number = number // 2 \newline \newline print ("Binary string is", binary\_ 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}{Countdown String Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("What number do you want to count down?") \newline number = int(user\_number) \newline countdown\_string = ' ' \newline \newline while number \textgreater{} 0: \newline countdown\_number = countdown\_string + str(number) + " " \newline number = number - 1 \newline \#print (number) \newline \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}{Define Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def calc(num1, num2, operation): \newline \#use if/elif/else to check what operation to do \newline \#call the correct function and return the answer \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 \newline \newline def sum(a,b): \newline \#calculate the sum of a and b \newline \#return the answer \newline return a+b \newline \newline def product(a,b): \newline \#calculate the product of a and b \newline \#return the answer \newline return a*b \newline \newline def diff(a,b): \newline \#calculate the difference between a and b \newline \#return the answer \newline return a-b \newline \newline def div(a,b): \newline if b != 0: \newline return a//b \newline else: \newline print("Error") \newline \#calculate the division of a and b \newline \#return the answer \newline \newline \newline print(calc(10,0,"div")) \#division by zero \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}{Define Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\_var1 = 1 \newline \_var1 = 3 \newline \_var1 + 100 \newline print(\_var1) \newline \newline def bacon(): \newline print("hello") \newline return \newline bacon() \newline \newline \#anything after return will not be printed \newline def bacon(): \newline print("hello") \newline print("line1") \newline return \newline print("bye") \newline bacon() \newline \newline def myprint(text):\# \newline print("{\bf{{\emph{" + str(text) + "}}}}") \newline return \#return exits the function \newline myprint(1) \newline myprint(2.5) \newline myprint("hello") \newline \newline def myprint2 (text, decoration): \#text and decoration are arguments(parameter) to the function (something you're giving to the function) \newline print(decoration + text + decoration) \newline return \newline myprint2("hello", "+++") \newline myprint2("hello", "-=-=-=-=-=-") \newline myprint2("hello", "\textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}\textgreater{}") \newline \newline def doubleIt(number): \newline return number{\emph{2 \#return value \newline print (doubleIt(2)) \newline myvar = doubleIt(doubleIt(3)) \#same as doubleIt(6) because doubleIt(3) ==6) \newline print (myvar) \#it will display 12 \newline \newline def areaOfCircle (r): \newline if r \textless{}= 0: \newline return "Error: invalid radius" \newline pi = 3.1415 \newline area = pi}}r**2 \newline return area \newline user\_radius = input("Enter the radius:") \newline radius = float(user\_radius) \newline print("The area of the circle is", areaOfCircle(radius))} \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}{Max value + For loops Python}} \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 \newline def max2(num1, num2): \newline if num1 \textgreater{} num2: \newline maxvalue = num1 \newline elif num2 \textgreater{} num1: \newline maxvalue = num2 \newline return maxvalue \newline \newline print(max2(2656,3)) \newline \newline def max2(num1, num2): \newline maxvalue = num1 \newline if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline return maxvalue \newline print(max2(2,3)) \newline \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 if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline \newline if num3 \textgreater{} maxvalue: \newline maxvalue = num3 \newline return maxvalue \newline print(max3(2,3,4)) \newline \newline \# write a function that returns the largest number in a list \newline \# name: maxlist \newline \# arguments: list \newline \# returns the largest value in the list \newline \newline def maxlist(list): \newline maxvalue = list{[}0{]} \newline for item in list: \newline if item \textgreater{} maxvalue: \newline maxvalue = item \newline return maxvalue \newline \newline mylist = {[}23,4342,468,56,7873{]} \newline print(maxlist(mylist))} \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}{Name Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{firstname = input("What is your first name?") \newline lastname = input("what is your last name?") \newline \newline fullname = firstname + " " + lastname \newline print (fullname) \newline \newline letternumber = int(input("What is the letter number?")) \newline \newline if letternumber \textgreater{}= len(fullname): \newline print("Invalid letter number,try again.") \newline else: \newline print(fullname{[}letternumber{]}) \newline \#letter number is a string \newline times = int(input("How many times to print the letter")) \newline if times \textgreater{} 100: \newline print ("Too many letters to print!") \newline else: \newline print (fullname{[}letternumber{]}*times) \newline \#square brackets select a letter inside a 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}{Return function Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#how to create a function \newline def nameOfFunction (myvar1, myvar2): \newline print("hello") \newline return myvar1 + myvar2 \newline \newline \#function call \newline nameOfFunction (2,3) \newline myanswer = nameOfFunction (4,1) \newline print(myanswer) \newline print(nameOfFunction(nameOfFunction(2,1),4)) \newline \# it will print(nameOfFunction(3,4))} \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 Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline word = input('Please enter a word') \newline index = 0 \newline reverse = '' \newline \newline while int(index) \textless{} len(word): \newline reverse = word{[}index{]} + (reverse) \newline index = int(index) + 1 \newline \newline print ("Reverse: ", reverse)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}