Chapter 1: Introduction

Quiz

face Josiah Wang

As usual - to make sure that you remember what we have covered so far, here is a quick quiz!

If you make a mistake, learn from it. This quiz will not affect your final course marks, so please feel safe to explore and make mistakes.

1 2 3 4 5 6 7 8 9 10 11 12

Question 1

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

x = 2
y = x == 2 == True

False
Explanation:

The expression x == 2 == True is a chained boolean expression. So Python checks whether x==2 and 2 == True are both True. Unfortunately the second expression is False, making the whole expression False (and this is assigned to y).

Question 2

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

x = 2
y = not 1 < x/2 < 3

True
Explanation:

The comparison operators have higher precedence, so 1 < x/2 < 3 evaluates to False. Inverting False with not results in True.

Question 3

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

x = 2
y = abs(-x) > 5 and round(x) == 2 

False
Explanation:

The first expression abs(-2) > 5 is False, making the whole expression False.

Question 4

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

x = 2
y = not False or x < 2 and True

True
Explanation:

The expression should be evaluated as ((not False) or ((x<2) and True)). Because not False is True, this evaluates to True.

Question 5

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

y = None == False

False
Explanation:

The None object is not equal to False.

Question 6

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

import math
y = math.isnan(math.nan)

True
Explanation:

The math.nan variable from the math module represents a Not-a-number (NaN). The function math.isnan() also from the math module checks whether a given number is NaN.

Question 7

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

import math
y = math.log2(0)

Error
Explanation:

The function call math.log2(0) will produce a ValueError, with the message "math domain error", since log(0) is undefined.

Question 8

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

import math
y = math.isclose(3.14159265, math.pi)

False
Explanation:

Apparently, this is not close enough, since Python defines math.pi as 3.141592653589793, and the default tolerance value for math.isclose() is 1e-09 (1 \times 10^{-9}). You can change the tolerance value using the rel_tol keyword argument, e.g. math.isclose(3.14159265, math.pi, rel_tol=1e-08). We will talk about keyword arguments later in this lesson.

Question 9

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. Write Infinite if you are expecting an infinite loop.

1
2
3
4
5
6
7
8
counter = 0
while counter < 10:
    if counter == 2 or counter == 4:
        pass
    if counter == 4:
        break
    counter = counter + 1
    print(counter)
1 2 3 4
Explanation:

The code will increment counter by 1, and then print the value of counter. Line 4 (pass) does not really do anything, even if the condition in Line 3 is fulfilled. The program will break out of the loop when counter == 4 (lines 5-6). The value 4 is printed since the printing happens after counter has been incremented, but before the condition is checked in the next iteration of the loop.

Question 10

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. Write Infinite if you are expecting an infinite loop.

1
2
3
while timer < 5:
    print(timer)
    timer = timer + 1
Error
Explanation:

What is timer? The code attempted to use something which has not yet been defined.

Question 11

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. Write Infinite if you are expecting an infinite loop.

1
2
3
4
5
6
current_row = 1
max_rows = 3
max_columns = max_rows
while current_row <= max_rows:
    print(f"{current_row}" * max_columns)
    current_row = current_row + 1
111 222 333
Explanation:

The code will print out the row index of the current row N times, where N is the number defined in max_columns (3 in this case).

Question 12

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. Write Infinite if you are expecting an infinite loop.

1
2
3
4
countdown = 10
while countdown % 2 == 0:
    print(countdown)
    countdown = countdown - 2
Infinite
Explanation:

This program will be stuck in an infinite loop, since the value of countdown will always be even.