Sequences
Sometimes we care about some sets of items, but the order matters.
- Bytes in a file
- Ranked sports team in a league
We use sequences for this job. These are often implemented as lists or arrays.
A sequence of length is a function for some set .
info
- Sequences are often notated like rather than
- Another notation could be
[1,5,7,1]
Sequences in Python: lists
my_list = [1,5,6]
my_list[2] = 12
print(my_list[2])
my_list.append(8)
my_list.pop()
len(my_list)
17 in my_list
Sequences in Python: tuples
see tuples.
Mutability
In Python, some objects can be changed, others are static.
- mutable types can change overtime:
lists
,dict
and,set
. - immutable types don't change:
tuple
,frozenset
,number
, andstring
Hashability
Hashable types can be identified by a hash value, used internally by some containers
- mutable types are not hashable
- immutable types are generally hashable
- immutable containers are hashable only if they contain only hashable elements.
info
Set elements and dictionary keys must be hashable.
- Set
- Dictionary
S = {[1]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
D = {["my_key"]:7}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'