Lists#

Variables in Python are objects which we can use to store data and give a name to. Such as this:

word = "Hello"

We can use the print() function to show the content of a variable:

print(word)
Hello

Variables can also contain multiple entries of values. We call those lists and tuples. Some programmers also call them vectors or arrays; arrays of values. We already know one kind of array, strings. Strings are lists of characters.

See also

You can access elements in an array using square brackets [] which allow you access an element at a given index. Indexing starts at 0. Thus, the first element of an array is element number 0. The following string contains 5 characters and thus, element with index 0, 1, 2, 3 and 4 can be accessed:

word[0]
'H'
word[1]
'e'
word[2]
'l'
word[3]
'l'
word[4]
'o'

When accessing an index that is not in the list, we receive an error:

word[5]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 word[5]

IndexError: string index out of range

Numeric lists#

Another type of array are numeric lists. They are common to store measurements of experiments for example:

measurements = [5.5, 6.3, 7.2, 8.0, 8.8]
measurements[0]
5.5
measurements[1]
6.3

Changing entries in lists works like this:

measurements[1] = 25
measurements
[5.5, 25, 7.2, 8.0, 8.8]

You can also append entries to lists:

measurements.append(10.2)
measurements
[5.5, 25, 7.2, 8.0, 8.8, 10.2]

Lists can also be reversed:

measurements.reverse()
measurements
[10.2, 8.8, 8.0, 7.2, 25, 5.5]

Just like strings, you can also concatenate arrays:

more_measurements = [12.3, 14.5, 28.3]
measurements + more_measurements
[10.2, 8.8, 8.0, 7.2, 25, 5.5, 12.3, 14.5, 28.3]

Multipyling a list with an (integer) number will append the list to itself:

measurements * 2
[10.2, 8.8, 8.0, 7.2, 25, 5.5, 10.2, 8.8, 8.0, 7.2, 25, 5.5]

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)
5.5
# maximum value in the list
max(measurements)
25
# sum of all elements in the list
sum(measurements)
64.7
# number of elements in the list
len(measurements)
6
# average of all elements in the list
sum(measurements) / len(measurements)
10.783333333333333

Mixed type lists#

You 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[0])
int
type(mixed_list[3])
str

Tuples#

Tuples are lists which cannot be changed:

immutable = (4, 3, 7.8)
immutable[1]
3
immutable[1] = 5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [36], in <cell line: 1>()
----> 1 immutable[1] = 5

TypeError: 'tuple' object does not support item assignment

You can convert tubles to lists and lists to tuples:

type(immutable)
tuple
mutable = list(immutable)
type(mutable)
list
again_immuntable = tuple(mutable)
type(again_immuntable)
tuple

Exercise#

Take the immutable tuple above and turn it into a list. Append the number 24 to the list and turn the result into a tuple.