# Add Some Logic In Python

Python also has special keywords (called logical operators) for comparing two expressions, which are: and , or , and not .

and keyword::

Saying “and” means that both statements must be true. For instance, 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 < 2 and 3 < 4 # both are True

True

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

False

>>> 1 < 2 and 4 < 3 # second statement is False

False

>>> 2 < 1 and 3 < 4 # first statement is False

False

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 using and — -> Result

1. True and True — -> True

2. True and False — -> False

3. False and True → False

4. False and False — -> False

>>> True and True

True

>>> True and False

False

>>> False and True

False

>>> False and False

False.

or keyword::

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 < 2 or 3 < 4 # both are True

True

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

False

>>> 1 < 2 or 4 < 3 # second statement is False

True

>>> 2 < 1 or 3 < 4 # first statement is False

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 using or — -> Result

1. True or True — -> True

2. True or False — -> True

3. False or True → True

4. False or False — -> False

>>> True or True

True

>>> True or False

True

>>> False or True

True

>>> False or False

False

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

>>> False or (False and True)

False

>>> True and (False or True)

True

not keyword::

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

Effect of using not — -> 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:

>>> (not False) or True

True

>>> False or (not False)

True

We can now combine these keywords with the boolean comparators that we learned in the previous section to compare much more complicated expressions.

example:

True and not (1 != 1)

We can break this statement, 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 complicated part (or parts) of the expression and build outward from there.

example:

(“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.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.