Two kinds of trip data
After the school trip, you have two piles of data.
Pile one: a list with name, age and money paid. Every row looks the same.
Pile two: 200 photos, a voice message and a long email from a parent.
Which pile can you add up? Which pile can you not? This lesson explains why.
Key words
| Word | What it means | Example |
|---|---|---|
| Structured data | Data in a fixed shape: rows and columns, and each column has one data type. | A table of names and ages |
| Unstructured data | Data with no fixed shape. You keep it as one object. | A photo or an email |
| Data type | The kind of thing a value is. | integer, real, string |
| Integer | A whole number. | 16 |
| Real | A number with a decimal point. Python calls it a float. | 12.50 |
| Float | The Python name for a real number. | 12.50 |
| String | Text. | "12.50" |
| Boolean | True or False. | True |
| Blob | A big lump of data, such as a photo or a song, kept as one object. | photo.jpg |
| Casting | Changing a value from one data type to another. | float("12.50") |
| Transformation | Changing data from one form to another, so it is more useful. | Text to number |
| Database | A place where data is kept in tables. | Trip payments |
The cells students fill in are left blank in this preview.
Structured and unstructured
| Structured data | Unstructured data | |
|---|---|---|
| Shape | Rows and columns | No fixed shape |
| Data types | Each column has one type | One object: a string or a blob |
| Example | Name, age, money paid | A photo, a voice message, an email |
| Can you search a part of it? | Yes, by column | Not easily |
| Can you add it up? | Yes, if the column is a number | No |
The cells students fill in are left blank in this preview.
Quick check: structured or not?
Have a go
Quick check: structured or not?
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
How structure and type fit together
Structured data splits things into small fields. Each field gets its own data type. Age is an integer. Money paid is a real number. Name is a string.
Unstructured data stays in one lump. A photo is a blob. An email is one long string. The computer does not know what is inside.
Try it: fields with types
payments = [
["Sam", 12.5, True],
["Aisha", 5.0, True],
["Ben", 12.5, False],
["Chloe", 0.0, False],
["Dev", 8.25, True],
]
# Each field has one type: a string, a real number, a Boolean.
# paid is a number, so > compares it as a number.
for row in payments:
if row[1] > 5:
print(row[0], row[1])
# Text does not compare like numbers. Is "12.50" more than "5"?
print("12.50" > "5")
Changing a type
Money saved as text cannot be added up yet. Casting turns the string "12.50" into the real number 12.5.
In Python you cast with int(), float() and str(). After casting, the value has a new data type.
Casting can go wrong
data-callouts.warning.label
Turning 12.99 into an integer gives 12. You lose the .99. Turning "abc" into a number gives an error. Turning "0045" into an integer gives 45, and you lose the zeros.
Try it: casting in Python
text = "12.50"
print(text, type(text))
number = float(text)
print(number, type(number))
print(int(12.99))
print(str(16) + " years old")
# Change the text to "abc" and see the error.