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{\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
"True" uses quotation marks, so the value is a string.
is_vegetable is an integer and can be used in arithmetic operations.
True or False is a Boolean variable.
True or False .
True and False are the only two values for a Boolean variable.
int results in implicit type conversion. The type conversion can cause issues later in the code if the programmer assumes is_dessert is still a Boolean.
False . Non-zero numbers convert to True .
True .
False .
True .
bool("False") is True .
True converted to a float is 1.0. False converted to a float is 0.0.
True converted to a string is "True" . False converted to a string is "False" .
True is converted to 1 , and False is converted to 0 .
"c" and "c" are equal, so "i" is compared to "o" . "i" is not equal to "o" so False is produced, though cilantro and coriander come from the same plant.
"d" is greater than, not less than, "c" , so False is produced.
4.2 If-else statements
age < 12 is between "if" and ":" and is an expression that evaluates to True or False .
-10 < 0 is true, so num is assigned with 25 . Then 25 < 100, so num is assigned with 25 + 50, which is 75 .
positive_num is always assigned with 0 . The positive input value is lost.
if branch is taken when x >= 15, so the else branch is taken when x is not greater than or equal to 15 .
40 > 30, so the body of the if statement is executed. y = 40 - 10 = 30.
if-else statement. Depending on x's value, y will either have a final value of 30 or 105 .
4.3 Boolean operations
2500 < 3000 is True , and 2.5 > 1.0 is True , so Jaden can enter.
3000 > 3000 is False . False and True is False . False and False is False . So no value of hrs_to_close will allow Darcy to enter.
False and True is False .
(8 < 10) and (21 > 20) evaluates to True and True, which is True . So the body of the if is executed, and z is assigned with 5 .
(21 < 30) or False evaluates to True or False, which is True .
(9%2==0 and 10%2 ==1) evaluates to (False and False), which is False . (9%2 == 1 and 10%2 == 0) evaluates to (True and True), which is True . False or True is True . The if statement checks whether the numbers form an even-odd or odd-even pair.
not(13 < 10) evaluates to not(False), which is True .
not(18 > 15 and 18 < 20) evaluates to not(True and True), then not(True), and finally, False .
65 > 60 is True , so is_turn is assigned with not(False), which is True .
4.4 Operator precedence
and .
and .
and .
x + (3*y) - 5. 8 + (3*9) - 5 = 30.
(8+3) * (9-5) = 11 * 4 = 44.
4.5 Chained decisions
elif will evaluate condition_2 if condition_1 is False and execute Body 2 if condition_2 is True .
x > 44, 42 > 44, is False , so x < 50 is evaluated. 42 < 50 is True , so y = 0 + 5. Only one of the if and elif branches is taken.
x < 0, Body 1 executes. Else if x == 0, Body 2 executes.
elif must be chained to a preceding statement and at the same indentation level.
if and if-elif statements are not chained. The first if evaluates to True and executes. Then the if-elif is evaluated and the elif branch executes.
hour < 8 and hour < 12 is False and hour < 13 is True .
elif can be chained to another elif to form a longer chained decision.
-1 < 0 and -2 < 0 is True , so y = 10.
price < 9.99. The second branch executes if price < 19.99 and 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
leaf_count == 3 is False , so the outer else ' s body is executed.
num_dancers is positive, so the else branch is taken. num_dancers is odd, so the error is printed before the program continues after the nested if and prints num_dancers.
256 == 513 is False , and 256 < 513 is True , so the elif branch is taken and Body 2 executes. 513 >= 512 is True , so Body 3 executes.
else isn't indented and is treated as the else to the original if . Thus the second else is not connected to an if statement and produces an error.
4.7 Conditional expressions
response with "even" if x is even. response is assigned with "odd" if x is odd.
100 < 100 is False so result is assigned with x - offset = 100 - 10 = 90.
min_num cannot be assigned with min_num = y. The correct expression is min_num = x if x < y else y"
(fee + 10) if hours > 12 else 2. So if hours > 12, the result will be fee + 10; otherwise, the result will be 2.


