Week 9 · 1.5 Data in text-based programs · 3 lessons
1
Every value has a type
Computing 8: Topic 1.5, pp. 106–124
GoalName the five data types and choose the right one for a piece of data.
New words
data type · the kind of value a variable holds, which decides what can be done with itinteger · a whole number with no decimal partreal · a number that can have a decimal part (float in Python)character · a single letter, digit or symbolBoolean · a value that is either True or False
Explanation
Every piece of data has a data type. An integer is a whole number, such as 24. A real number can have a decimal part, such as 3.75, and Python calls it a float. A character is one letter, digit or symbol, such as "K". A string is any text, such as "Trail", and Python stores a character as a string of length one. A Boolean is True or False. Choosing the right type matters because it decides what the computer can do. You can add integers, but adding two strings joins them. A code that looks like a number, such as a locker code 007, should be a string if the leading zeros must stay. A good rule is to ask whether you would ever do arithmetic with the value.
Data type
Meaning
Example
Python name
integer
whole number
24
int
real
number with a decimal part
3.75
float
character
one letter, digit or symbol
"K"
str (length 1)
string
text of any length
"Trail"
str
Boolean
True or False
True
bool
Worked examples
Fact
Value
Best type
Reason
number of wheels
2
integer
a whole number that can be counted
price
49.99
real
has a decimal part
frame size
"M"
character
one letter
model name
"Trail"
string
text
in stock
True
Boolean
only two possible answers
A bike shop keeps these facts about one bike. The reason decides the type.
A locker code that starts with zeros must be a string. As text it keeps both zeros. When it is cast to an integer, the zeros are lost. The user types 007.
Class activity
Data type sort (no computer). Cut ten small cards and write on each a value: 8, 8.5, "8", "R", True, 120, "Lima", 0.5, False and "B12". Make five labelled piles: integer, real, character, string and Boolean. Sort the cards and discuss any that could go in two piles. Then think of three pieces of data about your school, a market stall or a sports team, and write the best type for each with a reason. If you have a computer or tablet, ask an adult to show what + does with two numbers and with two strings.
Practice
1
Choose the best data type for each item: integer, real, character, string or Boolean.
Data
Example
Best type
pages in a book
312
?
a runner’s time in seconds
11.85
?
a team’s group letter
"B"
?
a pupil’s town
"Lima"
?
whether a door is open
True
?
Pages: integer. Time in seconds: real. Letter grade: character. Town: string. Door open: Boolean.
13, then 94 (the strings are joined), then 4.5, then 4.0 (a division always gives a real number).
3
Why should a locker code such as 0042 be stored as a string and not as an integer?
An integer does not keep leading zeros, so 0042 would become 42, a different code. We never do arithmetic on a code, so it does not need to be a number.
4
Is "12" an integer? Explain. What is the difference between "A" and "ABC"?
No. It is a string, because it is inside quotation marks. "A" is a character (a string of length one) and "ABC" is a string of three characters.