0:06

So we've learned about boolean values and boolean logic.

But, you may be asking yourself, well,

how do I actually generate these boolean values?

Where do they come from?

All right, well, that's one of the focuses of this video.

One of the primary ways in which you generate

boolean values in programs is by doing arithmetic comparisons.

I have some value say,

my age, and I want to find something out about it.

Like, am I younger than 30?

So I want to say, hey, is my age less than 30?

Well, I'll leave it to you decide the answer,

but, let's figure out how we might write a program to do it.

This is a simple program that demonstrates the use of Python's comparison operators.

Python has six of these comparison operators.

It has >, <,

>=, <=, ==, and!=.

Now I can use these comparison operators to do arithmetic comparisons, alright.

And, so, I have an expression here that compares two numbers,

alright, 7 > 3.

All right. Well, is seven greater than three?

I'll leave that for you to figure out.

But, what's happening now is when you use these comparison operations,

they evaluate to boolean values.

So this will evaluate to either true or false.

Hopefully, you've already figured out what it should be, and let's find out.

Okay, if you knew that was true,

and it did evaluate to true,

so when you prints it out, you get true.

So, let's check Python. Doesn't know what's going on here.

I have seven and three and I'm trying to compare them in different ways greater than (>).

Says, that's true, makes sense.

Less than (<) says that's false. Also good.

Greater than or equal to (>=), true.

Less than or equal to (<=), false.

Okay, it does make sense. Is 7==3?

No. So I should get false.

And is 7!=3? Well, yes it is.

So, I should get true. All right.

So Python knows how to do these comparisons and hopefully you do too now.

Now, we already knew that 7 was greater than 3, right?

So, there's really no point in writing 7>3 in your program.

You could just use true. Just put true there. You already knew that.

Okay, so, comparisons are actually

significantly more interesting when you're using variables, okay.

And these variables you might not know what their values are.

And that's the whole reason why you're comparing them, okay.

Perhaps they came from,

you know someone typing something at the keyboard,

perhaps they were computed elsewhere in your program and you

don't know exactly what got computed, okay.

Here we have num1 is assigned 7.3 and num2 is assigned 8.6,

now we're using floating way values now.

Comparisons are not limited to integers so we

can do arithmetic comparisons on floating point numbers as

well and we have the expression greater = num1 > num2.

Okay, so I want to check is num1 larger than num2?

And we're going to print out the result.

Well, let's check that out.

Okay, evaluates to false.

All right. So, the point here is that I don't usually do comparisons on constants.

I do comparisons with variables,

although I might have one constant right.

I might be checking, hey,

is age less than 30, for example.

I hope you get the idea that you can use these six comparison operators to

do arithmetic comparisons on values in your program to generate booleans.

These booleans can then be used elsewhere in your program as necessary.

Now I want to point out here that

these comparison operators are not just restricted to numbers.

I can also use == and!= On other Python types,

and this can be valuable in different situations in your program.

So let's take a look at using strings.

Okay. I have the string Scott here,

and I want to check,

and I've assigned to the variable,

name, and I want to check if name is not equal to Joe.

Hu hu hu, I hope not.

Okay, and so let's check this out.

All right. Hopefully Scott is not equal to Joe.

So I would expect this to print out true,

and in fact, it does.

Okay. I could change it and, you know,

also check is this name equal to Joe.

Let's run that here, and thankfully I get false.

Right? Now I want to point out here,

when you're doing this kind of comparison on strings

you're actually comparing the text of the string.

You're not comparing how the string was generated.

So I could change these double quotes here to

single quotes and that shouldn't really change anything.

Okay, if I run this again.

I should still get false,

and I do. All right.

Now if you're paying attention you should say hey that didn't do anything useful.

Right? That doesn't tell me anything,

because you know Scott is still not the same as Joe.

Doesn't matter if the're single quotes or double quotes around that.

So, let's do this.

Right now I'm checking if the string Scott that was generated with

single quotes is equal to the string Scott that was generated with double quotes.

And in fact, I do get back true here.

Don't worry about how the string was generated,

you're worrying about comparing the text to the string,

which is what you really want in

any situation in which you're doing these kinds of comparisons.

Now, I want to point out one last thing here.

Beware of the fact that

the equal comparison operator is

two equal signs and the assignment operator is one equal sign.

This can be incredibly confusing especially to beginners.

Right. And you can mix this up and type the wrong thing in

the wrong place and mess up your program.

Now usually Python will catch it and give you an error, but not always.

All right. Just remember a single equal is assignment.

Right. That means you're trying to assign the value of the expression on

the right of the equal sign to the variable on the left of the equal sign.

And a double equal sign is comparison.

Right. Is equal to?

You're trying to check if the thing on the left is the

same or equal to the thing on the right.

Don't worry. You will screw this up.

Even experienced programmers screw this up occasionally.

Hopefully, though, it won't cause you too much trouble.

These comparison operations, they evaluate to boolean values, remember, okay.

So we can use them in boolean logic expressions as well. All right.

So, let's do that.

Okay. We have, you know the variable greater up here.

That is now a boolean value because of that arithmetic comparison,

and we have the value of the variable equal,

that is also a boolean value because we checked for equality on two different strings.

And so, I can just use these now in a boolean logic expression.

Say print greater or equal,

and let's find out what happens here.

We get false, right?

Because greater was false, equal is false.

And so the expression greater or equal is false or false which we know is false, alight.

So we can combine these arithmetic comparisons and other boolean logic operations up into

creating much more complicated boolean logic expressions. So there we have it.

That's how we use the comparison operations in Python to generate

boolean values and we can take those boolean values and combine

them up into logical expressions, right?

Now, we still don't know the answer to whether or not my age is less than 30.

But, you know, maybe we could kind of write a Python program to figure it out.