Skip to main content

Section 3.5 Apply Truthiness

Subgoals for Evaluating Selection Statements.

  1. Diagram which statements go together by indentation
  2. For conditional, determine whether expression is true
    1. If true, follow true branch
    1. If false, follow next elif/else branch or exit conditional if no else branch
  3. Repeat step 2 as necessary

Subsection 3.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 3.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.
Figure 3.5.1.

Subsection 3.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 3.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.
Figure 3.5.2.

Subsection 3.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 3.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 3.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 3.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 3.5.9 Answer

The final printed text is:
alpha and beta, but not gamma and delta
You have attempted of activities on this page.