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

Data in detail

Week 7 · 1.5 Programming with data · 3 lessons
1

Pick the right type

Computing 9: Topic 1.5, pp. 89–118
GoalChoose a suitable data type for a value, and use casting to change a value from one type to another.
New words
data type · the kind of value stored, which decides what the program can do with itcharacter · a single letter, digit or symbol; Python stores it as a string with one charactercasting · changing a value from one data type to another, for example with int() or str()
Explanation

Every value has a data type: integer for whole numbers, real for numbers with a decimal point, string for text, and Boolean for True or False. A character is one single letter, digit or symbol. Python has no separate character type, so it stores a character as a string of length 1. Quotation marks make text, so 7 is a number but "7" is a string. Casting changes the type. int() turns "12" into 12, and it cuts the decimals off a real number without rounding: int(9.99) is 9. float() gives a real number, so float(7) is 7.0. str() makes text, so str(15) is "15". bool() gives False for 0 and for the empty string "", and True for every other number and every other string, even "False".

ValueTypeWhy
40integera whole number
3.75realit has a decimal point
"Kazan"stringtext in quotation marks
"7"stringa digit, but in quotation marks
TrueBooleanno quotation marks, capital T
"k"characterone symbol (a string of length 1 in Python)
Worked examples
Python
1a = "250"
2b = int(a) + 50
3print(b)
4print(int(9.99))
5print(float(7))
6print(str(15) + "cm")
Output
300
9
7.0
15cm
Casting in four lines. The string "250" is cast with int and 50 is added, which gives 300. int(9.99) cuts off the decimals and gives 9. float(7) gives 7.0. str(15) gives the text "15", and joining it with "cm" gives 15cm. All four results were produced by running the program.
Python
1print(bool(0))
2print(bool(5))
3print(bool(""))
4print(bool("No"))
Output
False
True
False
True
Boolean casting. The number 0 gives False, any other number gives True. The empty string gives False. The string "False" is not empty, so it gives True. Python looks at whether a string has something in it, not at what it says.
DataBest typeReason
Number of goals in a matchintegeryou cannot score half a goal
Height 1.62 mrealit has a decimal point
Phone number 0551234567stringnot used in sums; the leading 0 matters
Is the door open?Booleanonly two answers
Grade letter Bcharacterone symbol
Choosing the best type. A phone number is stored as a string even though it is made of digits, because we never add phone numbers and a leading 0 must stay.
Class activity

Casting machine (no computer). Write ten values on cards, such as 7, "7", 7.0, True, "True", 0, "", 12.8, "hello" and "3.5". Sort them into piles: integer, real, string, Boolean. Then draw a machine with four boxes, int(), float(), str() and bool(). Put each card through two of the machines and write down what comes out. Mark every card that would cause a ValueError, such as int("hello") or int("3.5"). Check the bool() results against the rule in the lesson.

Practice
1
Give the data type of each value.
ValueData type
18?
"18"?
18.0?
False?
"Fatima"?
"z"?
2
What does this program print?
Python
1x = int("40")
2y = int(6.9)
3print(x + y)
4print(str(x) + str(y))
3
The user types 12. Python stops with an error. What is the error message, and how do you correct the program?
Python
1age = input("Age? ")
2print(age + 1)
4
What is printed by each line?
Python
1print(bool(7))
2print(bool(0.0))
3print(bool("0"))
4print(bool(" "))