\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{Noktik} \pdfinfo{ /Title (intro-python.pdf) /Creator (Cheatography) /Author (Noktik) /Subject (Intro 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{Intro Python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Noktik} via \textcolor{DarkBackground}{\uline{cheatography.com/57911/cs/15324/}}} \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}Noktik \\ \uline{cheatography.com/noktik} \\ \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 31st March, 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Ch. 4 Repetition Structures}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Iteration:}} one execution of the body of a loop \newline % Row Count 2 (+ 2) {\bf{Infinite loop:}} loop that does not have a way of stopping \newline % Row Count 4 (+ 2) {\bf{Iterable:}} contains a sequence of values that can be iterated over \newline % Row Count 6 (+ 2) {\bf{Sentinel:}} special value that marks the end of a sequence of items \newline % Row Count 8 (+ 2) eg; `while lot ! = 0:` from program 4-13 in the text% Row Count 10 (+ 2) } \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}{The for Loop: a Count-Controlled Loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for variable in {[}value1, value2, etc.{]}: \newline statement \newline statement \newline etc.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Iterates a specific number of times (each item in the sequence)} \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}{for loops - Range Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for num in range(5): \newline print(num) \newline \newline for num in range(0, 5): \newline print(num) \newline \newline for num in {[}0, 1, 2, 3, 4{]}: \newline print(num)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{All three of these execute a For loop 5 times, and print the numbers {[}0-4{]} \newline \newline The range function simplifies the process of writing a for loop, since it returns an iterable object \newline {\bf{One argument: }}used as ending limit \newline {\bf{ Two arguments: }}starting value and ending limit \newline {\bf{Three arguments: }}third argument is step value \newline \newline Can generate a sequence of numbers in {\emph{descending order}}, if the starting value is larger than the end limit and the step value is negative. Eg; \newline `range (10, 0 -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}{The while Loop: a Condition-Controlled Loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{while condition: \newline statements} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{"While condition is true, do something. " Also known as a "pretest" loop, since it tests for a condition before performing an iteration. Also used as {\bf{Input Validation}} loops. \newline \newline - Condition tested for true or false value \newline - Statements repeated as long as condition is true} \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}{Accumulator Variable}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{8.4cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/noktik_1522400579_accumulator.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.976 cm} x{2.812 cm} x{2.812 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Augmented Assignment Operators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operator}} & {\bf{Example Usage}} & {\bf{Equivalent To}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} += & x += 5 & x = x + 5 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} -= & y -= 2 & y = y - 2 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} *= & z *= 10 & z = z *10 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} /= & a /= b & a = a / b \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \%= & c \%= 3 & c = c \% 3 \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{Shorthand operators} \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}{Input Validation Loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# Get a test score. \newline score = int(input('Enter a test score: ')) \newline \# Make sure it is not less than 0. \newline while score \textless{} 0: \newline print('ERROR: The score cannot be negative.') \newline score = int(input('Enter the correct score: '))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Input validation is the process of inspecting data that has been input to a program, to make sure it is valid before it is used in a computation. Input validation is commonly done with a loop that iterates as long as an input variable references bad data. \newline \newline E.g.; This input validation loop (using a while loop) rejects any input that isn't zero, and asks for new input.} \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 Loop -{}- Loop contained inside another loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# This program displays a rectangular pattern \newline \# of asterisks. \newline rows = int(input('How many rows? ')) \newline cols = int(input('How many columns? ')) \newline \newline for r in range(rows): \newline for c in range(cols): \newline print('*', end='') \newline print()} \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}{Chapter 4 Questions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.1 What is a repetition structure?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A structure that causes a section of code to repeat} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.2 What is a condition-controlled loop?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A loop that uses a true/false condition to control the number of times that it repeats} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.3 What is a count-controlled loop?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A loop that repeats a specific number of times} \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.4 What is a loop iteration?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}An execution of the statements in the body of the loop} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.5 Does the while loop test its condition before or after it performs an iteration?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Before} \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.6 How many times will 'Hello World' be printed in the following program? \{\{nl\}\}`count = 10` \{\{nl\}\}`while count \textless{} 1:` \{\{nl\}\}~~~~~~~~~`print('Hello World')`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}None. The condition `count \textless{} 0` will be false to begin with.} \tn % Row Count 21 (+ 7) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.7 What is an infinite loop?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A loop that has no way of stopping and repeats until the program is interrupted.} \tn % Row Count 24 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.8 Rewrite the following code so it calls the `range` function instead of using the list `{[}0, 1, 2, 3, 4, 5{]}: ` \{\{nl\}\}`for x in {[}0, 1, 2, 3, 4, 5{]}: ` \{\{nl\}\}~~~~` print('I love to program!')`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`for x in range(6):` \{\{nl\}\}~~~~` print('I love to program!')`} \tn % Row Count 31 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 4 Questions (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.9 What will the following code display? \{\{nl\}\}`for number in range(6):` \{\{nl\}\}~~~~`print(number)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\{\{nl\}\}`0` \{\{nl\}\}`1` \{\{nl\}\}`2` \{\{nl\}\}`3` \{\{nl\}\}`4` \{\{nl\}\}`5`} \tn % Row Count 5 (+ 5) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.10 What will the following code display? \{\{nl\}\}`for number in range(2, 6):` \{\{nl\}\}~~~~` print(number)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`2` \{\{nl\}\}`3` \{\{nl\}\}`4` \{\{nl\}\}`5`} \tn % Row Count 9 (+ 4) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.11 What will the following code display? \{\{nl\}\}`for number in range(0, 501, 100):` \{\{nl\}\}~~~~` print(number)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`0 \{\{nl\}\}`100` \{\{nl\}\}`200` \{\{nl\}\}`300` \{\{nl\}\}`400` \{\{nl\}\}`500`} \tn % Row Count 14 (+ 5) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.12 What will the following code display? \{\{nl\}\}`for number in range(10, 5, −1):` \{\{nl\}\}~~~~` print(number)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\{\{nl\}\}`10` \{\{nl\}\}`9` \{\{nl\}\}`8` \{\{nl\}\}`7` \{\{nl\}\}`6`} \tn % Row Count 19 (+ 5) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.13 What is an accumulator?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A variable that is used to accumulate the total of a series of numbers} \tn % Row Count 22 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.14 Should an accumulator be initialized to any specific value? Why or why not?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Yes, it should be initialized with the value 0. This is because values are added to the accumulator by a loop. If the accumulator does not start at the value 0, it will not contain the correct total of the numbers that were added to it when the loop ends.} \tn % Row Count 30 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 4 Questions (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.15 What will the following code display? \{\{nl\}\}`total = 0` \{\{nl\}\}`for count in range(1, 6):` \{\{nl\}\}~~~~` total = total + count` \{\{nl\}\}`print(total)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`15`} \tn % Row Count 5 (+ 5) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.16 What will the following code display? \{\{nl\}\}`number 1 = 10` \{\{nl\}\}`number 2 = 5` \{\{nl\}\}`number 1 = number 1 + number 2` \{\{nl\}\}`print(number1)` \{\{nl\}\}`print(number2)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\{\{nl\}\}`15` \{\{nl\}\}`5`} \tn % Row Count 10 (+ 5) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.17 Rewrite the following statements using augmented assignment operators: \{\{nl\}\}a. `quantity = quantity + 1` \{\{nl\}\}b. `days\_left = days\_left − 5` \{\{nl\}\}c. `price = price * 10` \{\{nl\}\}d. `price = price / 2`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\{\{nl\}\}a. `quantity += 1` \{\{nl\}\}b. `days\_left −= 5` \{\{nl\}\}c. `price *= 10` \{\{nl\}\}d. `price /= 2`} \tn % Row Count 18 (+ 8) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.18 What is a sentinel?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A sentinel is a special value that marks the end of a list of items.} \tn % Row Count 21 (+ 3) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.19 Why should you take care to choose a distinctive value as a sentinel?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A sentinel value must be unique enough that it will not be mistaken as a regular value in the list.} \tn % Row Count 26 (+ 5) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.20 What does the phrase "garbage in, garbage out" mean?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It means that if bad data (garbage) is provided as input to a program, the program will produce bad data (garbage) as output.} \tn % Row Count 31 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 4 Questions (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.21 Give a general description of the input validation process.} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}When input is given to a program, it should be inspected before it is processed. If the input is invalid, then it should be discarded and the user should be prompted to enter the correct data.} \tn % Row Count 7 (+ 7) % Row 21 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.22 Describe the steps that are generally taken when an input validation loop is used to validate data.} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}The input is read, then a pretest loop is executed. If the input data is invalid, the body of the loop executes. In the body of the loop, an error message is displayed so the user will know that the input was invalid, and then the input read again. The loop repeats as long as the input is invalid.} \tn % Row Count 17 (+ 10) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{4.23 What is a priming read? What is its purpose?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It is the input operation that takes place just before an input validation loop. The purpose of the priming read is to get the first input value.} \tn % Row Count 22 (+ 5) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4.24 If the input that is read by the priming read is valid, how many times will the input validation loop iterate?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}None} \tn % Row Count 26 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Ch 5 Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Function:}} group of statements within a program that perform as specific task \newline % Row Count 2 (+ 2) {\bf{Modularized program:}} program wherein each task within the program is in its own function \newline % Row Count 4 (+ 2) {\bf{Void Function:}} Simply executes the statements it contains and then terminates. \newline % Row Count 6 (+ 2) {\bf{A value-returning function:}}Executes the statements it contains, and then it returns a value back to the statement that called it. \newline % Row Count 9 (+ 3) ~~~~The input, int, and float functions are \newline % Row Count 11 (+ 2) ~~~~examples of value-returning functions. \newline % Row Count 13 (+ 2) {\bf{Function definition:}} specifies what function does \newline % Row Count 15 (+ 2) ~~~~ def function\_name(): \newline % Row Count 16 (+ 1) ~~~~ ~~~~ ~~~~ ~~~~ ~~~~statement \newline % Row Count 19 (+ 3) ~~~~ ~~~~ ~~~~ ~~~~ ~~~~statement \newline % Row Count 22 (+ 3) {\bf{Function header:}} first line of function. Includes keyword `def` and function name, followed by parentheses and colon \newline % Row Count 25 (+ 3) {\bf{Block:}} set of statements that belong together as a group \newline % Row Count 27 (+ 2) ~~~~Example: the statements included in a ~~~~function \newline % Row Count 29 (+ 2) {\bf{main function:}} called when the program starts% Row Count 30 (+ 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}{Chapter 5 Questions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.1 What is a function?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A function is a group of statements that exist within a program for the purpose of performing a specific task.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.2 What is meant by the phrase "divide and conquer"?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A large task is divided into several smaller tasks that are easily performed.} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.3 How do functions help you reuse code in a program?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}If a specific operation is performed in several places in a program, a function can be written once to perform that operation, and then be executed any time it is needed.} \tn % Row Count 14 (+ 6) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.4 How can functions make the development of multiple programs faster?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Functions can be written for the common tasks that are needed by the different programs. Those functions can then be incorporated into each program that needs them.} \tn % Row Count 20 (+ 6) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.5 How can functions make it easier for programs to be developed by teams of programmers?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}When a program is developed as a set of functions in which each performs an individual task, then different programmers can be assigned the job of writing different functions.} \tn % Row Count 26 (+ 6) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.6 A function definition has what two parts?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A function definition has two parts: a header and a block. The header indicates the starting point of the function, and the block is a list of statements that belong to the function.} \tn % Row Count 31 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 5 Questions (cont)}} \tn % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.7 What does the phrase "calling a function" mean?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}To call a function means to execute the function.} \tn % Row Count 4 (+ 4) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.8 When a function is executing, what happens when the end of the function's block is reached?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}When the end of the function is reached, the computer returns back to the part of the program that called the function, and the program resumes execution at that point.} \tn % Row Count 10 (+ 6) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.9 Why must you indent the statements in a block?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Because the Python interpreter uses the indentation to determine where a block begins and ends} \tn % Row Count 13 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.10 What is a local variable? How is access to a local variable restricted?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A local variable is a variable that is declared inside a function. It belongs to the function in which it is declared, and only statements in the same function can access it.} \tn % Row Count 19 (+ 6) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.11 What is a variable's scope?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}The part of a program in which a variable may be accessed} \tn % Row Count 22 (+ 3) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.12 Is it permissible for a local variable in one function to have the same name as a local variable in a different function?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Yes, it is permissible.} \tn % Row Count 26 (+ 4) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.13 What are the pieces of data that are passed into a function called?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Arguments} \tn % Row Count 29 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.14 What are the variables that receive pieces of data in a function called?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Parameters} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 5 Questions (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.15 What is a parameter variable's scope?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A parameter variable's scope is the entire function in which the parameter is declared.} \tn % Row Count 3 (+ 3) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.16 When a parameter is changed, does this affect the argument that was passed into the parameter?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}No, it does not.} \tn % Row Count 6 (+ 3) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.17 The following statements call a function named `show\_data`. Which of the statements passes arguments by position, and which passes keyword arguments? \{\{nl\}\}~~~~a. \seqsplit{`show\_data(name='Kathryn'}, age=25)` \{\{nl\}\}~~~~b. `show\_data('Kathryn', 25)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}passes by keyword argument passes by position} \tn % Row Count 13 (+ 7) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.18 What is the scope of a global variable?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}The entire program} \tn % Row Count 15 (+ 2) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.19 Give one good reason that you should not use global variables in a program.} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Here are three: \{\{nl\}\}\{\{nl\}\}Global variables make debugging difficult. Any statement in a program can change the value of a global variable. If you find that the wrong value is being stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from. In a program with thousands of lines of code, this can be difficult. \{\{nl\}\}\{\{nl\}\}Functions that use global variables are usually dependent on those variables. If you want to use such a function in a different program, you will most likely have to redesign it so it does not rely on the global variable. \{\{nl\}\}\{\{nl\}\}Global variables make a program hard to understand. A global variable can be modified by any statement in the program. If you are to understand any part of the program that uses a global variable, you have to be aware of all the other parts of the program that access the global variable.} \tn % Row Count 37 (+ 22) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 5 Questions (cont)}} \tn % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.20 What is a global constant? Is it permissible to use global constants in a program?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A global constant is a name that is available to every function in the program. It is permissible to use global constants. Because their value cannot be changed during the program's execution, you do not have to worry about its value being altered.} \tn % Row Count 8 (+ 8) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.21 How does a value-returning function differ from the void functions?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}The difference is that a value returning function returns a value back to the statement that called it. A simple function does not return a value.} \tn % Row Count 14 (+ 6) % Row 21 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.22 What is a library function?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A prewritten function that performs some commonly needed task} \tn % Row Count 17 (+ 3) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.23 Why are library functions like "black boxes"?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}The term "black box" is used to describe any mechanism that accepts input, performs some operation (that cannot be seen) using the input, and produces output.} \tn % Row Count 23 (+ 6) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.24 What does the following statement do? `x = random.randint(1, 100)`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It assigns a random integer in the range of 1 through 100 to the variable `x`.} \tn % Row Count 27 (+ 4) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.25 What does the following statement do? `print(random.randint(1, 20))`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It prints a random integer in the range of 1 through 20.} \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 5 Questions (cont)}} \tn % Row 25 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.26 What does the following statement do? \seqsplit{`print(random.randrange(10}, 20))`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It prints a random integer in the range of 10 through 19.} \tn % Row Count 4 (+ 4) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.27 What does the following statement do? `print(random.random())`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It prints a random floating-point number in the range of 0.0 up to, but not including, 1.0.} \tn % Row Count 8 (+ 4) % Row 27 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.28 What does the following statement do? \seqsplit{`print(random.uniform(0.1}, 0.5))`} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It prints a random floating-point number in the range of 0.1 through 0.5.} \tn % Row Count 12 (+ 4) % Row 28 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.29 When the `random` module is imported, what does it use as a seed value for random number generation?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It uses the system time, retrieved from the computer's internal clock.} \tn % Row Count 17 (+ 5) % Row 29 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.30 What happens if the same seed value is always used for generating random numbers?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}If the same seed value were always used, the random number functions would always generate the same series of pseudorandom numbers.} \tn % Row Count 22 (+ 5) % Row 30 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.31 What is the purpose of the `return` statement in a function?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}It returns a value back to the part of the program that called it.} \tn % Row Count 26 (+ 4) % Row 31 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.32 Look at the following function definition: \{\{nl\}\}`def do\_something(number):` \{\{nl\}\}~~~~` return number * 2` \{\{nl\}\}a. What is the name of the function? \{\{nl\}\}b. What does the function do? \{\{nl\}\}c. Given the function definition, what will the following statement display? \seqsplit{`print(do\_something(10))`}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}a. `do\_something` \{\{nl\}\} b. It returns a value that is twice the argument passed to it. \{\{nl\}\}c. 20} \tn % Row Count 36 (+ 10) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Chapter 5 Questions (cont)}} \tn % Row 32 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.33 What is a Boolean function?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A function that returns either `True` or `False`} \tn % Row Count 3 (+ 3) % Row 33 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.34 What `import` statement do you need to write in a program that uses the `math` module?} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`import math`} \tn % Row Count 6 (+ 3) % Row 34 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5.35 Write a statement that uses a `math` module function to get the square root of 100 and assigns it to a variable.} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`square\_root = math.sqrt(100)`} \tn % Row Count 10 (+ 4) % Row 35 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{5.36 Write a statement that uses a `math` module function to convert 45 degrees to radians and assigns the value to a variable.} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`angle = math.radians(45)`} \tn % Row Count 14 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}