All the varieties of sequences (
some common characteristics. We'll identify the common features first,
and then move on to cover these in detail for each individual type of
sequence. This section is a road-map for the following three sections
lists in detail.
Literal Values. Each sequence type has a literal representation. The details
will be covered in separate sections, but the basics are these:
strings are quoted:
tuples are in
tuple or a
list is a sequences of
various types of items. A
string is a sequence
of characters only.
Operations. Sequences have three common operations:
concatenate sequences to make longer sequences.
* is used
with a number and a sequence to repeat the sequence several times.
 operator is used to select elements from
The [ ] operator can extract a single item, or a subset of items
by slicing. There are two forms of
. The single item
. Items are numbered from 0.
The slice format is
. Items from
-1 are chosen to create a new sequence as
a slice of the original sequence; there will be
items in the
Positions can be numbered from the end of the
string as well as the beginning. Position -1 is
the last item of the sequence, -2 is the next-to-last item. Here's how
it works: each item has a positive number position that identifies the
item in the sequence. We'll also show the negative position numbers for
each item in the sequence. For this example, we're looking at a
four-element sequence like the
Why do we have two different ways of identifying each position in
the sequence? If you want, you can think of it as a handy short-hand.
The last item in any sequence,
S can be identified by
S[ len(S)-1 ]. For example, if we have a
sequence with 4 elements, the last item is in position 3. Rather than
S[ len(S)-1 ], Python lets us simplify this to
You can see how this works with the following example.
min apply to all varieties of sequences.
len returns the length of the sequence.
len("Wednesday") is 9.
the largest value in the sequence.
max( (1,2,3) ) is 3.
min, analogously, returns the smallest value in
min( [19,9,99] ) is 9.
Comparisons. The standard comparisons (<, <=, >, <=, ==, !=)
apply to sequences. These all work by doing item-by-item comparison
within the two sequences. The item-by-item rule results in
strings being sorted alphabetically, and
lists sorted in a way
that is similar to
There are two additional comparisons:
. These check to see if a single value occurs
in the sequence. The
operator returns a
True if the item is found,
if the item is not found. The
True if the item is not found in the sequence.
list classes have method functions that operate
on the object's value. For instance
upper method belonging to the
'ABC'. The exact dictionary of methods is
unique to each class of sequences.
are central to certain Python statements, like the
statement and the
statement. These were details that we skipped over in the section called “The
Statement” and the section called “Iterative Processing: For All and There Exists”.
tuple-specific details of these statements
will be covered in Chapter 13, Tuples
Modules. There is a
string module with several
string-specific functions. Most of these
functions are now member functions of the
string type, except for a special-purpose
function used to create translation tables. Additionally, this module
has a number of constants to define various subsets of the ASCII
character set, including digits, printable characters, whitespace
characters and others.
Factory Functions. There are also built-in factory (or conversion) functions for
the sequence objects.
Return the canonical
representation of the object. For most object types,
eval(repr(object)) == object.
Return a nice
of the object. If the argument is a
the return value is the same object.
Return a new
list whose items are the
same as those of the argument sequence.
Return a new
tuple whose items are
the same as those of the argument sequence. If the argument is a
tuple, the return value is the same