BAA1118 - Foundations of Programming for Analytics & AI
int, float, str, boolThis works for 3 months. It does not work for 3,000 customers.
Collections solve this: one name, many values.
Square brackets, values separated by commas.
A list can hold anything, even a mix of types, but a useful list holds one kind of thing.
['Ireland', 'France', 'Spain', 'Italy']
['Ireland', 'Germany', 'France', 'Spain', 'Italy']
This is the big difference with strings.
Important
list.sort() sorts in place and returns None. sorted(list) returns a new sorted list. Writing scores = scores.sort() destroys your data.
basket with these prices: 4.50, 12.99, 3.25, 8.0019.99 at the end3.25basket| Value | Ireland | France | Spain | Italy | Portugal |
|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 |
| From the end | -5 | -4 | -3 | -2 | -1 |
list[start:stop:step] returns a new list, from start up to but not including stop.
['France', 'Spain', 'Italy']
['Ireland', 'France']
['Italy', 'Portugal']
Leaving a side empty means “all the way”. [::-1] works on strings too.
Lists are mutable: an element can be replaced in place.
[99, 2, 3]
b = a gives the same list a second name. To copy:
Warning
This trap reappears in week 7 with DataFrames, where pandas warns you about it explicitly.
152 and print the new annual total[120, 135, 148] 403
[190, 205, 240] 635
[120, 160, 171, 190]
[240, 205, 190, 180, 168, 171, 149, 155, 160, 148, 135, 120]
2024
What is customer[2]? You have to remember. In six months, you will not.
A dictionary stores values by name instead of by position.
Curly braces, unique keys, and values of any type, even lists. Think of it as a single row of a spreadsheet with its column headers.
Damien
dict_keys(['name', 'city', 'spend'])
dict_values(['Damien', 'Dublin', 1400.0])
Round brackets instead of square ones.
Use a tuple when the collection is not supposed to change: a coordinate, a date, a pair of values returned by a function.
This works for lists too, and is used constantly:
You will see it in week 9 as fig, ax = plt.subplots().
{'South', 'East', 'North'}
3
Sets answer “which distinct values are in this column?”, and compare groups with no library at all:
| Syntax | Ordered | Changeable | Duplicates | |
|---|---|---|---|---|
| list | [1, 2, 3] |
yes | yes | yes |
| tuple | (1, 2, 3) |
yes | no | yes |
| dict | {"a": 1} |
yes | yes | keys: no |
| set | {1, 2, 3} |
no | yes | no |
In practice: list for a column of values, dict for a labelled record, set for uniqueness, tuple for fixed pairs.
customer with keys id, name, city and orders, where orders is a list of order amounts: 45.0, 120.5, 33.2578.0 to the list inside the dictionaryemail that does not exist4 276.75
Average order: 69.19
no email on file
This is how almost all real data arrives, from an API, a JSON file, or a database.
[{'name': 'Damien', 'city': 'Dublin', 'spend': 1250.5},
{'name': 'Aoife', 'city': 'Cork', 'spend': 890.0},
{'name': 'Liam', 'city': 'Dublin', 'spend': 2100.75}]
Turn your head sideways: names across the top, one record per row. This is a table.
Each row is one observation: one product, one customer, one country-year.

Each column is one variable: one thing measured about every observation.

Source: UBC Master of Data Science, Programming in Python for Data Science
sales = [
{"month": "Jan", "region": "North", "units": 120},
{"month": "Jan", "region": "South", "units": 95},
{"month": "Feb", "region": "North", "units": 138},
{"month": "Feb", "region": "South", "units": 101}
]
print(len(sales))
print(sales[2]["region"])
print(sales[-1]["units"])
sales.append({"month": "Mar", "region": "North", "units": 150})
print(sales[0]["units"] + sales[1]["units"])4
North
101
215
Adding up the whole column still needs a loop. That is next week.
Everything so far has been typed into the notebook. Real data arrives as a file, and you will meet four formats over and over.
| Format | Looks like | Holds |
|---|---|---|
.csv |
text, one row per line | one table |
.json |
text, nested brackets | anything, even nested records |
.xlsx |
an Excel document | sheets, formatting, formulas |
.parquet |
a binary data file | one table, typed and compressed |
Today you only need to recognise them and know what shape of Python each one maps onto. Reading them comes in week 5.
Comma Separated Values. Open it in a text editor and you can read it:
roasted_stores.csv
42 is the text “42” until something decides otherwiseThat is a list of dictionaries written down. It is the format you should ask for.
a_real_export.csv
This is why you never split a CSV by hand with .split(","). A proper reader handles all of it, and you will use one from week 5.
JavaScript Object Notation. Look closely at this:
customers.json
You wrote that ten minutes ago. JSON is a list of dictionaries, saved as text. Square brackets for a list, curly braces for a dictionary, colons for keys.
The json module turns one into the other. Imports come in week 5, so read this rather than worrying about the first line:
<class 'list'>
<class 'dict'>
61.4
Almost every website and app sends its data as JSON, precisely because it can hold nested structures that a table cannot: a customer with a list of orders inside them.
An .xlsx file is a zipped folder of XML. It can hold:
Warning
Excel is excellent for people and awkward for code. When you are given one, expect a title row above the headers, a blank column somebody used as a spacer, and totals in the middle of the data. Half of week 7 is dealing with exactly that.
It is the standard for data that is passed between systems rather than emailed to a person. You will meet it in any serious data job, and almost never in an inbox.

Thanks for your attention and don’t hesitate to ask if you have any questions!