Skip to content
Snippets Groups Projects

Corrections - Tom

1 file
+ 2
5
Compare changes
  • Side-by-side
  • Inline
+ 2
5
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
These are suitable to hold single values - but often, we want to create more complex datatypes that can hold more than one value.
These are suitable to hold single values - but often, we want to create more complex datatypes that can hold more than one value.
## Lists
## Lists
List are "containers" that store a sequence of elements.
Lists are "containers" that store a sequence of elements.
We can initialise a list with a sequence of elements or start with an empty list.
We can initialise a list with a sequence of elements or start with an empty list.
We can get the length of the list by using the function ```len()```
We can get the length of the list by using the function ```len()```
Dictionaries are a very common datatype in python that are typically used to store and look-up information.
Dictionaries are a very common datatype in python that are typically used to store and look-up information.
Each element of a dictionary has two elements: the "key" and the "value" that always come together, i.e. we have *key-value pairs*
Each element of a dictionary has two elements: the "key" and the "value" that always come together, i.e. we have *key-value pairs*
* key: Each key is associated with a value and we can use this key to access or change the information stored in the corresponding value. The keys can by any immutable data type (i.e. one we cannot change later, otherwise we could no longer establish the relationship "key - value" if we allowed the key to change.).
* key: Each key is associated with a value and we can use this key to access or change the information stored in the corresponding value. The keys can be any immutable data type (i.e. one we cannot change later, otherwise we could no longer establish the relationship "key - value" if we allowed the key to change.).
Each key needs to be unique, again, we could not establish a "key - value" relationship if we had the same key multiple times.
Each key needs to be unique, again, we could not establish a "key - value" relationship if we had the same key multiple times.
* value: This holds the content we want to associate with the key. The values can be of any python data type. We could have, for example, a simple string, a number - but also lists or even other dictionaries.
* value: This holds the content we want to associate with the key. The values can be of any python data type. We could have, for example, a simple string, a number - but also lists or even other dictionaries.
``` my_dict = { key_1 : value_1, key_2 : value_2, ...} ```
``` my_dict = { key_1 : value_1, key_2 : value_2, ...} ```
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
Loading