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 rows and columns, each column of one data type. | A table of names and ages |
| Unstructured data | Data with no fixed shape, kept 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. | 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 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. | Text to number |
| Database | An organised store of data 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 questionsHow 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")
Make a free account to save your work and track your progress.
Casting changes the type of a value
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
Warning
Many people think casting only changes the type. It can also change the value. Turning 12.99 into an integer gives 12, so you lose the .99. Turning "0045" into an integer gives 45, so you lose the zeros. Turning "abc" into a number gives an error.
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.
Make a free account to save your work and track your progress.
Match the change to what happens
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Extra: Try it: text to a date
# A date can be text, or a real date that Python can count with.
import datetime
text = "2026-06-14"
day = datetime.date.fromisoformat(text) # cast the text to a date
print(day, type(day))
print(day.year)
print(day.strftime("%d %B %Y")) # back to text, in a new shape
# Change the text to "2026-02-30". There is no 30 February, so you get an error.
Make a free account to save your work and track your progress.
Extra: Try it: a cast in SQL
-- The money in this table was saved as text. CAST turns it into a real number.
SELECT student, paid_text, typeof(paid_text) AS before_type,
CAST(paid_text AS REAL) AS paid, typeof(CAST(paid_text AS REAL)) AS after_type
FROM trip_payments;
Make a free account to save your work and track your progress.
Your turn: find who owes money
The trip costs 12.50. Each payment row keeps the money paid as a real number. Fill in two functions. The first lists the students who paid less than the price. The second adds up how much money is still owed. Press Run Tests to check.
def who_owes(payments, price):
# payments is a list of rows: [student, paid, signed].
# student is a string, paid is a real number, signed is a Boolean.
# Give back a list of the students who paid less than the price, in the same order.
return None
def still_owed(payments, price):
# Give back the total money still owed: for each student, the price minus what they paid.
# A student who paid the full price owes 0.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Your turn: change the types
Fill in the five functions. Each one changes data from one type to another. Press Run Tests to check your work.
def total_paid(texts):
# texts is a list of strings such as ["12.50", "5.00"].
# Turn each one into a float and give back the total.
return None
def price_label(amount):
# amount is a number such as 12.5. Give back text like "12.5 pounds".
return None
def next_year_age(text):
# text is a string such as "16". Give back the age one year later, as an integer: 17.
return None
def yes_no(signed):
# signed is True or False. Give back "Yes" or "No".
return None
def clean_age(text):
# text is a string like " 16 " with spaces. Give back the integer 16.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Which structure and type?
| Job | Best choice | Why |
|---|---|---|
| Add up money paid | Structured, real number | You can only add numbers. |
| Count students under 16 | Structured, integer | You compare whole numbers. |
| Keep the parent email as it arrived | Unstructured, string | You do not need to search inside it. |
| Store the class photo | Unstructured, blob | It is one big file. |
| Find all students who have signed | Structured, Boolean | Yes or no is easy to filter. |
The cells students fill in are left blank in this preview.
Judge the choice
Have a go
Judge the choice
3 questionsWatch: two piles of trip data
Press Play on the short film below. It follows the two piles of trip data, the type of each field, and casting. Listen for the myth about what casting changes.
Film: Structure, types and changing types
Structure, types and changing types
Every scene, as text
- Two kinds of trip data. Pile one: a list with name, age and money paid (structured: rows and columns). Pile two: 200 photos, a voice message and a long email (unstructured: no fixed shape). Which pile can you add up?
- How structure and type fit together. Age: 16 (integer: a whole number). Money paid: 12.50 (real: a number with a decimal point). Name: Sam (string: text). A photo (blob: a big lump kept as one object). An email (string: one long text).
- Text does not compare like numbers. Money saved as text cannot be added up yet. print("12.50" > "5") prints False (as text: "1" comes before "5"). print(12.5 > 5) prints True (as numbers: 12.5 is more than 5).
- Casting changes the type of a value. text = "12.50" (string: text, not a number yet). number = float(text) (casting: one data type to another). print(number, type(number)) prints 12.5 and its class, float (float: the Python name for a real number).
- Which structure and type? Add up money paid (best: structured, real number). Count students under 16 (best: structured, integer). Store the class photo (best: unstructured, blob). Paid or not paid (best: structured, Boolean).
- Myth (false): casting only changes types. That is not true: casting can also change the value. print(int(12.5)) prints 12 (lost: the .5, cut off, not rounded). print(int("06")) prints 6 (lost: the zero at the start).
Final check
Have a go
Final check
5 questionsRemember
Tip
Structured data has fields and each field has a type. Unstructured data is one lump. Cast a value to change its type, and check what you lose.