Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 2 ■ LISTS AND TUPLES 37 >>> numbers[8:3:-1] [9, 8, 7, 6, 5] >>> numbers[10:0:-2] [10, 8, 6, 4, 2] >>> numbers[0:10:-2] [] >>> numbers[::-2] [10, 8, 6, 4, 2] >>> numbers[5::-2] [6, 4, 2] >>> numbers[:5:-2] [10, 8] Getting things right here can involve a bit of thinking. As you can see, the first limit (the leftmost) is still inclusive, while the second (the rightmost) is exclusive. When using a negative step size, you have to have a first limit (start index) that is higher than the second one. What may be a bit confusing is that when you leave the start and end indices implicit, Python does the “right thing” here; for a positive step size it moves from the beginning toward the end, and for a negative step size it moves from the end toward the beginning. Adding Sequences Sequences can be concatenated with the addition (plus) operator: >>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] >>> 'Hello, ' + 'world!' 'Hello, world!' >>> [1, 2, 3] + 'world!' Traceback (innermost last): File "", line 1, in ? [1, 2, 3] + 'world!' TypeError: can only concatenate list (not "string") to list As you can see from the error message, you can’t concatenate a list and a string, although both are sequences. In general, you can only concatenate two sequences of the same kind. Multiplication Multiplying a sequence by a number x creates a new sequence where the original sequence is repeated x times: >>> 'python' * 5 'pythonpythonpythonpythonpython' >>> [42] * 10 [42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
38 CHAPTER 2 ■ LISTS AND TUPLES None, Empty Lists, and Initialization An empty list is simply written as two brackets ([])—there’s nothing in it. But what if you want to have a list with room for ten elements but with nothing useful in it? You could use [42]*10, as before, or perhaps more realistically [0]*10. You now have a list with ten zeros in it. Sometimes, however, you would like a value that somehow means “nothing,” as in “we haven’t put anything here yet.” That’s when you use None. None is a Python value and means exactly that— “nothing here.” So if you want to initialize a list of length 10, you could do the following: >>> sequence = [None] * 10 >>> sequence [None, None, None, None, None, None, None, None, None, None] Listing 2-3 contains a program that prints (to the screen) a “box” made up of characters, which is centered on the screen and adapted to the size of a sentence supplied by the user. The following is a sample run: Sentence: He's a very naughty boy! +——————————————————————————+ | | | He's a very naughty boy! | | | +——————————————————————————+ The code may look complicated, but it’s basically just arithmetic—figuring out how many spaces, dashes, and so on you need in order to place things correctly. Listing 2-3. Sequence (String) Multiplication Example # Prints a sentence in a centered "box" of correct width # Note that the integer division operator (//) only works in Python # 2.2 and newer. In earlier versions, simply use plain division (/) sentence = raw_input("Sentence: ") screen_width = 80 text_width = len(sentence) box_width = text_width + 6 left_margin = (screen_width - box_width) // 2 print print ' ' * left_margin + '+' + '-' * (box_width-2) + '+' print ' ' * left_margin + '| ' + ' ' * text_width + ' |' print ' ' * left_margin + '| ' + sentence + ' |' print ' ' * left_margin + '| ' + ' ' * text_width + ' |' print ' ' * left_margin + '+' + '-' * (box_width-2) + '+' print
- Page 17 and 18: xvi ■CONTENTS Dynamic Web Pages w
- Page 19 and 20: xviii ■CONTENTS ■CHAPTER 20 Pro
- Page 21 and 22: xx ■CONTENTS Second Implementatio
- Page 23 and 24: xxii ■CONTENTS Preparations . . .
- Page 26: About the Technical Reviewer ■JER
- Page 30 and 31: Introduction A C program is like a
- Page 32 and 33: CHAPTER 1 ■ ■ ■ Instant Hacki
- Page 34 and 35: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 36 and 37: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 38 and 39: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 40 and 41: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 42 and 43: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 44 and 45: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 46 and 47: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- 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 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
38 CHAPTER 2 ■ LISTS AND TUPLES<br />
None, Empty Lists, and Initialization<br />
An empty list is simply written as two brackets ([])—there’s nothing in it. But what if you want<br />
<strong>to</strong> have a list with room for ten elements but with nothing useful in it? You could use [42]*10,<br />
as before, or perhaps more realistically [0]*10. You now have a list with ten zeros in it. Sometimes,<br />
however, you would like a value that somehow means “nothing,” as in “we haven’t put<br />
anything here yet.” That’s when you use None. None is a <strong>Python</strong> value and means exactly that—<br />
“nothing here.” So if you want <strong>to</strong> initialize a list of length 10, you could do the following:<br />
>>> sequence = [None] * 10<br />
>>> sequence<br />
[None, None, None, None, None, None, None, None, None, None]<br />
Listing 2-3 contains a program that prints (<strong>to</strong> the screen) a “box” made up of characters,<br />
which is centered on the screen and adapted <strong>to</strong> the size of a sentence supplied by the user. The<br />
following is a sample run:<br />
Sentence: He's a very naughty boy!<br />
+——————————————————————————+<br />
| |<br />
| He's a very naughty boy! |<br />
| |<br />
+——————————————————————————+<br />
The code may look complicated, but it’s basically just arithmetic—figuring out how many<br />
spaces, dashes, and so on you need in order <strong>to</strong> place things correctly.<br />
Listing 2-3. Sequence (String) Multiplication Example<br />
# Prints a sentence in a centered "box" of correct width<br />
# Note that the integer division opera<strong>to</strong>r (//) only works in <strong>Python</strong><br />
# 2.2 and newer. In earlier versions, simply use plain division (/)<br />
sentence = raw_input("Sentence: ")<br />
screen_width = 80<br />
text_width = len(sentence)<br />
box_width = text_width + 6<br />
left_margin = (screen_width - box_width) // 2<br />
print<br />
print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'<br />
print ' ' * left_margin + '| ' + ' ' * text_width + ' |'<br />
print ' ' * left_margin + '| ' + sentence + ' |'<br />
print ' ' * left_margin + '| ' + ' ' * text_width + ' |'<br />
print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'<br />