Learning Python - Mark Lutz [183]
if elif else: # Optional else Basic Examples To demonstrate, let’s look at a few simple examples of the if statement at work. All parts are optional, except the initial if test and its associated statements. Thus, in the simplest case, the other parts are omitted: >>> if 1: ... print('true') ... true Notice how the prompt changes to ... for continuation lines when typing interactively in the basic interface used here; in IDLE, you’ll simply drop down to an indented line instead (hit Backspace to back up). A blank line (which you can get by pressing Enter twice) terminates and runs the entire statement. Remember that 1 is Boolean true, so this statement’s test always succeeds. To handle a false result, code the else: >>> if not 1: ... print('true') ... else: ... print('false') ... false Multiway Branching Now here’s an example of a more complex if statement, with all its optional parts present: >>> x = 'killer rabbit' >>> if x == 'roger': ... print("how's jessica?") ... elif x == 'bugs': ... print("what's up doc?") ... else: ... print('Run away! Run away!') ... Run away! Run away! This multiline statement extends from the if line through the else block. When it’s run, Python executes the statements nested under the first test that is true, or the else part if all tests are false (in this example, they are). In practice, both the elif and else parts may be omitted, and there may be more than one statement nested in each section. Note that the words if, elif, and else are associated by the fact that they line up vertically, with the same indentation. If you’ve used languages like C or Pascal, you might be interested to know that there is no switch or case statement in Python that selects an action based on a variable’s value. Instead, multiway branching is coded either as a series of if/elif tests, as in the prior example, or by indexing dictionaries or searching lists. Because dictionaries and lists can be built at runtime, they’re sometimes more flexible than hardcoded if logic: >>> choice = 'ham' >>> print({'spam': 1.25, # A dictionary-based 'switch' ... 'ham': 1.99, # Use has_key or get for default ... 'eggs': 0.99, ... 'bacon': 1.10}[choice]) 1.99 Although it may take a few moments for this to sink in the first time you see it, this dictionary is a multiway branch—indexing on the key choice branches to one of a set of values, much like a switch in C. An almost equivalent but more verbose Python if statement might look like this: >>> if choice == 'spam': ... print(1.25) ... elif choice == 'ham': ... print(1.99) ... elif choice == 'eggs': ... print(0.99) ... elif choice == 'bacon': ... print(1.10) ... else: ... print('Bad choice') ... 1.99 Notice the else clause on the if here to handle the default case when no key matches. As we saw in Chapter 8, dictionary defaults can be coded with in expressions, get method calls, or exception catching. All of the same techniques can be used here to code a default action in a dictionary-based multiway branch. Here’s the get scheme at work with defaults: >>> branch = {'spam': 1.25, ... 'ham': 1.99, ... 'eggs': 0.99} >>> print(branch.get('spam', 'Bad choice')) 1.25 >>> print(branch.get('bacon', 'Bad choice')) Bad choice An in membership test in an if statement can have the same default effect: >>> choice = 'bacon' >>> if choice in branch: ... print(branch[choice]) ... else: ... print('Bad choice') ... Bad choice Dictionaries are good for associating values with keys, but what about the more complicated actions you can code in the statement blocks associated with if statements? In Part IV, you’ll learn that dictionaries can also contain functions to represent more complex branch actions and implement general jump tables. Such functions appear as dictionary values, may be coded as function names