AND Comparisons

Here’s an interesting way to use some of our comparison operators. Let’s see what happens when we use the word and between two comparisons.

If both are True:


>>> 1==1 and 2==2

True


If only one is True:


>>> 1==1 and 2==3

False


If both are False:


>>> 1==2 and 2==3

False


If both comparisons are True, then the whole expression will be true. But what happens if one of the expressions is False?

Here's a real-life example that might help make some sense of all this:

Suppose you're going to the supermarket because you want to buy some cheese. You need two things: some way to pay for the cheese, and a bag to put it in so that you can carry it home.

If you have both money AND a bag, the shop will let you take your cheese home.

But what if you have a bag, but no money? Then you can't buy the cheese.

And what if you don't have money or a bag? Then you definitely can't buy the cheese.