Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS 87 >>> False == 0 True >>> True + False + 42 43 So now, if you see a logical expression returning 1 or 0 (probably in an older version of Python), you will know that what is really meant is True and False. The Boolean values True and False belong to the type bool, which can be used (just like, for example, list, str, and tuple) to convert other values: >>> bool('I think, therefore I am') True >>> bool(42) True >>> bool('') False >>> bool(0) False Because any value can be used as Boolean values, you will most likely rarely (if ever) need such an explicit conversion. Conditional Execution and the if Statement Truth values can be combined (which you’ll see in a while), but let’s first see what you can use them for. Try running the following script: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr. Gumby' This is the if statement, which lets you do conditional execution. That means that if the condition (the expression after if but before the colon) evaluates to true (as defined previously), the following block (in this case, a single print statement) is executed. If the condition is false, then the block is not executed (but you guessed that, didn’t you?). ■Note In the sidebar “Sneak Peek: The if Statement” in Chapter 1, the statement was written on a single line. That is equivalent to using a single-line block, as in the preceding example. else Clauses In the example from the previous section, if you enter a name that ends with “Gumby,” the method name.endswith returns True, making the if statement enter the block, and the greeting is printed. If you want, you can add an alternative, with the else clause (called a clause because it isn’t really a separate statement, just a part of the if statement):
88 CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr. Gumby' else: print 'Hello, stranger' Here, if the first block isn’t executed (because the condition evaluated to false), you enter the second block instead. This really makes you see how easy it is to read Python code, doesn’t it? Just read the code aloud (from if) and it sounds just like a normal (or perhaps not quite normal) sentence. elif Clauses If you want to check for several conditions, you can use elif, which is short for “else if.” It is a combination of an if clause and an else clause—an else clause with a condition: num = input('Enter a number: ') if num > 0: print 'The number is positive' elif num < 0: print 'The number is negative' else: print 'The number is zero' Nesting Blocks Let’s throw in a few bells and whistles. You can have if statements inside other if statement blocks, as follows: name = raw_input('What is your name? ') if name.endswith('Gumby'): if name.startswith('Mr.'): print 'Hello, Mr. Gumby' elif name.startswith('Mrs.'): print 'Hello, Mrs. Gumby' else: print 'Hello, Gumby' else: print 'Hello, stranger' Here, if the name ends with “Gumby,” you check the start of the name as well—in a separate if statement inside the first block. Note the use of elif here. The last alternative (the else clause) has no condition—if no other alternative is chosen, you use the last one. If you want to, you can leave out either of the else clauses. If you leave out the inner else clause, names that don’t start with either “Mr.” or “Mrs.” are ignored (assuming the name was “Gumby”). If you drop the outer else clause, strangers are ignored.
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123 and 124: 92 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 125 and 126: 94 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 127 and 128: 96 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 129 and 130: 98 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 131 and 132: 100 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 133 and 134: 102 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 135 and 136: 104 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 137 and 138: 106 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 139 and 140: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 141 and 142: 110 CHAPTER 6 ■ ABSTRACTION But w
- Page 143 and 144: 112 CHAPTER 6 ■ ABSTRACTION ■Ti
- Page 145 and 146: 114 CHAPTER 6 ■ ABSTRACTION Can I
- Page 147 and 148: 116 CHAPTER 6 ■ ABSTRACTION stora
- Page 149 and 150: 118 CHAPTER 6 ■ ABSTRACTION 4. Yo
- Page 151 and 152: 120 CHAPTER 6 ■ ABSTRACTION The p
- Page 153 and 154: 122 CHAPTER 6 ■ ABSTRACTION >>> p
- Page 155 and 156: 124 CHAPTER 6 ■ ABSTRACTION Also,
- Page 157 and 158: 126 CHAPTER 6 ■ ABSTRACTION Feel
- Page 159 and 160: 128 CHAPTER 6 ■ ABSTRACTION ■No
- Page 161 and 162: 130 CHAPTER 6 ■ ABSTRACTION So yo
- Page 163 and 164: 132 CHAPTER 6 ■ ABSTRACTION numer
- Page 165 and 166: 134 CHAPTER 6 ■ ABSTRACTION LAMBD
- Page 167 and 168: 136 CHAPTER 6 ■ ABSTRACTION In th
88 CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS<br />
name = raw_input('What is your name? ')<br />
if name.endswith('Gumby'):<br />
print 'Hello, Mr. Gumby'<br />
else:<br />
print 'Hello, stranger'<br />
Here, if the first block isn’t executed (because the condition evaluated <strong>to</strong> false), you enter<br />
the second block instead. This really makes you see how easy it is <strong>to</strong> read <strong>Python</strong> code, doesn’t<br />
it? Just read the code aloud (from if) and it sounds just like a normal (or perhaps not quite<br />
normal) sentence.<br />
elif Clauses<br />
If you want <strong>to</strong> check for several conditions, you can use elif, which is short for “else if.” It is a<br />
combination of an if clause and an else clause—an else clause with a condition:<br />
num = input('Enter a number: ')<br />
if num > 0:<br />
print 'The number is positive'<br />
elif num < 0:<br />
print 'The number is negative'<br />
else:<br />
print 'The number is zero'<br />
Nesting Blocks<br />
Let’s throw in a few bells and whistles. You can have if statements inside other if statement<br />
blocks, as follows:<br />
name = raw_input('What is your name? ')<br />
if name.endswith('Gumby'):<br />
if name.startswith('Mr.'):<br />
print 'Hello, Mr. Gumby'<br />
elif name.startswith('Mrs.'):<br />
print 'Hello, Mrs. Gumby'<br />
else:<br />
print 'Hello, Gumby'<br />
else:<br />
print 'Hello, stranger'<br />
Here, if the name ends with “Gumby,” you check the start of the name as well—in a separate<br />
if statement inside the first block. Note the use of elif here. The last alternative (the else clause)<br />
has no condition—if no other alternative is chosen, you use the last one. If you want <strong>to</strong>, you can<br />
leave out either of the else clauses. If you leave out the inner else clause, names that don’t start<br />
with either “Mr.” or “Mrs.” are ignored (assuming the name was “Gumby”). If you drop the<br />
outer else clause, strangers are ignored.