• No results found

Add some logic

In document Real Python Part 1 (Page 63-67)

Python also has special keywords (called logical operators) for comparing two expressions, which are: and, or, and not. These keywords work much the same way as we use them in English.

Let’s start with the and keyword. Saying “and” means that both statements must be true. For in-stance, take the following two simple phrases (and assume they’re both true):

1. cats have four legs 2. cats have tails

If we use these phrases in combination, the phrase “cats have four legs and cats have tails” is true, of course. If we negate both of these phrases, “cats do not have four legs and cats do not have tails” is false. But even if we make one of these phrases false, the combination also becomes false: “cats have four legs and cats do not have tails” is a false phrase. Likewise, “cats do not have four legs and cats have tails” is still false.

Let’s try this same concept out in Python by using some numerical expressions:

1 >>> 1 < 2 and 3 < 4 # both are True

1. 1 < 2 and 3 < 4: both statements are True, so the combination is also True 2. < 1 and 4 < 3: both statements are False, so their combination is also False

3. 1 < 2 and 4 < 3: the first statement (1 < 2) is True, while the second statement (4 < 3) is False;

since both statements have to be True, combining them with the and keyword gives us False 4. 2 < 1 and 3 < 4: the first statement (2 < 1) is False, while the second statement (3 < 4) is True;

again, since both statements have to be True, combining them with the and keyword gives us False

We can summarize these results as follows:

Combination usingand—>Result 1. True and True —> True 2. True and False —> False 3. False and True –> False 4. False and False —> False

It might seem counter-intuitive that “True and False” is False, but think back to how we use this term in English; the following phrase, taken in its entirety, is of course false: “cats have tails and the moon is made of cheese.”

The or keyword means that at least one value must be true. When we say the word “or” in everyday conversation, sometimes we mean an “exclusive or” - this means that only the first option or the second option can be true. We use an “exclusive or” when we say a phrase such as, “I can stay or I can go.” I can’t do both - only one of these options can be true.

However, in programming we use an “inclusive or” since we also want to include the possibility that both values are true. For instance, if we said the phrase, “we can have ice cream or we can have cake,” we also want the possibility of ice cream and cake, so we use an “inclusive or” to mean “either ice cream, or cake, or both ice cream and cake.”

Again, we can try this concept out in Python by using some numerical expressions:

1 >>> 1 < 2 or 3 < 4 # both are True

If any part of our expression is True, even if both parts are True, the result will also be True. We can summarize these results as follows:

Combination usingor—>Result 1. True and True —> True

2. True and False —> True 3. False and True –> True 4. False and False —> False

We can also combine the “and” and “or” keywords using parentheses to create more complicated statements to evaluate, such as the following:

1 >>> False or (False and True)

2

3 False

4

5 >>> True and (False or True)

6

7 True

8

9 >>>

Finally, as you would expect, the not keyword simply reverses the truth of a single statement:

Effect of usingnot—>Result 1. not True —> False 2. not False —> True

Using parentheses for grouping statements together, we can combine these keywords with True and False to create more complex expressions. For instance:

1 >>> (not False) or True

2

3 True

4

5 >>> False or (not False)

6

7 True

8

9 >>>

We can now combine these keywords with the boolean comparators that we learned in the previ-ous section to compare much more complicated expressions. For instance, here’s a somewhat more involved example:

1 True and not (1 != 1)

We can break this statement down by starting on the far right side, as follows:

1. We know that 1 == 1 is True, therefore…

2. 1 != 1 is False, therefore… not (1 != 1) can be simplified to not (False) 3. not False is True

4. Now we can use this partial result to solve the full expression…

5. True and not (1 != 1) can be simplified to True and True 6. True and True evaluates to be True

When working through complicated expressions, the best strategy is to start with the most compli-cated part (or parts) of the expression and build outward from there. For instance, try evaluating this example:

1. (“A” != “A”) or not (2 >= 3)

We can break this expression down into two sections, then combine them:

1. We’ll start with the expression on the left side of the “or” keyword 2. We know that “A” == “A” is True, therefore…

3. “A” != “A” is False, and the left side of our expression is False

4. Now on the right side of the “or”, 2 >= 3 is False, therefore… not (2 >= 3) is True 5. So our entire expression simplifies to False or True

6. False or True evaluates to be True

Note that we didn’t have to use parentheses to express either of these examples. However, it’s usually best to include parentheses as long as they help to make a statement more easily readable.

NOTE: You should feel very comfortable using various combinations of these keywords and boolean comparisons. Play around in the interactive window, creating more com-plicated expressions and trying to figure out what the answer will be before checking yourself, until you are confident that you can decipher boolean logic.

Review exercises:

1. Figure out what the result will be (True or False) when evaluating the following expressions, then type them into the interactive window to check your answers:

1 (1 <= 1) and (1 != 1)

2

3 not (1 != 2)

4

5 ("good" != "bad") or False

6

7 ("good" != "Good") and not (1 == 1)

In document Real Python Part 1 (Page 63-67)