The next four types described
(str, unicode,
list and
tuple) are collectively referred to
as sequence types.
All of them represent an ordered set in the mathematical sense, that is, a collection of things in a specific order.
A value of str (8-bit
string) or
unicode (Unicode string) type
represents a sequence of zero or more characters. Python
strings are immutable or
atomic, that is, their contents cannot be modified in
place.
A list is a sequence of zero
or more values of any type (or combination of types).
Lists are mutable,
meaning that you can make changes to them by deleting,
inserting, or replacing elements.
A tuple is also a sequence
of zero or more values of any type, but it is
immutable: once you have formed a tuple, you cannot
delete, insert, or replace any of its
elements.
All four of the basic sequence types
(str,
unicode,
list, and
tuple) share a common set of
functions. In the table below,
means any sequence:S
len( | The number of elements in . |
max( | The largest element of . |
min( | The smallest element of . |
| True if any members of
are equal to
. A predicate function,
like the “>=” and other
comparison operators.. |
| True if none of the elements of
are equal to
. |
| Concatenate two sequences. |
tuple( | Convert the members of to a tuple. |
list( | Convert S to a list. |
| A new sequence containing copies of the contents of
.
For example,
[0]*5 is a list of five
zeroes. |
| Subscripting:
Refers to element
of sequence , counting
from zero. For example, "abcd"[2]
is "c".
A negative index value is counted from the end
back toward the front. Element -1 refers to the last
element, -2 to the next-to-last, and so on, so
"abcdef"[-1] is
"f". |
| Retrieve another sequence that is a
slice of sequence
starting at element
(counting from zero) and
going up to but not including
element .
For example,
"abcdef"[2:4] is
"cd". You can
omit to
start the slice at the beginning of ;
you can omit to take
a slice all the way to the end of . For example,
"abcdef"[:3] is
"abc", and
"abcdef"[3:] is
"def". See the diagram below.
You can even
omit both parts;
gives you a new sequence that is a copy of the old
one. |
In the diagram above, if string s
is "abcdef", slice
s[2:5] is "cde",
slice s[:3] is
"abc", and
s[3:] is
"def".