Prerequisites
- Types
Collections
Previously, we were introduced to individual types that exist in Python. Python types go beyond individual items, including objects that can hold collections of variables. This section will introduce three different collection types:
- Lists
- Tuples
- Dictionaries
As is the case with individual types, different collection types can be acted on in different ways.
noble_gases = ['helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon']
type(noble_gases)
print(noble_gases)
We can use the index notation to identify particular elements in a list. However, it is important to note that this notation counts from $0$, so the first element of the list is found with,
print(noble_gases[0])
The index notation is shown for a list of the first 7 elements in the figure below.
We can also change particular values, as lists are mutable.
noble_gases[0] = 'Helium'
The final element in the tuple therefore has the index of $n-1$, where $n$ is the tuple length.
The lengths of a collection can be found with the len()
function.
len(noble_gases)
print(noble_gases[len(noble_gases)-1])
You may notice that Oganesson is missing from the list, it is possible to concatenate it.
noble_gases = noble_gases + ['Oganesson']
print(noble_gases)
In addition to standard indexing, it is also possible to perform negative indexing with lists and tuples, where $-1$ is the final element of the list with the counting up moving backwords through the list.
print(noble_gases[-1])
We can also get slices of a list, this is achieved using the :
notation.
print(noble_gases[1:3])
This notation can be a bit confusing, as this notation is inclusive then exclusive.
The slice of the list includes the element with the index that preceeds the colon and exclusive of that after the colon.
In the example above, we can see that is includes the index $1$ item ('Neon'
) but excludes the index $3$ element ('Krypton'
).
This is shown for the elements
list in the figure below.
It is also possible to use list slicing to skip elements of the list, where the number after the second colon is the step size.
print(noble_gases[1:5:2])
Above, the second to the sixth elements are printed skipping each second (so those with indices $2$ and $4$).
As well as list concatentation (the adding together of lists above), it is possible to append
single elements to the end of a list.
halogens = ['Fluorine', 'Chlorine', 'Bromine', 'Iodine']
print(halogens)
halogens.append('Astanine')
print(halogens)
halogens.append('Tennessine')
print(halogens)
Finally, we note that a list does not have to be of a consistent type
.
So it is possble for a list to be made up of disparate forms of information.
chlorine = ['Cl', 17, 35.45]
print(chlorine)
Where the list chlorine
contains information about the chemical symbol, atomic number, and the average mass number.
noble_gases = ('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon')
Above is a tuple of noble gases, where the tuple is defined using round brackets.
type(noble_gases)
Like with lists, we can use the index notation to identify particular elements in the tuple.
print(noble_gases[0])
print(noble_gases[-1])
Above it was mentioned that a tuple is an immutable type, that once created it's objects cannot be altered. We can see this in action if we try and change the value of one of the elements in the tuple.
noble_gases[0] = 'Helium'
However, it is still possible to concatenate tuples, like lists, but the item that is being concatentated must be a tuple, hence the brackets and trailing comma.
noble_gases + ('oganesson',)
The immutability of a tuple does not mean that the whole variable cannot be overwritten.
noble_gases = 'inert'
print(noble_gases)
Additionally, like lists, tuple items can also consist of data of different types.
chlorine = {'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45}
print(chlorine)
Here, we are able to use the keys
of the dictionaries as labels for each datum.
print(chlorine.keys())
It is possible to access a single one of the elements of a dictionary, by passing the relevant key
in lieu of an index.
chlorine['atomic number']
However, traditional indexing does not work for dictionaries, as the index is not a key
.
chlorine[1]
We can add elements to a dictionary rather simply.
chlorine['number of isotopes'] = 2
print(chlorine)
Finally, the items in a dictionary need not be single variables, like a float or an int.
They can also be a list
or tuple
.
chlorine['mass numbers'] = (35, 37)
print(chlorine)
d_block = {}