AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Types, flags and fixed values

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 typeMeaningExamplePython name
integerwhole number24int
realnumber with a decimal part3.75float
characterone letter, digit or symbol"K"str (length 1)
stringtext of any length"Trail"str
BooleanTrue or FalseTruebool
Worked examples
FactValueBest typeReason
number of wheels2integera whole number that can be counted
price49.99realhas a decimal part
frame size"M"characterone letter
model name"Trail"stringtext
in stockTrueBooleanonly two possible answers
A bike shop keeps these facts about one bike. The reason decides the type.
Python
1print(7 + 2)
2print(7 / 2)
3print("7" + "2")
4print(10 / 5)
Output
9
3.5
72
2.0
The type changes what an operator does. + adds two numbers but joins two strings. / always gives a real number, even when the answer is whole.
Python
1code = input("Locker code? ")
2print(code)
3print(int(code))
Output
007
7
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.
DataExampleBest type
pages in a book312?
a runner’s time in seconds11.85?
a team’s group letter"B"?
a pupil’s town"Lima"?
whether a door is openTrue?
2
Predict what each line prints.
Python
1print(9 + 4)
2print("9" + "4")
3print(9 / 2)
4print(8 / 2)
3
Why should a locker code such as 0042 be stored as a string and not as an integer?
4
Is "12" an integer? Explain. What is the difference between "A" and "ABC"?