5 Algorithmic Thinking

5.4 Branching

Goals:
Learn how to create if-then statements in Python;
Learn various boolean operators and what boolean values are.

Chapter 4 introduced Excel’s 𝙸𝙵 command. In this section, we will introduce the equivalent if command in Python, which is, from a visual standpoint, more friendly to read and construct.

First, however, we must introduce another data type: boolean. Unlike number-types or strings, boolean has only two possible values: True or False. In Python, there are certain comparisons that will evaluate to True or False. The following table gives those we will use:

QuestionPython SyntaxAre 𝚡 and 𝚢 equal? x == yIs 𝚡 less than or equal to 𝚢? x <= yIs 𝚡 less than 𝚢? x < yIs 𝚡 greater than or equal to 𝚢? x >= yIs 𝚡 greater than 𝚢? x > yAre 𝚡 and 𝚢 not equal? x <> y

Figure 5.15 shows two Shell windows with examples:

Figure 5.15: Boolean Examples

In the following, let BV1 and BV2 denote a boolean values, i.e., something that evaluates to True or False. Here’s how the if statement works:

PythonComments𝚒𝚏 BV1:checks if BV1 is 𝚃𝚛𝚞𝚎 or 𝙵𝚊𝚕𝚜𝚎    Command(s)do this group if BV1 is 𝚃𝚛𝚞𝚎𝚎𝚕𝚒𝚏 BV2:checks if BV2 is 𝚃𝚛𝚞𝚎 or 𝙵𝚊𝚕𝚜𝚎    Command(s)do this group if BV2 is 𝚃𝚛𝚞𝚎else:BV1 and BV2 are 𝙵𝚊𝚕𝚜𝚎    Command(s)do this group if BV is 𝙵𝚊𝚕𝚜𝚎

Notes:

  1. 1.

    The else part is optional, i.e., a standalone if is okay.

  2. 2.

    The elif part may also be optional, given the scenario.

  3. 3.

    As with the def command, the colon : denotes the beginning of the group of command(s) to execute. If one forgets the colon, IDLE won’t auto-indent and the Shell will give a syntax error. And, as with the def examples prior, grouping is done by indenting in multiples of 4; any other number of indentations will cause a syntax error.

Some examples:

Example 1Example 2Example 3Example 4if 3 < 4:if 3 > 4:x = 3x=3    x = 2    x = 2if x < 1:if x<1:    x = x**2    x = x**2    print x**2    print x**2else:else:else:elif x < 3:    x = 5    x = 5    if x < 3:    print x**3    x = x**2    x = x**2        print x**3else:print xprint x    else:    print x**4        print x**4Value printed: 4Value printed: 25Value printed: 81Value printed: 81

Concepts Check: 1. What is the output of the following?
x = 5if x == 5:print 5else:print 10
Answer: 5
2. What is the output of the following?
x = 4if x <> 4:print 5else:print 10
Answer: 10

5.4.1 Exercises

  1. 1.

    Answer the following as True or False.

    1. (a)

      There is no difference between = and ==.

    2. (b)

      The syntax >< is used to denote not equal to.

    3. (c)

      Boolean values consist of only True, False, or Maybe.

    4. (d)

      Indentation is necessary for if statements.

    5. (e)

      The else part is required in all if statements.

  2. 2.

    Write a function that takes as input a number x, and outputs the following: if x is greater than 5, return 9; otherwise return 3.

  3. 3.

    Write a function that uses randbetween to randomly generate an integer n between 0 and 2, inclusive. The function returns ”Yes” if n is equal to 0, ”No” if n is 1, and ”Maybe” otherwise.