Section 4.5 Apply Truthiness
Subgoals for Evaluating Selection Statements.
- Diagram which statements go together by indentation
-
For conditional, determine whether expression is true
- If true, follow true branch
- If false, follow next elif/else branch or exit conditional if no else branch
- Repeat step 2 as necessary
Subsection 4.5.1
Given the following declarations:
alpha = 2
beta = 1
delta = 3
eta = 0
gamma = 0
Evaluate these statements and determine what will be printed.
if alpha and beta:
if gamma and delta:
print("alpha, beta, gamma, delta")
elif gamma or delta:
print("alpha and beta, but not gamma and delta")
else:
print("alpha and beta, neither gamma nor delta")
else:
print("Neither")
Subsection 4.5.2 1. Diagram which statements go together by indentation
In this diagram, the first thing to note is the outer
if/else
statement highlighted in blue.The
True
branch of the parent block contains an inner if/elif/else
block.The
else
block only contains a single statement.Subsection 4.5.3 2. For conditional, determine whether expression is true
Because there is an outer
if
statement, we start with that one, and then repeat SG2 and SG3 for the other relevant statements.We evaluate
alpha and beta
, substituting 2
for alpha
and 1
for beta
. The and
operator evaluates each operand in turn until it encounters a value that is False
according to the rules of Truthiness. Since all the values are True
according to the rules of Truthiness, the last value (1
) is used as the result of the expression. According to the rules of Truthiness, any non-zero value will be considered True
, so in this case the expression evaluates to True
.Subsection 4.5.4 3. If true, follow true branch; If false, follow next elif/else branch or exit conditional if no else branch
The condition is
True
, so we execute the True
branch.The
True
branch contains an if
/elif
/else
statement, so we must repeat SG2 and SG3.Subsection 4.5.5 2. For conditional, determine whether expression is true
Start with the
if
in the inner block.First we evaluate
gamma and delta
, which is substituted as 0 and 3
. The and
operator evaluates each operand in turn, and immediately encounters 0
, which is False
according to the rules of Truthiness. That value is the result, and therefore the conditional expression will be considered False
.Subsection 4.5.6 3. If true, follow true branch; If false, follow next elif/else branch or exit conditional if no else branch
The condition is
False
, so we follow the elif
branch next and repeat SG2 and SG3.Subsection 4.5.7 2. For conditional, determine whether expression is true
In the
elif
branch, we evaluate gamma or delta
, which is substituted as 0 or 3
. The or
operator evaluates each operand in turn until it encounters a value that is True
according to the rules of Truthiness. The 0
is skipped because it is not True
according to the rules of Truthiness, but the 3
is True
according to the rules of Truthiness. Therefore, the result of the expression is 3
and the conditional expression will be considered True
.Subsection 4.5.8 3. If true, follow true branch; If false, follow next elif/else branch or exit conditional if no else branch
The condition is
True
, so we execute the body of the elif
statement (and will skip over the else
statement).print("alpha and beta, but not gamma and delta")
The line prints the text shown, and then skips over the rest of the
else
statements.Subsection 4.5.9 Answer
The final printed text is:
alpha and beta, but not gamma and delta
You have attempted of activities on this page.