Mathematically, a *sequence* in
Python represents an ordered set.

Sequences are an example
of *container classes*: values that
contain other values inside them.

*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.

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, "Fred", 69.8] []`
| Yes |

`tuple` | Any values |
`(23, "Fred", 69.8) () (44,)`
| No |

`int`

and`unicode`

are used for strings.`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.To create a list, use an expression of the form

[

,`expr`

_{1}, ... ]`expr`

_{1}with a list of zero or more values between

*square brackets*, “`[…]`

”.To create a tuple, use an expression of the form

(

,`expr`

_{1}, ... )`expr`

_{1}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.

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*)

`S`

, and function
`min(``S`

)

returns the smallest value in a sequence `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

`S`

`True`

if there is at least
one element of `S`

that equals `v`

; it returns `False`

otherwise.
>>> 2 in L True >>> 77 in L False

There is an inverse operator,

, that returns * v* not in

`S`

`True`

if `v`

does not equal
any element of `S`

, `False`

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]