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.

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 7 a simple if statement asks if the user entered the letters 'cowboys' on line 2. Note the use of the function upper() on line 7.

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 16-22 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 16 will evaluate to False in which case the block of statements below the 'else statement' on line 19 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 29-41 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 25, then the condition on line 32 will be true, the 'block' on line 33 will be run, and execution will jump to line 42 after the tests. See the use of the 'or' logical operator on line 29. 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 49-59 provide another example of the elif. Notice also that 'and' and 'or' logical operators are included for examples.

 

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 84 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.