Data in detail
Answers are for parents and teachers.
1
Choose the best data type for each: the number of goals in a match, a price of 4.50, a pupil’s name, whether the lights are on, and the first letter of a name.
Integer; real; string; Boolean; character (a string of length 1 in Python).
2
Work out what these two lines print: print(int("15") + int(2.9)) and print("15" + str(2)).
17 and then 152. The first line adds the numbers 15 and 2. The second joins the texts "15" and "2".
3
The program starts with word = "Mountain". Write the index of every letter, then say what word[2], word[7] and len(word) give.
Python
1word = "Mountain" 2print(word[2]) 3print(word[7]) 4print(len(word))
M 0, o 1, u 2, n 3, t 4, a 5, i 6, n 7. word[2] is u, word[7] is n and len(word) is 8.
4
Write a program that asks for a word and then prints it with one character on each line, using a for loop.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): word = input("Word? ") / for i in range(len(word)): / [indent] print(word[i])
5
Explain why the loop line is range(len(word)) and not range(len(word) + 1). What happens with the second one?
The valid indexes are 0 to len(word) - 1. range(len(word)) gives exactly those. With + 1 the last pass asks for an index that does not exist, and Python stops with an IndexError.