Types, flags and fixed values
Answers are for parents and teachers.
1
Choose the best data type for each: the number of goals, the price of a pen (0.75), the first letter of a name, a team’s motto, whether a light is on.
Goals: integer. Price: real. First letter: character. Motto: string. Light on: Boolean.
2
What does each line print? print(5 / 2), print("5" + "2"), print(int(4.9)), print(bool("")).
Python
1print(5 / 2) 2print("5" + "2") 3print(int(4.9)) 4print(bool(""))
2.5, 52, 4, False. The division gives a real, the plus joins strings, int cuts off the decimal part and an empty string is False.
3
Write a Python program with a constant TAX = 2. Ask for a price and print the price plus the tax.
Model answer (lines are separated by /): TAX = 2 / price = int(input("Price? ")) / print("You pay:", price + TAX)
4
Write a line that stores in the flag raining whether the rain amount is more than 0, and an if line that uses it to print Take an umbrella.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): raining = rain > 0 / if raining: / [indent] print("Take an umbrella")
5
Explain why the text "False" is not the same as the Boolean False, and what bool("False") gives.
"False" is a string, so it is only text. The Boolean False is a value with no quotation marks. bool("False") gives True, because any non-empty string is True.