So, so far we've seen two ways of creating Booleans.
We've seen literal expressions That's either true or false.
We've seen comparison operators,
That compare two values.
So there's equal to, not equal to, less than, less than or equal to, greater than,
greater than or equal to.
And all of these compare one thing on the left with the one thing on the right.
And the value of the overall expression is going to be true or false, again,
a Boolean.
Now, there's another way to create Booleans, which are logical operators.
So like comparison operators, logical operators expect one thing on the left and
one thing on the right.
So the logical operators that we'll cover, and, or, and not.
So and, and or, just like comparison operators,
expect one thing on the left, And one thing on the right.
Not, only expects one thing that I'll call B short for Boolean.
Unlike comparison operators where the thing on the left and the thing on
the right can be an integer or string or pretty much any time logical operators
expect the thing on the left and the thing on the right to be Boolean expressions.
So, the value of L and R is going to be true if both L and R are true.
The value of L or R is going to be true if either L is true or
R is true, or both are true.
And, the value of not B is going to be true only if B is false.
In order to make it clear how these logical operators work,
I'm going to draw what's called a truth table.
So, I'll start by drawing the truth table for, and.
So, remember that L and R are both Booleans.
And, so L is either going to be true or false.
So I'm going to represent whether L is true or false in columns.
So here, the first column is going to represent L being true.
And the second column is going to represent L being false.
I'm going to represent the value of R in rows so
R being true is the first row and
R being false is the second row.
So every cell represents the combination between L and
R values represented by that row and column.
So here, this cell represents L is true and R is true.
This represents L is true and R is false.
This represents L is false and R is true and this represents L is false and
R is false.
And I'm going to write the value of L and R in each cell.
So here if L is true and R is true then L and R is true.
If R is false, but L is still true, then the value of L and R is going to be false.
And if the reverse were true, so if L were false but R was true,
then the value of L and R is going to be false.
And if both are false, the value of L and R is going to be false.
So you can see that L and R is true only if L is true and if R is true.
Let's write out the tree table for or.
So just like and, we have L and R.
So we have L true, L false and
we have R true, R false.
So if the value of both of these is true then the value of L or
R is going to be true.
If L is true and R is false, then L or R is true.
Same thing with if R is true but L is false.
The only way that L or R is going to be false is if L is false and R is false.
So you can see L or R is going to be true if either L is true or R is true.
And then the table for not is a little simpler.
So if B is true
If B is true, then the value of not B is going to be false.
If B is false, the value of not B is going to be true.
So you can see that not B is only true if B is false.
So, when we write out logical operators,
you don't need to write out these truth tables entirely.
But, I use these truth tables as a way to understand systematically how and,
or, and not work.
So, let's look at some code.
In this code, we assign the variable x to have the value five, and
we print up X greater than 0 and X less than 10.
So, here on the left, we have one Boolean expression.
And, we have another Boolean expression here on the right.
What that means is that the value of this overall Boolean expression is
going to be true only if this expression is true and this expression is true.
So we can check, is x greater than 0?
Well x is equal to 5 and so yes 5 is greater than 0.
So this is true.
Is x less than 10?
Well, I see that x is 5 and yes 5 is less than 10.
So this is true, and what that means is that if this is true and
this is true, then we're here in our truth table,
meaning that the value of this overall expression is true.
On line four, we assign a variable n to have the value 25.
And we print N modulo 2 equals 0.
Now remember that modulo means remainder, so
what this is saying is what's the remainder of when we divide n by 2.
So when we pick modulo 2, we're either going to get 0 if it's an even number,
or one if it's an odd number because even numbers will divide by 2 and
have no remainder.
Odd numbers will divide by 2 and have remainder of 1.
Here we see that n is 25 which is an odd number,
so n modulo 2 is going to be 1.
If we take N modulo 3, Matt's asking is there a remainder when we divide by 3 And
we should actually have a remainder of 1 when we divide by 3.
And so what that means is that this expression, n modulo 3==0,
the value of this expression is going to be false because it's
comparing 1 with 0 and checking if they're equal.
And n%3 == 0.
The value of this overall expression is also going to be false
Because 1 is not equal to 0.
And so when we ask false or false, then we fall here in the truth table.
And false or false is going to be false.
So I'm printing out true and then I'm printing out false.
Let's check our work.
So you can see we get true, and then false So if
you recall from a previous lecture, Python has a notion of operator precedence.
So at the highest precedence Is when we have parentheses.
And then just below that is exponentiation.
That's when we say something like x**2 which stands for x squared.
Below that, we have multiplication and division.
So that's x times 5 or x divided by 5,
then we have addition and subtraction, x plus 2.
And so our comparison and
logical operators do fall in this order of precedence.
In fact, comparison operators fall just under addition and subtraction, so
here we have comparison Like equals equals
And then below that, we have the not operator like not x.
And then below that we have and or
or so that's like x and y.
What that means is that if we have an expression like x greater
than five and y less than two.
Then Python is first going to evaluate this expression because
this comparison between x and five has a higher precedence than the and
And then it's going to evaluate this overall expression.
That's all for now until next time