And/Or

2.3. And/Or#

It is possible to build more complex conditions using and or or.

  • and: both conditions have to be True to evaluate as True

  • or: only one condition has to be True to evaluate as True

One way to remember this is:

  • if you want fish and chips, you are expecting both.

  • if you want fish or chips, you are only expecting one or the other, but you’re also happy if you get both.

You can visualise these rules using a truth table:

../../_images/truth_table.png
Question 1

Does the following program return True or False?

x = 4
print(x > 2 or x < 10)
Solution

x < 2 is True

x < 10 is True

True or True = True

Question 2

Does the following program return True or False?

x = 1
print(x < 2 and x > 3)
Solution

Solution is locked

Question 3

Does the following program return True or False?

x = 6
y = 2
z = x * y

print(z > x or y > 2)
Solution

Solution is locked

Question 3

Does the following program return True or False?

day = 5
month = 'November'
print(day == 5 and month != 'November')
Solution

Solution is locked