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 93 name = raw_input('Please enter your name: ') or '' In other words, if the return value from raw_input is true (not an empty string), it is assigned to name (nothing changes); otherwise, the default '' is assigned to name. This sort of short-circuit logic can be used to implement the so-called “ternary operator” (or conditional operator), found in languages such as C and Java. For a thorough explanation, see Alex Martelli’s recipe on the subject in the Python Cookbook (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/ 52310). Assertions There is a useful relative of the if statement, which works more or less like this (pseudocode): if not condition: crash program Now, why on earth would you want something like that? Simply because it’s better that your program crashes when an error condition emerges than at a much later time. Basically, you can require that certain things be true. The keyword used in the statement is assert: >>> age = 10 >>> assert 0 < age < 100 >>> age = -1 >>> assert 0 < age < 100 Traceback (most recent call last): File "", line 1, in ? AssertionError It can be useful to put the assert statement in your program as a checkpoint, if you know something has to be true for your program to work correctly. A string may be added after the condition, to explain the assertion: >>> age = -1 >>> assert 0 < age < 100, 'The age must be realistic' Traceback (most recent call last): File "", line 1, in ? AssertionError: The age must be realistic Loops Now you know how to do something if a condition is true (or false), but how do you do something several times? For example, you might want to create a program that reminds you to pay the rent every month, but with the tools we have looked at until now, you’d have to write the program like this (pseudocode):
94 CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS send mail wait one month send mail wait one month send mail wait one month (...and so on) But what if you wanted it to continue doing this until you stopped it? Basically, you want something like this (again, pseudocode): while we aren't stopped: send mail wait one month Or, let’s take a simpler example. Let’s say that you want to print out all the numbers from 1 to 100. Again, you could do it the stupid way: print 1 print 2 print 3 . . . and so on. But you didn’t start using Python because you wanted to do stupid things, right? while Loops In order to avoid the cumbersome code of the preceding example, it would be useful to be able to do something like this: x = 1 while x
- 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 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123: 92 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
- Page 169 and 170: 138 CHAPTER 6 ■ ABSTRACTION Scope
- Page 171 and 172: 140 CHAPTER 7 ■ MORE ABSTRACTION
- Page 173 and 174: 142 CHAPTER 7 ■ MORE ABSTRACTION
CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS 93<br />
name = raw_input('Please enter your name: ') or ''<br />
In other words, if the return value from raw_input is true (not an empty string), it is assigned <strong>to</strong> name<br />
(nothing changes); otherwise, the default '' is assigned <strong>to</strong> name.<br />
This sort of short-circuit logic can be used <strong>to</strong> implement the so-called “ternary opera<strong>to</strong>r” (or conditional<br />
opera<strong>to</strong>r), found in languages such as C and Java. For a thorough explanation, see Alex Martelli’s recipe on the<br />
subject in the <strong>Python</strong> Cookbook (http://aspn.activestate.com/ASPN/Cookbook/<strong>Python</strong>/Recipe/<br />
52310).<br />
Assertions<br />
There is a useful relative of the if statement, which works more or less like this (pseudocode):<br />
if not condition:<br />
crash program<br />
Now, why on earth would you want something like that? Simply because it’s better that<br />
your program crashes when an error condition emerges than at a much later time. Basically,<br />
you can require that certain things be true. The keyword used in the statement is assert:<br />
>>> age = 10<br />
>>> assert 0 < age < 100<br />
>>> age = -1<br />
>>> assert 0 < age < 100<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
AssertionError<br />
It can be useful <strong>to</strong> put the assert statement in your program as a checkpoint, if you know<br />
something has <strong>to</strong> be true for your program <strong>to</strong> work correctly.<br />
A string may be added after the condition, <strong>to</strong> explain the assertion:<br />
>>> age = -1<br />
>>> assert 0 < age < 100, 'The age must be realistic'<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
AssertionError: The age must be realistic<br />
Loops<br />
Now you know how <strong>to</strong> do something if a condition is true (or false), but how do you do something<br />
several times? For example, you might want <strong>to</strong> create a program that reminds you <strong>to</strong> pay<br />
the rent every month, but with the <strong>to</strong>ols we have looked at until now, you’d have <strong>to</strong> write the<br />
program like this (pseudocode):