Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · 1.3

Input and Operators

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 17 marks. Total 27 marks.

Name

Class

Date

Mark  / 27

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What does input() always return?1
    1. AA float
    2. BA boolean
    3. CAn integer
    4. DA string
  2. 2What does 17 % 5 give?1
    1. A2
    2. B3
    3. C3.4
    4. D85
  3. 3What does 17 // 5 give?1
    1. A2
    2. B3
    3. C3.4
    4. D12
  4. 4Which condition correctly tests whether n is even?1
    1. An % 2 == 1
    2. Bn % 2 == 0
    3. Cn == 2
    4. Dn / 2 == 0
  5. 5How would you get the seconds part of a total number of seconds?1
    1. Atotal % 60
    2. Btotal / 60
    3. Ctotal // 60
    4. Dtotal * 60
  6. 6What does 7 / 2 give in Python?1
    1. A3
    2. B3.0
    3. C3.5
    4. D4
  7. 7What does 6 / 2 give?1
    1. A3
    2. B3.0
    3. C2
    4. DAn error
  8. 8What is 2 ** 5?1
    1. A7
    2. B10
    3. C25
    4. D32
  9. 9Which correctly reads a whole number from the user?1
    1. Ainput("Age: ") + 0
    2. Bint("Age: ")
    3. Cint(input("Age: "))
    4. Dinput(int("Age: "))
  10. 10What does " hi ".strip() give?1
    1. A" hi "
    2. BAn error
    3. C"hi "
    4. D"hi"

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1ExplainExplain why age = input("Age: ") followed by if age > 18: causes an error.3
  2. 2WriteWrite a program that asks for a number and states whether it is even or odd.4
  3. 3ExplainExplain the difference between the / operator and the // operator in Python.2
  4. 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
  5. 5ExplainExplain what the modulus operator does and give two situations where it is useful.4

Mark scheme

Section A

1D2A3B4B5A6C7B8D9C10D

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.