Python · 1.2
Variables and Data Types
Name
Class
Date
Mark / 25
Section A
Circle the letter of the correct answer. One mark each.
- 1What does
score = score + 5do if score was 10?1- ACauses an error
- BChecks whether score equals 15
- CCreates a new variable called score + 5
- DMakes score refer to 15
- 2What type is
3.14?1- Astr
- Bfloat
- Cint
- Dbool
- 3What does
"5" + "3"give?1- AAn error
- B8
- C"53"
- D"8"
- 4What does
int(7.9)give?1- A8
- B7.9
- CAn error
- D7
- 5Which type should store whether a user is logged in?1
- Abool
- Bfloat
- Cint
- Dstr
- 6Why store a phone number as a string?1
- AA leading zero would be lost if it were an int
- BIt runs faster
- CPhone numbers are too long for an int
- DStrings use less memory
- 7What is
score += 3short for?1- Ascore == score + 3
- Bprint(score + 3)
- Cscore = score + 3
- Dscore = 3
- 8Which variable name is invalid in Python?1
- A2nd_score
- BscoreTwo
- C_score
- Dsecond_score
- 9What does
type("42")return?1- Aint
- Bbool
- Cstr
- Dfloat
- 10Which is the Python convention for a constant?1
- AMaxlives
- BMAX_LIVES
- Cmax_lives
- DmaxLives
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1StateState the most suitable data type for each of: a student's age, a product price, whether an order is paid.3
- 2ExplainExplain why
"10" + "5"gives a different result from10 + 5.3 - 3ExplainExplain why a phone number should be stored as a string rather than an integer.3
- 4WriteWrite Python code that creates a variable for a player's score starting at zero, adds 25 to it, and prints the result.3
- 5ExplainA program uses
int(4.8)and the programmer expects the result 5. Explain what actually happens and how to get the intended result.3
Mark scheme
Section A
Section B
1 State the most suitable data type for each of: a student's age, a product price, whether an order is paid. 3
One mark for each of the following, to a maximum of 3.
- Age is an integer
- Price is a float
- Paid is a boolean
Model answer. A student's age should be an integer, because ages are whole numbers and arithmetic is performed on them. A product price should be a float, because prices include pence and therefore need a decimal part. Whether an order is paid should be a boolean, because there are only two possible states and a boolean makes that meaning completely clear while using the least memory.
2 Explain why "10" + "5" gives a different result from 10 + 5. 3
One mark for each of the following, to a maximum of 3.
- The first pair are strings
- The plus operator joins strings together rather than adding
- The second pair are integers so the plus operator performs addition
Model answer. In the first case both values are strings, because they are written inside quotation marks. When the plus operator is applied to two strings it concatenates them, joining them end to end, so the result is the string "105". In the second case both values are integers, so the plus operator performs ordinary numerical addition and the result is 15. This is why input from the user must be converted with int() before any arithmetic is done on it.
3 Explain why a phone number should be stored as a string rather than an integer. 3
One mark for each of the following, to a maximum of 3.
- Phone numbers often begin with a zero
- An integer would discard the leading zero, changing the number
- No arithmetic is ever performed on a phone number
Model answer. Phone numbers in the UK begin with a zero, and an integer stores only the numeric value with no record of leading zeros, so 07700900123 would be stored and displayed as 7700900123, which is not the correct number and would fail to match records held elsewhere. In addition, no arithmetic is ever carried out on a phone number, so there is no benefit at all to storing it as a number. A string preserves every character exactly as entered, which is what is needed.
4 Write Python code that creates a variable for a player's score starting at zero, adds 25 to it, and prints the result. 3
One mark for each of the following, to a maximum of 3.
- Initialises the variable to 0
- Adds 25 to it
- Prints the result
Model answer. score = 0 score += 25 print("Your score is", score) The variable is created and set to 0 first, which matters because a variable cannot be used before it exists. The += shortcut adds 25 to whatever the score currently is, and the print statement then displays the updated value.
5 A program uses int(4.8) and the programmer expects the result 5. Explain what actually happens and how to get the intended result. 3
One mark for each of the following, to a maximum of 3.
- int() removes the decimal part rather than rounding
- The result is therefore 4
- Use round() to round to the nearest whole number
Model answer. The int() function converts a value to a whole number by discarding everything after the decimal point rather than rounding, so int(4.8) gives 4 rather than 5. To round to the nearest whole number the programmer should use round(4.8), which correctly gives 5. This distinction matters in any calculation involving averages or prices, where truncating instead of rounding can produce noticeably wrong results.