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

Preview

Structure, types and changing types

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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 a fixed shape: rows and columns, and each column has one data type.A table of names and ages
Unstructured dataData with no fixed shape. You keep it 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. Python calls it a float.12.50
FloatThe Python name for a real number.12.50
StringText."12.50"
BooleanTrue or False.True
BlobA big lump of data, such as a photo or a song, 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, so it is more useful.Text to number
DatabaseA place where data is kept 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

Your 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

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")

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

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.

The lesson goes on

9 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.