First, I want to talk about logic and in particular I want to talk about Boolean

logic, where all values can either be true or false and there's no gray area. There

is no middle ground. Everything is either true or it's false, okay?

And so, as we talk about this, I want to have some abbreviations for obvious

reasons, abbreviate these as H and Z. So as we go on I don't have to write out true

or false. Okay. So now, all right, I hope at least

some of you are paying attention out there behind your computer.

There is no possible logical reason why I would abbreviate true and false as H and

Z. Let's abbreviate them as T and F, Like normal people?

Alright. Now,

It's all on good to be able to have values that are true and false but it's not too

interesting unless I can combine them in some way.

So Boolean logic includes the operators not, and,

Not and an a or. Now, intuitively, I think you probably

understand these from their definition of the English words of not, end, and or And

you can just kind of work your way through what they neded Alright?

But I wanna be mathematically precise here.

So I'm gonna give you the exact definition of these three operators.

Now, if I have a variable that can take on a value true of false,

Let's call it a. Okay.

Question is what is not A and I'm going to draw a tree table to show you this.

Okay, the tree table looks like this where I show all the possible values of A can

either be true or false. And I'll show you the value of the

expression Not A over here. So if A is true, Not A is false.

If A is false then not A is true. Okay and that probably makes intuitive

sense to you or at least I hope it does. Okay? So let's look at and, and or now all

right. So I don't just have to have one variable

I can have two Boolean variables, Let's say A and B.

Then we can look at what does A and B mean.

Okay? So, I have got more combinations now.

A could be false. B could be false.

Or, A could be false and B could be true. Or, A could be true and B could be false.

Or they both could be true. And that's it.

Those are the four different combinations of A and B.

Now, going to have A and B. Intuitively, you think if A is true and B is true, Then

this also must be true. So, let's put a true down here.

Now, if either of them are false, then I can no longer say that both A and B are

true. Therefore the rest of these combinations

here are false. Okay and since I don't want to draw

another truth table let's just extend this one here and look at operator A or, okay

now, A or B. If they're both false, hopefully, it's

obvious that neither A nor B is true, so therefore the things going to evaluate to

false. But this expression is true in all of the cases because at least one of A or

B is true. Okay.

So if I have the bullion expression a and b, it's true.

If and only if both a and b are true and false otherwise.

And if I have the bullion expression A and or B it's false.

If and only if A and B are both false, and its true otherwise.

Okay. So, hopefully this give you an intuitive

understanding of the different logical operators.

And what I to point out you, don't have to have simple expressions that only include

one operator. I could have for instance have A, and.

B or C and. Not D.

Okay? And so, you can build this up in

arbitrarily complex ways and you have to evaluate them in the same way that we

evaluated the simpler expressions. So you can build a big giant truth table

and you can actually figure out what is the value of this expression under all

possible inputs. So we will actually use Boolean Logic in

our program, So let's take a look at how they work in

Python. Oka? So I can hand variables that take on

the values true or false. So in Python, the word's true with a

capital T and false with a capital F represent these Boolean values.

I can print them and assume and do the obvious thing.

Okay? You can see true and false show up here. I can create logical expressions,

So let's put a little separator there. I can print A or not A.

I can print. A and B. And print A or B. Okay? let's run

this. You can see when A is true and B is false.

Nod A is false, A and B is false, and A or B is true.

This all makes sense, and this is directly from, from the previous slide.

You can go back and look at the truth tables if you're a little bit confused

still. Now, you notice that the.

Boolean logic operators in Python are the English words, not and, and or and that's

kind of nice cuz it's very easy to see what's going on here.

Unlike some other programming languages where they use more cryptic operators for

these things, okay? And I don't have to limit myself to simple

expressions like that. I can do the complicated expression that

we saw on the previous slide. I can print a and b, or c and not d.

Now, I better define. C and D to be something here.

C equals true. D equals false.

Let's run this. And you can see for this set of inputs,

that expression evaluates to true. And I invite you to sort of walk through

it on your own and make sure that you understand what's going on.

All right? So this is how we use Boolean logic inside

Python programs. So Boolean logic is interesting and useful

in and of itself, but you don't always have true and false values lying around in

your program. Instead, you end up generating them a lot

of time. Okay?

And we generate them using comparison operators.

Python has six comparison operators. It has greater than, less than, greater

than or equal to, less than or equal to, equal to and not equal to.

And these six comparison operators allow you to take two values, compare them to

each other and generate a boolean. Alright?

So let's try it out. So let's say A equals seven greater than

three. Print A.

Now instead of just assigning true or false to A we're assigning A to the value

of the expression seven greater than three.

And we know that seven is greater than three so I would expect this to print true

and low and behold it does. Alright?

And there's no reason why I just tapped out a constance there.

I could say X= eight. Y=5.

Five. B=

X greater than or equal to Y. Print the.

Okay. Now I've got an expression involving

variable, involving variables and X greater than or equal to Y.

I would expect that also to evaluate to true, because eight is, in fact, greater

than or equal to Y and again it does what it's expected.

Alright and since I used greater than or equal to here.

I could actually change that to be the same number, it remains true.

If I take out the equal,= , it becomes false. Okay?

Alright. Now, I don't have to just do comparisons

on numbers. I can also do comparisons on strings.

So I could say, C=hello==hello, Hello"" =,, = Hello."" Right?

Print C. What's gonna happen here.

This value is true. Why?

Because the text of the string hello is the same as the text of the string hello,

and that makes good sense. Alright and I want to point out here that

we're comparing the text, not the way you generated it.

If you recall, we can have strings that use either single or double quotes and

that does not matter here. I'm comparing the text.

Okay? So if I actually want to get it to

evaluate to false, I have to change the actual string.

Now when I compare, it's false. Okay?

So it's important to remember I'm comparing the text, not the, the way that

it looks. Okay, with the single or the double

quotes. Now I also want to point something else

out about this expression. Okay.

There is a single equal sign here, and there is a double equal sign here.

This is a source of a lot of errors for beginning programmers, and quite frankly,

experienced programmers as well. The single equal sign is assignment.

Alright? So, this means, take what's on the right

hand side of the equal sign, evaluate it, and put the value into the variable that's

on the left. In this case C,

Alright? The double equals is, is equal. Tell me if

the left and right are equal to each other and evaluate to true or false okay?

So try not to get that mixed up although I'm sure that you will every once in

awhile, and most of the time Python will give you an error alright, when you mess

it up. All right, I don't have to just do numbers

or strings or integers or strings. I can do pretty much any Python type here,

so I could say, hey, is 20.6 less than or equal to 18.3, printd.'Kay so that should

evaluate to false I should hope.'Kay. And this is now a good way of generating

these Boolean values. Using these comparison operators, I can

then combine them, you know, with my favorite expression from before.