Sequences in Python#
Variables can contain multiple values, those are called sequences. One example we already introduced is a string-typed variable, containing an immutable ordered collection of Unicode items.
The other three basic sequence types are:
Lists#
Lists are mutable collections of heterogeneous values, which means they can be changed. They are also called vectors or arrays.
measurements = [3.5, 1.5, 4.6, 5.7, 2.4]
type(measurements)
list
Access via index#
Entries can be accessed by their index in the collection via the bracket operator []
. Indices in such collections start at 0
:
measurements[0]
3.5
measurements[1]
1.5
Entries can be changed (lists are mutable) by assigning a new value to an index:
measurements[1] = 8.5
measurements[1]
8.5
List methods and operations#
The list object provides several methods to work with the underlying data
We can append entries to lists:
measurements.append(10.2)
We can reverse lists:
measurements.reverse()
measurements
[10.2, 2.4, 5.7, 4.6, 8.5, 3.5]
And we can sort lists:
measurements.sort()
measurements
[2.4, 3.5, 4.6, 5.7, 8.5, 10.2]
Like strings, we can also concatenate lists:
more_measurements = [6.1, 8.9, 1.3]
measurements + more_measurements
[2.4, 3.5, 4.6, 5.7, 8.5, 10.2, 6.1, 8.9, 1.3]
When working with numeric lists, you can use some of python’s built-in functions to do basic statistics on your measurements:
# minimum value in the list
min(measurements)
2.4
# maximum value in the list
max(measurements)
10.2
# sum of all elements in the list
sum(measurements)
34.9
# number of elements in the list
len(measurements)
6
# average of all elements in the list
sum(measurements) / len(measurements)
5.816666666666666
Lists of mixed types#
We can also store values of different types in a list
mixed_list = [22, 5.6, "Cat", 'Dog']
mixed_list[0]
22
mixed_list[3]
'Dog'
type(mixed_list[3])
str
Tuples#
Tuples are immutable collections of heterogeneous values, which means they cannot be changed.
immutable = (4, 3, 2 ,1, "Dog")
type(immutable)
tuple
immutable[0]
4
immutable[0] = 5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[22], line 1
----> 1 immutable[0] = 5
TypeError: 'tuple' object does not support item assignment
You can convert tuples to lists and vice versa:
mutable = list(immutable)
type(mutable)
list
immutable_again = tuple(mutable)
type(immutable_again)
tuple
Ranges#
Ranges are immutable collections of numbers, commonly used for counting in loops (later more). Ranges can be constructed via [start:end:step]
, where end
is not inclusive, and step
is an optional parameter.
numbers = range(1, 11)
type(numbers)
range
numbers[0]
1
list(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Exercise#
Assume we have measurements on multiple days. Please compute the average measurement of this week.
measurements_monday = [2.3, 3.1, 5.6]
measurements_tuesday = [1.8, 7.0]
measurements_wednesday = [4.5, 1.5, 6.4, 3.2]
measurements_thursday = [1.9, 2.0]
measurements_friday = [4.4]