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 107 A Quick Summary In this chapter you’ve seen several kinds of statements: Printing. You can use the print statement to print several values by separating them with commas. If you end the statement with a comma, later print statements will continue printing on the same line. Importing. Sometimes you don’t like the name of a function you want to import—perhaps you’ve already used the name for something else. You can use the import...as... statement, to locally rename a function. Assignments. You’ve seen that through the wonder of sequence unpacking and chained assignments, you can assign values to several variables at once, and that with augmented assignments you can change a variable in place. Blocks. Blocks are used as a means of grouping statements through indentation. They are used in conditionals and loops, and as you see later in the book, in function and class definitions, among other things. Conditionals. A conditional statement either executes a block or not, depending on a condition (Boolean expression). Several conditionals can be strung together with if/elif/else. Assertions. An assertion simply asserts that something (a Boolean expression) is true, optionally with a string explaining why it has to be so. If the expression happens to be false, the assertion brings your program to a halt (or actually raises an exception—more on that in Chapter 8). It’s better to find an error early than to let it sneak around your program until you don’t know where it originated. Loops. You either can execute a block for each element in a sequence (such as a range of numbers) or continue executing it while a condition is true. To skip the rest of the block and continue with the next iteration, use the continue statement; to break out of the loop, use the break statement. Optionally, you may add an else clause at the end of the loop, which will be executed if you didn’t execute any break statements inside the loop. List comprehension. These aren’t really statements—they are expressions that look a lot like loops, which is why I grouped them with the looping statements. Through list comprehension, you can build new lists from old ones, applying functions to the elements, filtering out those you don’t want, and so on. The technique is quite powerful, but in many cases using plain loops and conditionals (which will always get the job done) may be more readable. pass, del, exec, and eval. The pass statement does nothing, which can be useful as a placeholder, for example. The del statement is used to delete variables or parts of a datastructure, but cannot be used to delete values. The exec statement is used to execute a string as if it were a Python program. The built-in function eval evaluates an expression written in a string and returns the result.
108 CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS New Functions in This Chapter Function chr(n) eval(source[, globals[, locals]]) enumerate(seq) ord(c) range([start,] stop[, step]) reversed(seq) sorted(seq[, cmp][, key][, reverse]) xrange([start,] stop[, step]) zip(seq1, seq2,...) Description Returns a one-character string with ordinal n (0 ≤ n < 256) Evaluates a string as an expression and returns the value Yields (index, value) pairs suitable for iteration Returns the integer ordinal value of a onecharacter string Creates a list of integers Yields the values of seq in reverse order, suitable for iteration Returns a list with the values of seq in sorted order Creates an xrange object, used for iteration Creates a new sequence suitable for parallel iteration What Now? Now you’ve cleared the basics. You can implement any algorithm you can dream up; you can read in parameters and print out the results. In the next couple of chapters, you learn about something that will help you write larger programs without losing the big picture. That something is called abstraction.
- 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 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: 106 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
- Page 175 and 176: 144 CHAPTER 7 ■ MORE ABSTRACTION
- Page 177 and 178: 146 CHAPTER 7 ■ MORE ABSTRACTION
- Page 179 and 180: 148 CHAPTER 7 ■ MORE ABSTRACTION
- Page 181 and 182: 150 CHAPTER 7 ■ MORE ABSTRACTION
- Page 183 and 184: 152 CHAPTER 7 ■ MORE ABSTRACTION
- Page 185 and 186: 154 CHAPTER 7 ■ MORE ABSTRACTION
- Page 187 and 188: 156 CHAPTER 7 ■ MORE ABSTRACTION
CHAPTER 5 ■ CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS 107<br />
A Quick Summary<br />
In this chapter you’ve seen several kinds of statements:<br />
Printing. You can use the print statement <strong>to</strong> print several values by separating them with<br />
commas. If you end the statement with a comma, later print statements will continue<br />
printing on the same line.<br />
Importing. Sometimes you don’t like the name of a function you want <strong>to</strong> import—perhaps<br />
you’ve already used the name for something else. You can use the import...as... statement,<br />
<strong>to</strong> locally rename a function.<br />
Assignments. You’ve seen that through the wonder of sequence unpacking and chained<br />
assignments, you can assign values <strong>to</strong> several variables at once, and that with augmented<br />
assignments you can change a variable in place.<br />
Blocks. Blocks are used as a means of grouping statements through indentation. They are<br />
used in conditionals and loops, and as you see later in the book, in function and class definitions,<br />
among other things.<br />
Conditionals. A conditional statement either executes a block or not, depending on a condition<br />
(Boolean expression). Several conditionals can be strung <strong>to</strong>gether with if/elif/else.<br />
Assertions. An assertion simply asserts that something (a Boolean expression) is true,<br />
optionally with a string explaining why it has <strong>to</strong> be so. If the expression happens <strong>to</strong> be false,<br />
the assertion brings your program <strong>to</strong> a halt (or actually raises an exception—more on that<br />
in Chapter 8). It’s better <strong>to</strong> find an error early than <strong>to</strong> let it sneak around your program<br />
until you don’t know where it originated.<br />
Loops. You either can execute a block for each element in a sequence (such as a range of<br />
numbers) or continue executing it while a condition is true. To skip the rest of the block<br />
and continue with the next iteration, use the continue statement; <strong>to</strong> break out of the loop,<br />
use the break statement. Optionally, you may add an else clause at the end of the loop,<br />
which will be executed if you didn’t execute any break statements inside the loop.<br />
List comprehension. These aren’t really statements—they are expressions that look a lot<br />
like loops, which is why I grouped them with the looping statements. Through list comprehension,<br />
you can build new lists from old ones, applying functions <strong>to</strong> the elements, filtering<br />
out those you don’t want, and so on. The technique is quite powerful, but in many cases<br />
using plain loops and conditionals (which will always get the job done) may be more readable.<br />
pass, del, exec, and eval. The pass statement does nothing, which can be useful as a placeholder,<br />
for example. The del statement is used <strong>to</strong> delete variables or parts of a datastructure,<br />
but cannot be used <strong>to</strong> delete values. The exec statement is used <strong>to</strong> execute a string as if it<br />
were a <strong>Python</strong> program. The built-in function eval evaluates an expression written in a<br />
string and returns the result.