COSC 1336 | PyThon

ACC

Selection

Foreword

Selection is the second of the three fundamental programming control structures. In this chapter we consider the various constructs that can be used in Python to make decisions. Remember that when making any decision in programming, the same question is asked. "Is it true?" That's it. No matter how large or small the expression, it must always resolve to a simple boolean answer of True or False. Also remember to include decision symbols in your flowcharts to represent decisions in code.

The following five decision structures are explained in this chapter:

  • if statement
  • if else statement
  • if elif statement
  • Nested if's
  • Chained relational operators

if statement

An if statement is the simplest of the selection control structures. If the condition is true, then the statement(s) within the block just below the if statement will be executed. If the condition is False, the statement(s) in the block are skipped. In the code example below, on line 9 a simple if statement asks if the user entered the letters 'cowboys' on line 4. Note the use of the function upper() on line 9.

The upper() function performs a temporary conversion of the letters in the fav_team variable to all uppercase. This enables the program to check for the letters 'cowboys' irrespective of the case combination the user entered. For instance, if the user entered all lowercase, all uppercase, or a mixed combination of case, by temporarily converting to uppercase and comparing the result to all uppercase (COWBOYS), the results will be true as long as those letters are entered in the correct order. At other locations in the program, the lower() function is used for the same purpose.

if else statement

Lines 18-24 demonstrate the next selection control structure, the 'if else statement'. With the 'if else statement' one block of code is executed if the condition is True and another block is executed if the condition is false. So, in the example, animal_identity == turtle and fav_food == pizza then the block directly below the 'if statement' will be executed. However, if one or both subconditions are False, then the main condition on line 18 will evaluate to False in which case the block of statements below the 'else statement' on line 21 will be executed. The 'if else statement' is ideal for 'binary' situations in which one block should be executed if a condition is True and another block should be executed if the condition is False.

if elif statement

The 'if elif statement' enables a continuation of conditional testing. There is no limit to the number of 'if elif statements' that can be assembled together to produce an extensive series of logical tests. Lines 31-43 perform tests to determine the letter grade for a numerical grade entered by the user. In series of 'if elif statements' like that shown, as soon as one of the conditions evaluates to True the statements in that block will be executed and then the remaining elif's will be skipped. For instance, if a grade of 95 is entered on line 27, then the condition on line 34 will be true, the 'block' on line 35 will be run, and execution will jump to line 44 after the tests. See the use of the 'or' logical operator on line 31. The 'or', combined with the two conditions, determine if the grade entered is outside of the range between 0-100.

The if and elif statements on lines 51-61 provide another example of the elif. Notice also that 'and' and 'or' logical operators are included for examples.

Short-Circuit Evaluation

Suppose you have an if statement with a series of and conditionals. How many and's must Python check to determine if the entire if statement is true? Well, for the total if statement to be true, all and's must be true so Python must check all of them. However, suppose the second and is false. Does Python need to check the remaining and's? No, Python can (and does) stop evaluating as soon as one of the and's is false. This behavior is known as short-circuit evaluation and is available natively in Python.

What if we have have an if statement with a series of or conditionals. How many or's must Python check to determine if the entire if statement is true? Python can (and does) stop evaluting as soon as one of the or's is true. So, short-circuit evaluation works for a series of or's like it does for a series of and's.

Nested if's

Nested if's enable the developer to cascade conditions and formulate intricate decision pathways. In the example, 'if else' and 'if elif' statements are used in combination to resolve multiple conditions. Something to keep in mind... when using an 'elif' or multiple 'elifs', it usually makes sense to ensure there is an 'else' to handle the default condition when none of the above are True. This concept was covered in an earlier chapter and is called a trailing else.

Chained Relational Operators

The last decision structure we consider in this chapter is the chained relational operators. Unlike many other popular programming languages, Python allows relational operators to be chained and used like they would appear in an algebraic expression. For example, two less than comparisons (<) are on line 87 in the 'if statement'. The alternative, and slightly longer way of writing those comparisons, would require an 'and' operator:

 if a < b and b < c:

The code examples to match the flowcharts and discussions above.

Here's the output.

Those of you familiar with other languages may be wondering about the switch/case decision structure. Python does not have that structure but the same logic can be accomplished with a combination of 'if elif' statements.

Wait, What?

06Dec2021 - Python 3.10.1, the first maintenance release of Python 3.10 was released. It included Structural Pattern Matching in the form of match/case statements. These are analogous to the switch/case statements available in other languages.

For more about the new match/case, see the following: Structural Pattern Matching: Tutorial.