Chapter 2: Logical operators

Test your understanding

face Josiah Wang

Now let us test your understanding further by combining logical operators with boolean expressions like x == 3.

Warning - some are trick questions! There are some subtle details when using logical and boolean expressions together that you might not even notice! So do read the explanations even if you entered the correct answer!

1 2 3 4 5 6 7 8 9 10

Question 1

What is the value of z?

x = 7
y = 2
z = x>3 and y<1

False
Explanation:

The comparison operators have the highest precedence, followed by the logical operators, and finally the assignment operator. So you should evaluate the comparison operators first. So z = ((x>3) and (y<1)). y<1 is False, making the whole thing False. Then z is set to False.

Question 2

What is the value of z?

x = 7
y = 2
z = x>3 or y<1

True
Explanation:

The comparison operators have the highest precedence, followed by the logical operators, and finally the assignment operator. So you should evaluate the comparison operators first. So z = ((x>3) or (y<1)). x>3 is True, making the whole thing True whatever the result of y<1. Python actually does not even check y<1 - this is called short-circuiting.

Question 3

What is the value of z?

x = 7
z = not x>=5 and x<3

False
Explanation:

Remember the operator precedence: not > and > or. So this should be parsed as (not x>=5) and (x<3). The first part is False, making the whole expression False.

Question 4

What is the value of z?

x = 7
z = not (x>=5 and x<3)

True
Explanation:

Always evaluate what is inside any parenthesis first. So x>=5 is True and x<3 is False, making this False. Then apply not which makes it True.

Question 5

What is the value of z?

x = 7
y = 2
z = not 5+y > 3*x or 4 == x+3

True
Explanation:

Again, with the operator precedence rules, this should be evaluated as (not ((5+y) > (3*x))) or (4 == (x+3)). The first half is True, making the whole expression True.

Question 6

What is the value of z?

z = True == not False

Error
Explanation:

Python returns a syntax error, even with just True == not False (try it yourself!) Remember that == has higher precedence than not, so Python is reading this expression as (True == not) False, which does not make any sense. You should write z = True == (not False) or even z = (True == (not False)) if you want to be even more explicit.

Question 7

What is the value of z?

x = 7
z = not (x>=5 or x<3) == (not x>=5 and not x<3)

True
Explanation:

This is De Morgan's Theorem. not (A or B) is equal to (not A) and (not B).

Question 8

What is the value of z?

x = 7
z = (3<x<5) == (3<x and x<5)

True
Explanation:

If you remember from the previous lesson, you can chain boolean expressions. So 3<x<5 is equivalent to 3<x and x<5.

Question 9

What is the value of z?

x = 7
z = 3<x<5 == 3<x and x<5

False
Explanation:

This one is a trick question. Because of operator precedence, this expression is parsed as (3<x<5==3<x) and (x<5). The first half is a chained boolean expression, so it is equivalent to 3<x and x<5 and 5==3 and 3<x, which is False.

Question 10

What is the value of z?

x = 7
z = 3<x<5 == (3<x and x<5)

False
Explanation:

Another trick question. Although there is a parenthesis in the second half, the first half is not grouped together since < and == have the same precedence. So Python assumes that this is a chained expression: 3<x and x<5 and 5==(3<x and (x<5)). Obviously, the last part (5==False) is clearly False.