Mathematically, a sequence in Python represents an ordered set.
Sequences are an example of container classes: values that contain other values inside them.
| Type name | Contains | Examples | Mutable? |
|---|---|---|---|
str | 8-bit characters |
"abc" 'abc' "" '' '\n' '\x00'
| No |
unicode | 32-bit characters |
u'abc' u'\u000c'
| No |
list | Any values |
[23, "Ruth", 69.8] []
| Yes |
tuple | Any values |
(23, "Ruth", 69.8) () (44,)
| No |
str and unicode are used
to hold text, that is, strings of characters.
list and tuple are used
for sequences of zero or more values of any type. Use
a list if the contents of the sequence
may change; use a tuple if the contents
will not change, and in certain places where tuples are
required. For example, the right-hand argument of the
string format operator (see Section 3.4, “The string format method”) must be a tuple if you are formatting more than one
value.
To create a list, use an expression of the form
[expr1,expr1, ... ]
with a list of zero or more values between square brackets, “[…]”.
To create a tuple, use an expression of the form
(expr1,expr1, ... )
with a list of zero or more values enclosed in
parentheses, “(…”).
To create a tuple with only one element , use the
special syntax “v(”. For example,
v,)(43+1,) is a one-element tuple containing
the integer 44. The trailing comma is used to
distinguish this case from the expression “(43+1)”, which yields the integer 44,
not a tuple.
Mutability: You can't change
part of an immutable value. For
example, you can't change the first character of a
string from 'a' to 'b'.
It is, however, easy to build a new string out of
pieces of other strings.
Here are some calculator-mode examples. First, we'll
create a string named s, a list named L, and a tuple named t:
>>> s = "abcde"
>>> L = [0, 1, 2, 3, 4, 5]
>>> t = ('x', 'y')
>>> s
'abcde'
>>> L
[0, 1, 2, 3, 4, 5]
>>> t
('x', 'y')
The built-in function len( returns the number of elements
in a sequence S).
S
>>> print len(s), len(L), len(t) 5 6 2
Function max( returns the largest value in a sequence S), and function
Smin(
returns the smallest value in a sequence S).
S
>>> max(L) 5 >>> min(L) 0 >>> max(s) 'e' >>> min(s) 'a'
To test for set membership, use the “in” operator. For a value and a sequence
v, the
expression S returns the
Boolean value v in
STrue if there is at least
one element of that equals S; it returns vFalse otherwise.
>>> 2 in L True >>> 77 in L False
There is an inverse operator, , that returns v not in STrue if does not equal
any element of v, SFalse otherwise.
>>> 2 not in L False >>> 77 not in L True
The “+” operator is used to
concatenate two sequences of the same type.
>>> s + "xyz"
'abcdexyz'
>>> L + L
[0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
>>> t + ('z',)
('x', 'y', 'z')
When the “*” operator occurs
between a sequence and an integer S, you get a new sequence
containing n
repetitions of the elements of n.
S
>>> "x" * 5 'xxxxx' >>> "spam" * 8 'spamspamspamspamspamspamspamspam' >>> [0, 1] * 3 [0, 1, 0, 1, 0, 1]