The Python programming language
|Python Data Types|
A sequence is any object or data structure that stores and accesses elements in a fixed order. Built-in sequence types include
range. By contrast, sets and dictionaries, while they share some common features with sequences, are not themselves sequences because they are unordered.
Sequences always support iteration (though not all iterable types are necessarily sequences) and a set of other basic operations.
- 1 Types
- 2 Operations
- 3 Mutable sequences
- 4 Copying techniques
A string (
str) is a sequence whose elements are characters (letters, symbols, or spaces). A string is surrounded by single quotes, double quotes, or a trio of double quotes:
str = 'test' str = "test" str = """test"""
tuple is a sequence that stores elements of any type. A tuple is created by enclosing comma-separated values in parentheses:
tup = (1, 2, 3)
tup = (1,) # (1) would be interpreted as an integer
Alternatively, a tuple can be constructed by calling
tuple(s) for some sequence
list also stores elements of any type. Lists behave much like tuples, except lists are mutable (see below). A list is created by enclosing comma-separated values in brackets:
lst = [1, 2, 3]
list(s)for some sequence
s. In addition a list can be created by a list comprehension.
A range is a sequence that contains integers in a specified interval. A range object is constructed by
range(start, end, size), which creates a sequence containing the integers from
end - 1, with a step size of
size. The third argument is optional. If the first and third arguments are not provided, the start value defaults to 0.
The number of elements in a sequence
s is given by
Indices and accessing
Elements in a sequence are organized by index, an integer denoting where that element is located in the ordering. The first element is at index 0, the second and index 1, and so on. This convention is called zero-based indexing. Note that Python also allows negative indices for convenience. The last element is at -1, the second to last at -2, and so on.
A particular element
x in a sequence
s can be retrieved with
i is the index of
x. Note that this returns, but does not remove, the element
x. Negative indices are permissible. If
i is out of the bounds of the sequence, Python will throw an error.
Indices are also used to return a slice of the sequence. For a sequence
s, create a slice with the syntax
s[start:end:step]. This will return a new sequence that begins with the element at index
start, ends with the element at index
end - 1, counting indices by
step. If not specified
start defaults to 0,
end defaults to
step defaults to 1. Negative indices and steps (indicating counting indices backwards) are permissible. For example,
>>> s = "abababab" >>> s[:-2:2] "aaa"
If no indices overlap with the specified slice, an empty sequence is returned. Example:
>>> s = [1, 2, 3] >>> s[3:] 
Two sequences of the same type can be concatenated together with the addition operator, which returns a new sequence containing all the elements in the first sequence followed by all the elements in the second. For example,
(1, 2) + (3, 4) returns
(1, 2, 3, 4).
x in s returns
x is an element of
x not in s returns the inverse: returns
x is not an element of
s. For example:
>>> 1 in [1, 2, 3] True >>> 1 not in [1, 2, 3] False
not in act like a substring test. For example:
>>> 'abc' in 'abcde' True
s and integer
s * n returns
n shallow copies of
s concatenated. For example:
>>> 'hi ' * 3 'hi hi hi '
The only built-in mutable sequence type in Python is the
list. In addition to the common sequence operations, lists support internal modification.
An element at index
i of list
lst can be reassigned to
lst[i] = c. Example:
>>> lst = [1, 2, 3] >>> lst = 5 # replaces first element of lst >>> lst [5, 2, 3]
A slice of list
s can be reassigned to other values. Examples:
>>> lst = [1, 2, 3] >>> lst[0:2] = 5, 6 >>> lst [5, 6, 3] >>> lst[0:0] = 1, 2 >>> lst [1, 2, 5, 6, 3]
append takes in one argument and inserts it at the end of the list. Example:
>>> lst = [5, 2, 3] >>> lst.append(5) >>> lst [5, 2, 3, 5]
remove takes in one argument and removes the first instance of it from the list. Example:
>>> lst = [5, 2, 3, 5] >>> lst.remove(5) >>> lst [2, 3, 5]
pop removes and returns the last element if it is not passed any additional arguments, or removes and returns the element at index
>>> lst = [2, 3, 5] >>> lst.pop() 5 >>> lst [2, 3] >>> lst.pop(0) 2 >>> lst 
Other built-in methods that rely upon mutability include
sort which sorts the given list in-place (modifies the original object), and
reverse, which reverses a list in-place.
There are several methods to make a shallow copy of a sequence
constructoris the relevant constructor for the sequence
s + empty_sequence, where
empty_sequenceis the syntax for an empty version of the sequence
s * 1
>>> s = [1, 2, 3] >>> copy1 = s[:] >>> copy2 = list(s) >>> copy3 = s +  >>> copy4 = s * 1 >>> copy1 [1, 2, 3] >>> copy2 [1, 2, 3] >>> copy3 [1, 2, 3] >>> copy4 [1, 2, 3] >>> (copy1 is not s) and (copy2 is not s) and (copy3 is not s) and (copy4 is not s) True