16.3: Chapter 4
- Page ID
- 117499
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)4.1 Boolean values
- c.
"True"uses quotation marks, so the value is a string. - b.
is_vegetableis an integer and can be used in arithmetic operations. - a. A variable assigned with either
TrueorFalseis a Boolean variable. - a. A Boolean variable represents the value
TrueorFalse. - b.
TrueandFalseare the only two values for a Boolean variable. - b. Assigning a bool with an
intresults in implicit type conversion. The type conversion can cause issues later in the code if the programmer assumesis_dessertis still a Boolean. - b. Any numeric type representing 0 converts to
False. Non-zero numbers convert toTrue. - a. A negative number is non-zero and converts to
True. - b. The empty string is the only string that converts to
False. - a. "0" is a non-empty string and is converted to
True. - a. The input is read in as a string, so
bool("False")isTrue. - b.
Trueconverted to a float is1.0.Falseconverted to a float is0.0. - b.
Trueconverted to a string is"True". False converted to a string is"False". - b. When a Boolean is converted to a numeric type,
Trueis converted to1, andFalseis converted to0. - b. 14 is greater than 13, so 14 is not less than or equal to 13.
- a. 0 is less than 0.4, so 0 is not equal to 0.4.
- b. 4 is equal to 4.0, so 4 is not less than 4.0.
- c. Numeric types cannot be compared to strings using the comparison operators: >, <, >=, <=. Using == or != will produce a Boolean.
- b. Strings are compared by comparing each character's Unicode values. Letters have ascending Unicode values in alphabetical order (and are case sensitive).
"c"and"c"are equal, so"i"is compared to"o"."i"is not equal to"o"soFalseis produced, though cilantro and coriander come from the same plant. - b. Letters have ascending Unicode values in alphabetical order (and are case sensitive).
"d"is greater than, not less than,"c", soFalseis produced.
4.2 If-else statements
- b.
age < 12is between"if"and ":" and is an expression that evaluates toTrueorFalse. - c. The program executes the body when the condition is true.
- b. The body, line 3, isn't executed because the condition is false.
- c.
-10 < 0is true, sonumis assigned with25. Then25 < 100, sonumis assigned with25 + 50, which is75. - b. Line 4 is not indented, so
positive_numis always assigned with0. The positive input value is lost. - c. The
ifbranch is taken whenx >= 15, so theelsebranch is taken whenxis not greater than or equal to15. - a.
40 > 30, so the body of theifstatement is executed.y = 40 - 10 = 30. - b. One of the branches is always taken in an
if-elsestatement. Depending onx's value,ywill either have a final value of30or105.
4.3 Boolean operations
- a.
2500 < 3000isTrue, and2.5 > 1.0isTrue, so Jaden can enter. - b.
3000 > 3000isFalse. False and True isFalse.False and FalseisFalse. So no value ofhrs_to_closewill allow Darcy to enter. - b.
False and TrueisFalse. - b.
(8 < 10) and (21 > 20)evaluates toTrue and True, which isTrue. So the body of theifis executed, andzis assigned with5. - a.
(21 < 30) or Falseevaluates toTrue or False, which isTrue. - c. The discount does not apply for ages between 12 and 65 inclusive.
- a.
(9%2==0 and 10%2 ==1)evaluates to(False and False), which isFalse.(9%2 == 1 and 10%2 == 0)evaluates to(True and True), which isTrue.False or TrueisTrue. Theifstatement checks whether the numbers form an even-odd or odd-even pair. - a.
not(13 < 10)evaluates tonot(False), which isTrue. - b.
not(18 > 15 and 18 < 20)evaluates tonot(True and True), thennot(True), and finally,False. - a.
65 > 60isTrue, sois_turnis assigned withnot(False), which isTrue.
4.4 Operator precedence
- b. Exponentiation has the highest precedence, then division, then addition.
- a. Multiplication has the highest precedence, then greater than, then not.
- b. Division has the highest precedence, then equality and less than, then
and. - b. Multiplication and division have the highest precedence, are left associative, and have higher precedence than addition.
- b. Exponentiation is right associative and has higher precedence than multiplication.
- c. The expression is valid and is separated into comparisons connected with
and. - a. The equality operator, ==, has higher precedence than the logical
and. - a. The expression is evaluated as
x + (3*y) - 5. 8 + (3*9) - 5 = 30. - b. The expressions in parentheses are evaluated first.
(8+3) * (9-5) = 11 * 4 = 44.
4.5 Chained decisions
- b.
elifwill evaluatecondition_2ifcondition_1isFalseand executeBody 2ifcondition_2isTrue. - b.
x > 44,42 > 44, isFalse, sox < 50is evaluated.42 < 50isTrue, soy = 0 + 5. Only one of theifandelifbranches is taken. - a. If
x < 0,Body 1executes. Else ifx == 0,Body 2executes. - a. The
elifmust be chained to a preceding statement and at the same indentation level. - c. The
ifandif-elifstatements are not chained. The firstifevaluates toTrueand executes. Then theif-elifis evaluated and theelifbranch executes. - b. The third branch executes if
hour < 8 and hour < 12isFalseandhour < 13isTrue. - c. An
elifcan be chained to anotherelifto form a longer chained decision. - a. Only one branch is executed in a chained decision statement.
-1 < 0 and -2 < 0isTrue, soy = 10. - c. The first branch executes if
price < 9.99. The second branch executes ifprice < 19.99and the first condition fails: that is,price >= 9.99. The third branch executes if the first and second conditions fail: that is,price >= 19.99. Chaining can simplify decision statements.
4.6 Nested decisions
- c.
leaf_count == 3isFalse, so the outerelse 's body is executed. - c.
num_dancersis positive, so theelsebranch is taken.num_dancersis odd, so the error is printed before the program continues after the nestedifand printsnum_dancers. - b.
256 == 513isFalse, and256 < 513isTrue, so theelifbranch is taken andBody 2executes.513 >= 512isTrue, soBody 3executes. - c. The first
elseisn't indented and is treated as theelseto the originalif. Thus the secondelseis not connected to anifstatement and produces an error.
4.7 Conditional expressions
- c. The conditional expression will assign
responsewith"even"ifxis even.responseis assigned with"odd"ifxis odd. - a.
100 < 100isFalsesoresultis assigned withx - offset = 100 - 10 = 90. - c.
min_numcannot be assigned withmin_num = y. The correct expression ismin_num = x if x < y else y" - b. Conditional expressions that evaluate to a Boolean are redundant. The Boolean expression should be used instead.
- b. The expression is evaluated as
(fee + 10) if hours > 12 else 2. So if hours > 12, the result will be fee + 10; otherwise, the result will be 2.


