# CONDITIONALS & CONTROL FLOW And

#1

Let’s practice with and. Assign each variable to the appropriate boolean value.

Set bool_one equal to the result of
False and False
Set bool_two equal to the result of
-(-(-(-2))) == -2 and 4 >= 16 ** 0.5
Set bool_three equal to the result of
19 % 4 != 300 / 10 / 10 and False
Set bool_four equal to the result of
-(1 ** 2) < 2 ** 0 and 10 % 10 <= 20 - 10 * 2
Set bool_five equal to the result of
True and True

I don’t understand what I’m supposed to do with this exercise. I don’t get how I can copy, paste and get it right.

#2

Your variables should be set to the boolean literals, True or False. Don’t use the computer to solve it, use your thought process.

What do we know about AND? We know that it takes two operands which can be literal or can be expressions. We know that it can only return True when both operands evaluate to True. That means it short-circuits on False. No single operand in the logical expression may be False.

``````True and False  => False
False and True  => False
False and False => False
``````

The last one is moot.

What do we know about negative signs and multiplying? We know that when there are an even number of negative signs, the product will be positive, otherwise it will be negative.

``````-1 * -1 => 1
-1 * 1  => -1
-1 * -1 * -1 * -1 => 1
``````

Let’s apply this to the above by rewriting the expression…

``````-1 * (-1 * (-1 * (-1 * 2)))
``````

Working from the innermost brackets,

``````-1 * 2 => -2
``````

rewriting gives,

``````-1 * (-1 * (-1 * -2))
``````

Again, the innermost brackets come first…

``````-1 * -2 => 2
``````

rewriting gives,

`````` -1 * (-1 * 2)
``````

Finishing with the brackets,

``````-1 * 2 => -2
``````

and finally,

``````-1 * -2 => 2
``````

2 does not equal -2 so that operand is False. Short-circuit, and stop right there. The result is False, no matter what the other operand may give.

Starting to make sense?

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.