0% found this document useful (0 votes)
46 views

Introduction To Computation and Programming Using Python, Revised - Guttag, John v..83

The document compares and contrasts the common sequence types in Python - strings, tuples, and lists. It notes that while they share common operations like indexing, length checking, concatenation and slicing, strings can only contain characters while tuples and lists can contain any data type. Tuples are immutable while lists are mutable, making lists more commonly used but tuples suitable for dictionary keys. It provides examples of constructing each type and using lists to incrementally build new lists.

Uploaded by

ZhichaoWang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Introduction To Computation and Programming Using Python, Revised - Guttag, John v..83

The document compares and contrasts the common sequence types in Python - strings, tuples, and lists. It notes that while they share common operations like indexing, length checking, concatenation and slicing, strings can only contain characters while tuples and lists can contain any data type. Tuples are immutable while lists are mutable, making lists more commonly used but tuples suitable for dictionary keys. It provides examples of constructing each type and using lists to incrementally build new lists.

Uploaded by

ZhichaoWang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

66

5.4

Chapter 5. Structured Types, Mutability, and Higher-Order Functions

Strings, Tuples, and Lists


We have looked at three different sequence types: str, tuple, and list. They
are similar in that objects of all of these types can be operated upon as
described in Figure 5.6.
seq[i] returns the ith element in the sequence.
len(seq) returns the length of the sequence.
seq1 + seq2 returns the concatenation of the two sequences.
n * seq returns a sequence that repeats seq n times.
seq[start:end] returns a slice of the sequence.
e in seq is True if e is contained in the sequence and False otherwise.
e not in seq is True if e is not in the sequence and False otherwise.
for e in seq iterates over the elements of the sequence.

Figure 5.6 Common operations on sequence types


Some of their other similarities and differences are summarized in Figure 5.7.
Type

Type of elements

Examples of literals

Mutable

str

characters

'', 'a', 'abc'

No

tuple

any type

(), (3,), ('abc', 4)

No

list

any type

[], [3], ['abc', 4]

Yes

Figure 5.7 Comparison of sequence types


Python programmers tend to use lists far more often than tuples. Since lists are
mutable, they can be constructed incrementally during a computation.
For example, the following code incrementally builds a list containing all of the
even numbers in another list.
evenElems = []
for e in L:
if e%2 == 0:
evenElems.append(e)

One advantage of tuples is that because they are immutable, aliasing is never a
worry. Another advantage of their being immutable is that tuples, unlike lists,
can be used as keys in dictionaries, as we will see in the next section.
Since strings can contain only characters, they are considerably less versatile
than tuples or lists. On the other hand, when you are working with a string of
characters there are many built-in methods that make life easy. Figure 5.8
contains short descriptions of a few of them. Keep in mind that since strings are
immutable these all return values and have no side effect.

You might also like