Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 2 ■ LISTS AND TUPLES 51 So What’s the Point? By now you are probably wondering why anyone would ever want such a thing as an immutable (unchangeable) sequence. Can’t you just stick to lists and leave them alone when you don’t want them to change? Basically, yes. However, there are two important reasons why you need to know about tuples: • They can be used as keys in mappings—lists can’t be. (You may remember that mappings were mentioned in the chapter introduction. You’ll learn more about them in Chapter 4.) • They are returned by some built-in functions and methods, which means that you have to deal with them. As long as you don’t try to change them, “dealing” with them most often means treating them just like lists (unless you need methods such as index and count, which tuples don’t have). In general, lists will probably be adequate for all your sequencing needs. A Quick Summary Let’s review some of the most important concepts covered in this chapter: Sequences. A sequence is a data structure in which the elements are numbered (starting with zero). Examples of sequence types are lists, strings, and tuples. Of these, lists are mutable (you can change them), whereas tuples and strings are immutable (once they’re created, they’re fixed). Parts of a sequence can be accessed through slicing, supplying two indices, indicating the starting and ending position of the slice. To change a list, you assign new values to its positions, or use assignment to overwrite entire slices. Membership. Whether a value can be found in a sequence (or other container) is checked with the operator in. Using in with strings is a special case—it will let you look for substrings. Methods. Some of the built-in types (such as lists and strings, but not tuples) have many useful methods attached to them. These are a bit like functions, except that they are tied closely to a specific value. Methods are an important aspect of object-oriented programming, which we look at later, in Chapter 7.
52 CHAPTER 2 ■ LISTS AND TUPLES New Functions in This Chapter Function cmp(x, y) len(seq) list(seq) max(args) min(args) reversed(seq) sorted(seq) tuple(seq) Description Compares two values Returns the length of a sequence Converts a sequence to a list Returns the maximum of a sequence or set of arguments Returns the minimum of a sequence or set of arguments Lets you iterate over a sequence in reverse Returns a sorted list of the elements of seq Converts a sequence to a tuple What Now? Now that you’re acquainted with sequences, let’s move on to character sequences, also known as strings.
- 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 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 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 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
CHAPTER 2 ■ LISTS AND TUPLES 51<br />
So What’s the Point?<br />
By now you are probably wondering why anyone would ever want such a thing as an immutable<br />
(unchangeable) sequence. Can’t you just stick <strong>to</strong> lists and leave them alone when you don’t<br />
want them <strong>to</strong> change? Basically, yes. However, there are two important reasons why you need<br />
<strong>to</strong> know about tuples:<br />
• They can be used as keys in mappings—lists can’t be. (You may remember that mappings<br />
were mentioned in the chapter introduction. You’ll learn more about them in Chapter 4.)<br />
• They are returned by some built-in functions and methods, which means that you have<br />
<strong>to</strong> deal with them. As long as you don’t try <strong>to</strong> change them, “dealing” with them most<br />
often means treating them just like lists (unless you need methods such as index and<br />
count, which tuples don’t have).<br />
In general, lists will probably be adequate for all your sequencing needs.<br />
A Quick Summary<br />
Let’s review some of the most important concepts covered in this chapter:<br />
Sequences. A sequence is a data structure in which the elements are numbered (starting<br />
with zero). Examples of sequence types are lists, strings, and tuples. Of these, lists are<br />
mutable (you can change them), whereas tuples and strings are immutable (once they’re<br />
created, they’re fixed). Parts of a sequence can be accessed through slicing, supplying two<br />
indices, indicating the starting and ending position of the slice. To change a list, you assign<br />
new values <strong>to</strong> its positions, or use assignment <strong>to</strong> overwrite entire slices.<br />
Membership. Whether a value can be found in a sequence (or other container) is checked<br />
with the opera<strong>to</strong>r in. Using in with strings is a special case—it will let you look for substrings.<br />
Methods. Some of the built-in types (such as lists and strings, but not tuples) have many<br />
useful methods attached <strong>to</strong> them. These are a bit like functions, except that they are tied<br />
closely <strong>to</strong> a specific value. Methods are an important aspect of object-oriented programming,<br />
which we look at later, in Chapter 7.