All the varieties of sequences (string
s,
tuple
s and list
s) have
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
that cover string
s, tuple
s
and list
s in detail.
Literal Values. Each sequence type has a literal representation. The details
will be covered in separate sections, but the basics are these:
string
s are quoted:
"string"
; tuple
s are in
()
's: (1,'b',3.1)
; list
s
are in []
's: [1,'b',3.1]
. A
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: +
will
concatenate sequences to make longer sequences. *
is used
with a number and a sequence to repeat the sequence several times.
Finally, the []
operator is used to select elements from
a sequence.
The [ ] operator can extract a single item, or a subset of items
by slicing. There are two forms of []
. The single item
format is
sequence
[
index
]
. Items are numbered from 0.
The slice format is
sequence
[
start
:
end
]
. Items from
start
to
end
-1 are chosen to create a new sequence as
a slice of the original sequence; there will be
end
−
start
items in the
resulting sequence.
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 tuple
(3.14159,"two words",2048,(1+2j))
.
forward position |
0 |
1 |
2 |
3 |
reverse position |
-4 |
-3 |
-2 |
-1 |
item |
3.14159 |
"two words" |
2048 |
(1+2j) |
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
the formula S[ len(S)-1 ]
. For example, if we have a
sequence with 4 elements, the last item is in position 3. Rather than
write S[ len(S)-1 ]
, Python lets us simplify this to
S[-1]
.
You can see how this works with the following example.
>>>
a=(3.14159,"two words",2048,(1+2j))
>>>
a[0]
3.1415899999999999
>>>
a[-3]
'two words'
>>>
a[2]
2048
>>>
a[-1]
(1+2j)
Built-in Functions.
len
, max
and
min
apply to all varieties of sequences.
len
returns the length of the sequence.
len("Wednesday")
is 9. max
returns
the largest value in the sequence. max( (1,2,3) )
is 3.
min
, analogously, returns the smallest value in
the sequence. 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
string
s being sorted alphabetically, and
tuple
s and list
s sorted in a way
that is similar to string
s.
There are two additional comparisons:
in
and
not in
. These check to see if a single value occurs
in the sequence. The
in
operator returns a
True
if the item is found, False
if the item is not found. The
not in
operator returns
True
if the item is not found in the sequence.
Methods. The string
and
list
classes have method functions that operate
on the object's value. For instance "abc".upper()
executes the upper
method belonging to the
string
literal "abc"
. The
result is 'ABC'
. The exact dictionary of methods is
unique to each class of sequences.
Statements.
tuple
s and list
s
are central to certain Python statements, like the
assignment
statement and the
for
statement. These were details that we skipped over in the section called “The
Assignment
Statement” and the section called “Iterative Processing: For All and There Exists”.
The additional 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.
-
repr
(
object
) →
string
-
Return the canonical string
representation of the object. For most object types,
eval(repr(object)) == object
.
-
str
(
object
) →
string
-
Return a nice string
representation
of the object. If the argument is a string
,
the return value is the same object.
-
list
(
sequence
) →
list
-
Return a new list
whose items are the
same as those of the argument sequence.
-
tuple
(
sequence
) →
tuple
-
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
object.