Chapter 1: Introduction

Quiz

face Josiah Wang Joe Stacey

Now, here is a quick quiz to make sure that you understand and remember what have been covered so far!

As usual, this quiz is ungraded, so feel free to make as many mistakes as you like - that is how you learn!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Question 1

What is the value of y? Type Error if you are expecting an error.

x = 5
y = x < 11 % 2

False
Explanation:

The modulo operator % has the highest precedence, so 11 % 2 is computed first giving 1. And since x == 5 and 5 > 1, this returns False.

Question 2

What is the value of y? Type Error if you are expecting an error.

y = 10 // 2 <= 5.0000

True
Explanation:

The floor division operator // has the highest precedence, so 10 // 2 is evaluated resulting in 5. And since 5 <= 5.0000 (in fact, 5 == 5.0000), y is set to True.

Question 3

What is the value of y? Type Error if you are expecting an error.

x = 5
y = 10 < x < 3 + 1

False
Explanation:

Since x == 5, neither 10 < x nor x < 3+1 is True.

Question 4

What is the value of y? Type Error if you are expecting an error.

y = 2.2 + 4.4 == 6.6

False
Explanation:

Remember that floating point values are usually approximations when stored on a computer. In this case 2.2+4.4 results in 6.6000000000000005. This does not always occur for all floating points though, e.g. 1.1+3.3==4.4 is True.

Question 5

What is the value of y? Type Error if you are expecting an error.

3 < 5 + 1 = y

Error
Explanation:

The left hand side of an assignment statement must be a variable.

Question 6

What is the value of y? Type Error if you are expecting an error.

import math
y = math.inf + 1000 * 2

inf
Explanation:

Infinity + 2000 is still infinity.

Question 7

What is the value of y? Type Error if you are expecting an error.

y = None

None
Explanation:

The value is None, although technically None has no value. Python does not show anything if you type y in an interactive prompt (because None does not have a value). But print(y) gives you None because it converts None to its str representation. Try str(None), you should get the string 'None'.

Question 8

What is the value of y? Type Error if you are expecting an error.

y = None == 0

False
Explanation:

The keyword None is not equal to 0.

Question 9

What is the value of y? Type Error if you are expecting an error.

y = "money" < "happiness"

False
Explanation:

The character "m" comes after "h" in the unicode code point. So ord("m") > ord("h").

Question 10

What is the value of y? Type Error if you are expecting an error.

y = "Money" < "happiness"

True
Explanation:

The unicode code point of "M" (77) is smaller than the code point of "h" (104), so ord("M") < ord("h").

Question 11

What is the value of y? Type Error if you are expecting an error.

y = len("Tower of London")

15
Explanation:

The str "Tower of London" is made up of 15 characters (including the spaces)

Question 12

What is the value of y? Type Error if you are expecting an error.

y = round(11/2)

6
Explanation:

The function call round(5.5) returns 6.

Question 13

What is the value of y? Type Error if you are expecting an error.

y = round(6.5)

6
Explanation:

Beware that the function does NOT always round up 'borderline' cases, e.g. round(6.5) is rounded DOWN to 6. It depends on whether the number is even or odd. This is a Python feature, apparently it reduces rounding bias. Read this long article if you are interested to learn further.

Question 14

What is the value of y? Type Error if you are expecting an error.

import math
y = math.ceil(math.sqrt(math.floor(math.pi * 3)))

3
Explanation:

Here is composition in action!

  • math.pi * 3 gives you approx 9.42
  • math.floor(9.42) gives you 9 (an int)
  • math.sqrt(9) gives you 3.0 (note that it returns a float)
  • math.ceil(3.0) gives you 3 (an int).

Question 15

What is the output of the following piece of Python code? Please write the output in separate lines if the answer spans multiple lines. Write Error if you are expecting an error.

a = 4
b = a
a = b + a

if b == a:
    print("Yes")
else:
    print("No")

print(a)
print(b)

No 8 4
Explanation:

The values of b and a are different after the reassignment in Line 3.

Question 16

What is the output of the following piece of Python code? Please write the output in separate lines if the answer spans multiple lines. Write Error if you are expecting an error.

message = "Piccadilly"

if len(message) < 3:
    print("Short")
elif message == "Kensington":
    print("Expensive")
elif len(message) < 20:
    print("Long")
    message = "Oxford Street"
elif message == "Oxford Street":
    print("Shopping")
    message = "Imperial College"
elif 5 < len(message) <= 15: 
    print("Medium")
    message = "nowhere"
elif message == "Piccadilly":
    print("Busy")

print(f"You are at {message}")

Long You are at Oxford Street
Explanation:

The first matching condition is the third condition len(message) < 20. This prints out Long and sets message to be "Oxford Street". This is multiway selection, so no other conditions are examined despite the value of message changing inside the body. In the end, it prints out the value of message, which is "Oxford Street".