Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

4.1 Boolean values

1.
c. "True" uses quotation marks, so the value is a string.
2.
b. is_vegetable is an integer and can be used in arithmetic operations.
3.
a. A variable assigned with either True or False is a Boolean variable.
4.
a. A Boolean variable represents the value True or False .
5.
b. True and False are the only two values for a Boolean variable.
6.
b. Assigning a bool with an 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.
7.
b. Any numeric type representing 0 converts to False . Non-zero numbers convert to True .
8.
a. A negative number is non-zero and converts to True .
9.
b. The empty string is the only string that converts to False .
10.
a. "0" is a non-empty string and is converted to True .
11.
a. The input is read in as a string, so bool("False") is True .
12.
b. True converted to a float is 1.0. False converted to a float is 0.0.
13.
b. True converted to a string is "True" . False converted to a string is "False" .
14.
b. When a Boolean is converted to a numeric type, True is converted to 1 , and False is converted to 0 .
15.
b. 14 is greater than 13, so 14 is not less than or equal to 13.
16.
a. 0 is less than 0.4, so 0 is not equal to 0.4.
17.
b. 4 is equal to 4.0, so 4 is not less than 4.0.
18.
c. Numeric types cannot be compared to strings using the comparison operators: >, <, >=, <=. Using == or != will produce a Boolean.
19.
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" so False is produced, though cilantro and coriander come from the same plant.
20.
b. Letters have ascending Unicode values in alphabetical order (and are case sensitive). "d" is greater than, not less than, "c" , so False is produced.

4.2 If-else statements

1.
b. age < 12 is between "if" and ":" and is an expression that evaluates to True or False .
2.
c. The program executes the body when the condition is true.
3.
b. The body, line 3, isn't executed because the condition is false.
4.
c. -10 < 0 is true, so num is assigned with 25 . Then 25 < 100, so num is assigned with 25 + 50, which is 75 .
5.
b. Line 4 is not indented, so positive_num is always assigned with 0 . The positive input value is lost.
6.
c. The if branch is taken when x >= 15, so the else branch is taken when x is not greater than or equal to 15 .
7.
a. 40 > 30, so the body of the if statement is executed. y = 40 - 10 = 30.
8.
b. One of the branches is always taken in an if-else statement. Depending on x's value, y will either have a final value of 30 or 105 .

4.3 Boolean operations

1.
a. 2500 < 3000 is True , and 2.5 > 1.0 is True , so Jaden can enter.
2.
b. 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.
3.
b. False and True is False .
4.
b. (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 .
5.
a. (21 < 30) or False evaluates to True or False, which is True .
6.
c. The discount does not apply for ages between 12 and 65 inclusive.
7.
a. (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.
8.
a. not(13 < 10) evaluates to not(False), which is True .
9.
b. not(18 > 15 and 18 < 20) evaluates to not(True and True), then not(True), and finally, False .
10.
a. 65 > 60 is True , so is_turn is assigned with not(False), which is True .

4.4 Operator precedence

1.
b. Exponentiation has the highest precedence, then division, then addition.
2.
a. Multiplication has the highest precedence, then greater than, then not.
3.
b. Division has the highest precedence, then equality and less than, then and .
4.
b. Multiplication and division have the highest precedence, are left associative, and have higher precedence than addition.
5.
b. Exponentiation is right associative and has higher precedence than multiplication.
6.
c. The expression is valid and is separated into comparisons connected with and .
7.
a. The equality operator, ==, has higher precedence than the logical and .
8.
a. The expression is evaluated as x + (3*y) - 5. 8 + (3*9) - 5 = 30.
9.
b. The expressions in parentheses are evaluated first. (8+3) * (9-5) = 11 * 4 = 44.

4.5 Chained decisions

1.
b. elif will evaluate condition_2 if condition_1 is False and execute Body 2 if condition_2 is True .
2.
b. 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.
3.
a. If x < 0, Body 1 executes. Else if x == 0, Body 2 executes.
4.
a. The elif must be chained to a preceding statement and at the same indentation level.
5.
c. The 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.
6.
b. The third branch executes if hour < 8 and hour < 12 is False and hour < 13 is True .
7.
c. An elif can be chained to another elif to form a longer chained decision.
8.
a. Only one branch is executed in a chained decision statement. -1 < 0 and -2 < 0 is True , so y = 10.
9.
c. The first branch executes if 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

1.
c. leaf_count == 3 is False , so the outer else ' s body is executed.
2.
c. 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.
3.
b. 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.
4.
c. The first 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

1.
c. The conditional expression will assign response with "even" if x is even. response is assigned with "odd" if x is odd.
2.
a. 100 < 100 is False so result is assigned with x - offset = 100 - 10 = 90.
3.
c. min_num cannot be assigned with min_num = y. The correct expression is min_num = x if x < y else y"
4.
b. Conditional expressions that evaluate to a Boolean are redundant. The Boolean expression should be used instead.
5.
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.
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.