Python · 1.3
Input and Operators
Name
Class
Date
Mark / 27
Section A
Circle the letter of the correct answer. One mark each.
- 1What does
input()always return?1- AA float
- BA boolean
- CAn integer
- DA string
- 2What does
17 % 5give?1- A2
- B3
- C3.4
- D85
- 3What does
17 // 5give?1- A2
- B3
- C3.4
- D12
- 4Which condition correctly tests whether n is even?1
- An % 2 == 1
- Bn % 2 == 0
- Cn == 2
- Dn / 2 == 0
- 5How would you get the seconds part of a total number of seconds?1
- Atotal % 60
- Btotal / 60
- Ctotal // 60
- Dtotal * 60
- 6What does
7 / 2give in Python?1- A3
- B3.0
- C3.5
- D4
- 7What does
6 / 2give?1- A3
- B3.0
- C2
- DAn error
- 8What is
2 ** 5?1- A7
- B10
- C25
- D32
- 9Which correctly reads a whole number from the user?1
- Ainput("Age: ") + 0
- Bint("Age: ")
- Cint(input("Age: "))
- Dinput(int("Age: "))
- 10What does
" hi ".strip()give?1- A" hi "
- BAn error
- C"hi "
- D"hi"
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1ExplainExplain why
age = input("Age: ")followed byif age > 18:causes an error.3 - 2WriteWrite a program that asks for a number and states whether it is even or odd.4
- 3ExplainExplain the difference between the / operator and the // operator in Python.2
- 4ExplainA program asks the user for a number of pencils and a total cost, then works out the cost of one pencil. Explain which operators and conversions are needed and why.4
- 5ExplainExplain what the modulus operator does and give two situations where it is useful.4
Mark scheme
Section A
Section B
1 Explain why age = input("Age: ") followed by if age > 18: causes an error. 3
One mark for each of the following, to a maximum of 3.
- input returns a string
- A string cannot be compared with a number
- Convert with int() first
Model answer. The input function always returns a string, so age holds the text "20" rather than the number 20 even if the user types digits. Python cannot compare a string with an integer using a greater than operator, because there is no meaningful way to order text against a number, so it raises a TypeError. The fix is to convert the input as it is read, writing age = int(input("Age: ")), after which the comparison works as intended.
2 Write a program that asks for a number and states whether it is even or odd. 4
One mark for each of the following, to a maximum of 4.
- Asks for input and converts it to an integer
- Uses the modulus operator to test divisibility by 2
- Prints Even when the remainder is 0
- Uses else to print Odd otherwise
Model answer. number = int(input("Enter a number: ")) if number % 2 == 0: print("Even") else: print("Odd") The input is converted to an integer so arithmetic can be performed. The modulus operator gives the remainder after dividing by 2, and a remainder of exactly 0 means the number divides evenly and is therefore even. Any other remainder means it is odd, which the else branch handles.
3 Explain the difference between the / operator and the // operator in Python. 2
One mark for each of the following, to a maximum of 2.
- / performs normal division and always gives a float
- // performs integer division, giving only the whole number part
Model answer. The single slash performs ordinary division and always produces a float, so 7 / 2 gives 3.5 and even 6 / 2 gives 3.0 rather than 3. The double slash performs integer division, discarding anything after the decimal point and returning only the whole number part, so 7 // 2 gives 3. Integer division is useful when a fractional result makes no sense, for example working out how many whole minutes are in a number of seconds.
4 A program asks the user for a number of pencils and a total cost, then works out the cost of one pencil. Explain which operators and conversions are needed and why. 4
One mark for each of the following, to a maximum of 4.
- input returns a string so both values must be converted
- The count is converted with int because it is a whole number
- The cost is converted with float because it has a decimal part
- Division with a single slash is used, which correctly gives a float
Model answer. count = int(input("How many pencils? ")) cost = float(input("Total cost: ")) print(f"Each pencil costs {cost / count:.2f}") The input function always returns a string, so neither value can be used in arithmetic until it is converted. The number of pencils is converted with int because a count is always a whole number, while the total cost is converted with float because prices include pence and an int would discard them. Ordinary division with a single slash is correct here because the answer genuinely may have a decimal part, and it returns a float even when the division happens to be exact. Integer division would be wrong, because it would throw away the pence.
5 Explain what the modulus operator does and give two situations where it is useful. 4
One mark for each of the following, to a maximum of 4.
- Modulus returns the remainder after division
- Gives a correct example such as 17 % 5 being 2
- First use such as testing whether a number is even
- Second use such as extracting time units or cycling a value
Model answer. The modulus operator performs a division and returns the remainder rather than the quotient, so 17 % 5 gives 2 because 5 goes into 17 three times with 2 left over. One common use is testing divisibility: if a number modulus 2 equals 0 then it divides exactly by two and is therefore even, and the same technique checks whether something should happen every third or every tenth time round a loop. A second use is extracting units from a total, for example converting a total number of seconds into minutes and seconds, where total // 60 gives the whole minutes and total % 60 gives the seconds remaining. Modulus is also used to wrap a value around a fixed range, such as moving a player back to the start of a board after the last square.