Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

6.4 (2 of 2): Structure, types and changing types

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
Structured dataData in rows and columns, each column of one data type.A table of names and ages
Unstructured dataData with no fixed shape, kept as one object.A photo or an email
Data typeThe kind of thing a value is.integer, real, string
IntegerA whole number.16
RealA number with a decimal point.12.50
FloatThe Python name for a real number.12.50
StringText."12.50"
BooleanTrue or False.True
BlobA big lump of data kept as one object.photo.jpg
CastingChanging a value from one data type to another.float("12.50")
TransformationChanging data from one form to another.Text to number
DatabaseAn organised store of data in tables.Trip payments

The cells students fill in are left blank in this preview.

Structured and unstructured

Structured dataUnstructured data
ShapeRows and columnsNo fixed shape
Data typesEach column has one typeOne object: a string or a blob
ExampleName, age, money paidA photo, a voice message, an email
Can you search a part of it?Yes, by columnNot easily
Can you add it up?Yes, if the column is a numberNo

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

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

Code: python
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

Code: 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

Code: python
# 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

Code: 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.

Code: python
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.

Code: python
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?

JobBest choiceWhy
Add up money paidStructured, real numberYou can only add numbers.
Count students under 16Structured, integerYou compare whole numbers.
Keep the parent email as it arrivedUnstructured, stringYou do not need to search inside it.
Store the class photoUnstructured, blobIt is one big file.
Find all students who have signedStructured, BooleanYes 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Watch: 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

  1. 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?
  2. 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).
  3. 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).
  4. 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).
  5. 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).
  6. 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.