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".
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.
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.
Data
Best type
Reason
Number of goals in a match
integer
you cannot score half a goal
Height 1.62 m
real
it has a decimal point
Phone number 0551234567
string
not used in sums; the leading 0 matters
Is the door open?
Boolean
only two answers
Grade letter B
character
one 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.
Value
Data type
18
?
"18"
?
18.0
?
False
?
"Fatima"
?
"z"
?
18 is an integer. "18" is a string. 18.0 is a real number. False is a Boolean. "Fatima" is a string. "z" is a character, which Python stores as a string of length 1.
46 and then 406. int("40") is 40 and int(6.9) is 6, so the sum is 46. str(40) + str(6) joins the two texts to make "406".
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)
TypeError: can only concatenate str (not "int") to str. input always gives text, so age is a string. Change the first line to age = int(input("Age? ")).