Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 2 ■ ■ ■ Lists and Tuples This chapter introduces a new concept: data structures. A data structure is a collection of data elements (such as numbers or characters—or even other data structures) that is structured in some way, for example, by numbering the elements. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number—its position, or index. The first index is zero, the second index is one, and so forth. ■Note When you count or number things in your daily life, you probably start counting from 1. The numbering scheme used in Python may seem odd, but it is actually quite natural. One of the reasons for this, as you see later in the chapter, is that you can also count from the end: The last item of a sequence is numbered –1, the next-to-last –2, and so forth. That means you can count forward or backward from the first element, which lies at the beginning, or zero. Trust me, you get used to it. Python has six built-in types of sequences, but let’s concentrate on two of the most common ones—lists and tuples. The main difference between these is that you can change a list, but you can’t change a tuple. This means a list might be useful if you need to add elements as you go along, while a tuple can be useful if, for some reason, you can’t allow the sequence to change. Reasons for the latter are usually rather technical, having to do with how things work internally in Python. That’s why you may see built-in functions returning tuples. For your own programs, chances are you can use lists instead of tuples in almost all circumstances. (One notable exception, as described in Chapter 4, is using tuples as dictionary keys. There lists aren’t allowed, because you aren’t allowed to modify keys.) ■Note The other built-in sequence types are strings (which I revisit in the next chapter), Unicode strings, buffer objects, and xrange objects. 31
32 CHAPTER 2 ■ LISTS AND TUPLES Sequences are useful when you want to work with a collection of values. You might have a sequence representing a person in a database, with the first element being their name, and the second their age. Written as a list (the items of a list are separated by commas and enclosed in square brackets), that would look like this: >>> edward = ['Edward Gumby', 42] But sequences can contain other sequences, too, so you could make a list of such persons, which would be your database: >>> edward = ['Edward Gumby', 42] >>> john = ['John Smith', 50] >>> database = [edward, john] >>> database [['Edward Gumby', 42], ['John Smith', 50]] This chapter begins with some operations that are common to all sequences, including lists and tuples. These operations will also work with strings, which will be used in some of the examples, although for a full treatment of string operations, you have to wait until the next chapter. After dealing with these basics, we start working with lists and see what’s special about them. After lists, we come to tuples, which are very similar to lists, except that you can’t change them. ■Note Python has a basic notion of a kind of data structure called a container, which is basically any object that can contain other objects. The two main kinds of containers are sequences (such as lists and tuples) and mappings (such as dictionaries). While the elements of a sequence are numbered, each element in a mapping has a name (also called a key). You learn more about mappings in Chapter 4. For an example of a container type that is neither a sequence nor a mapping, see the discussion of sets in Chapter 10. Common Sequence Operations There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence, and for finding its largest and smallest elements. ■Note One important operation not covered here is iteration. To iterate over a sequence means to perform certain actions repeatedly, once per element in the sequence. To learn more about this, see the section “Loops” in Chapter 5.
- Page 11 and 12: x ■CONTENTS ■CHAPTER 5 Conditio
- Page 13 and 14: xii ■CONTENTS ■CHAPTER 8 Except
- Page 15 and 16: xiv ■CONTENTS ■CHAPTER 11 Files
- 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 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 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
CHAPTER 2<br />
■ ■ ■<br />
Lists and Tuples<br />
This chapter introduces a new concept: data structures. A data structure is a collection of data<br />
elements (such as numbers or characters—or even other data structures) that is structured in<br />
some way, for example, by numbering the elements. The most basic data structure in <strong>Python</strong> is<br />
the sequence. Each element of a sequence is assigned a number—its position, or index. The first<br />
index is zero, the second index is one, and so forth.<br />
■Note When you count or number things in your daily life, you probably start counting from 1. The numbering<br />
scheme used in <strong>Python</strong> may seem odd, but it is actually quite natural. One of the reasons for this, as you see<br />
later in the chapter, is that you can also count from the end: The last item of a sequence is numbered –1, the<br />
next-<strong>to</strong>-last –2, and so forth. That means you can count forward or backward from the first element, which<br />
lies at the beginning, or zero. Trust me, you get used <strong>to</strong> it.<br />
<strong>Python</strong> has six built-in types of sequences, but let’s concentrate on two of the most common<br />
ones—lists and tuples. The main difference between these is that you can change a list, but you<br />
can’t change a tuple. This means a list might be useful if you need <strong>to</strong> add elements as you go<br />
along, while a tuple can be useful if, for some reason, you can’t allow the sequence <strong>to</strong> change.<br />
Reasons for the latter are usually rather technical, having <strong>to</strong> do with how things work internally<br />
in <strong>Python</strong>. That’s why you may see built-in functions returning tuples. For your own programs,<br />
chances are you can use lists instead of tuples in almost all circumstances. (One notable exception,<br />
as described in Chapter 4, is using tuples as dictionary keys. There lists aren’t allowed,<br />
because you aren’t allowed <strong>to</strong> modify keys.)<br />
■Note The other built-in sequence types are strings (which I revisit in the next chapter), Unicode strings,<br />
buffer objects, and xrange objects.<br />
31