0:17

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.

Â