At the end of the last lesson, you saw me allude to a true value variable when we said Tommy is vegetarian is true. In this video, we'll take a look at these true false value variables, which we call in computer science Boolean variables and see how you can use these variables in Python to compare data with each other. Ask questions like which number is bigger? And it also to use these comparisons to answer questions about data. Let's take a look. In the last video you saw me use a dictionary like this one to store Tommy's food preferences. I want to use a boolean variable to store whether or not he is vegetarian. So, I'm just going to comment out this line and this run these three cells like so. And if I now print foods preferences Tommy. We end up with this, showing that is vegetarian is true. And we don't need to store the vegetarianism of Tommy under this dietary restrictions key anymore. True as well as false are special values in Python. If I were to type print true and print false, you might think that this would generate error because I didn't put true and false in quotation marks, but if you run this this is actually valid. What's going on here? If you were to check the type of true, the type of true is bool, which stands for boolean. And we are going to look at the type of false that too is bool or boolean. So you've seen integers and floats and strings and lists and dictionaries. And this, here is a new data type called a Boolean variable or bool for short. Just as integers allow only certain values like negative one, zero, one, two, three, but not one by five. Boolean allows only certain values, and those values are either true or false. If you're curious why this is called a boolean. Let's ask our chatbot companion. Why is it true or false data type called a boolean? Turns out this is named after George Boole, a mathematician who had developed Boolean algebra. And it turns out Boolean algebra is important for computer logic and designing digital circuits. And we'll see later that Boolean values are often using computer programming to help a computer decide, do we take this action or not? Yes or no? You see more on the use of Boolean values in decision-making later in this course. So to recap, booleans are a data that can take on only two values true or false, like the outcome from a coin toss which can be only heads or tails. And you can think of booleans as giving answers to yes or no, or to true or false questions. So for example, if we ask is Tommy my friend? You might set this to true if indeed he is and you say, yes, he's a good friend of mine. In Python we'll often make comparisons between different pieces of data, and when you compare two values, the result from comparing two values is a boolean. Here are some Python operators for making comparisons. So is one thing greater than another? Thing is something less than another thing. And in code this is how you write greater than or equal. You write the greater than sign than the equal sign. This is less than or equal. And something we'll see later as well is use two equal signs to check if two values are equal. So for example, if you want to check if Isabel's age is greater than or equal to, Tommy's is age. You were right age is about greater than equal to age Tommy. And this expression here will return a value. That's either true or false. That is a boolean value depending on whether or not age Isabel is about is greater than or equal to age Tommy. Let's take a look at some examples in code. I'm going to assign is Tommy my friend equals true. And notice that is capital T and Isabel older than me equals false. If I print is Tommy my friend I get true. And if I print is Isabel older than me, I get back the value I had stored in this boolean variable is Isabel older than me. And if I were to ask, was the type of this you fit that this is a bool. Now let's say Isabel, Daniel, and Tommy are playing a game. And they decide that whoever's youngest gets to go first in the game. Here are some things you could do in Python to compare these different numbers. I can check if Isabel is age is greater than Daniel's age and this expression returns a bool. So let me print Isabel age greater than Daniel age. And because Isabel is 28 and Daniel is 30, this should print out false. And yes, indeed that is false. However, we're to print Isabel age less than Daniel's age. Then it is indeed true that Isabel's age, which is 28, is less than Daniel's age, which is 30. So if I run this off the old print out. True. Here's another nifty thing you could do. You can set a new variable is Isabel older than Daniel to be equal to the result of Isabel age greater than Daniel age. So what this does is it will carry out this comparison. See if Isabel's age is greater than Daniel's age, which is not. So this expression that I've just highlighted should give us the Boolean value of false and this line of code will therefore set is Isabel older than Daniel to be equal to false. So let's run this and hopefully this prints out false. Yes it does I use greater than and less than up there. You can also use the less than or equal to operator. So is Isabel's age less than equal to Daniel's age? Yes it is because 28 is less than equal to 30. Is Tommy's age less than Daniel's age. No it's not because they're both 30. So this should be false. By the way, to answer this Tommy's less than or equal to Daniel's age. That because Danny and Tommy are both 30. This should return true. Feel free to the pause a video and we do this with greater than and greater than or equal to and see what results you get. In addition to testing for less than or less than or equal to or greater than or greater than equal to, we can also test for equality. So what do we want to check if two things are equal? It turns out in Python, the way to do that is to use the double equal sign. You see, we use a single equal sign to assign a value to a variable. A double equal sign is a different operator than a single equal sign, and it tests if two things are equal. So for example we can do print Tommy age equals Daniel age. And because Tommy and Daniel are both 30, this should return true. Whereas if I were to see if Isabel and Daniel the same age, they should print false. And please be careful about single equal sign and double equal sign. One common bug in Python is to write a single equal sign like this, where you meant to use a double equal sign, and this actually take the value of Daniel's age and overwrite Tommy age with the value there and to lead to some pretty unexpected effects. So be sure to use a double equal sign if you're actually trying the test of two things are equal to each other. And by the way, you can use double equals as well to test for equality of strings. So, is vegetarians equals a vegan. Maybe this is not a good way to test these two concepts are the same. But what Python is doing is looking at the string vegetarian and checking if that string is equal to the string on the right, and in this case it is not. Whereas if I were to run this is vegan equal to vegan. And this would be true. And you can imagine that testing of two strings are equal or not equal to each other is an important part of how computers check if a password you just entered is equal to the correct password. Because that involves testing if two strings are equal to each other. By the way, if you ever build a high-security password checker, there are some other steps that are recommended to make it more secure. But testing whether two strings are equal really is one of the steps. Using password checkers today. Now, let's say I'm hanging out with two people, Tommy and Isabel, and I have to saved two variables: is Tommy my friend, is true. Is Isabel my friend is also true. And we want the computer to ask are both Tommy and Isabel my friend? How can you do that? It turns out Python has a logical operator called the "and" operator. So you can ask Is Tommy my friend? And is Isabel my friend? And there are printers. This turns out to be true. If for some reason Isabel didn't want to be my friend anymore and that were false, then this would become false, because it's no longer true that both Tommy and Isabel are my friends. Wait. I don't like that. Let me change this back. All right. Much better. So these are the most frequently used logical operators in Python. And it's true. If both of the inputs are true and all is true if at least one is true and this is an example of the and statements that you saw in the code just now. Just illustrate some other examples. Is Tommy, my friends, or Isabel, my friend. This is true. And again if Isabel doesn't like me anymore, then where they're both all my friends is false. But whether these one of them is still my friend, that's remains true. I know I just went through a lot of boolean logic here. If any of this doesn't fully make sense, I encourage you to pause the video. Try running the code yourself with different true false values and see what you get and also feel free to ask AI chatbot companions explain these results to you if part of it still doesn't make sense. Just to wrap up this example, if Isabel, Danny, and Tommy are playing a game and these are the ages we had defined previously, then we can set boolean values to be: is Isabel younger than Tommy? This is my younger than Daniel and then to see if Isabel is the youngest, so she gets to go first, we can say is Isabel younger than Tommy and is Isabel younger than Daniel? Because if this is true, then she is younger than the three. And yes, indeed she is. So she can still go for us in this game. Now we've learned about boolean values you be able to in the next video, use boolean values to help AI make decisions where it'll be able to compute whether something is true or false, and then depending on that value, decide what action to take. Let's take a look at how to do that in the next video.