In this lesson we will talk about Booleans--True and False values.
So let's create a new variable called myBoolean and assign a key word called True. Pay attention because we have to use it with a capital T and we can't use it quotation marks because then we will be creating a string, and that's not what we want. We want the True value.
Here I got an error because I didn't capitalize very well, so pay attention because Python is very case-sensitive. Alright, so as we can see, the type of myBoolean is 'bool' meaning we are dealing with a Boolean.
We also have the false value:
Have you asked yourself why we need true or false values? This is one of the most important things about programming. All the logic of our programs will be based on these values.
But what we just did, manually assigning true or false values to a variable, is not very useful. Actually, those values are going to be generated when we make comparisons, so let's create two variables: number one and number two, as num1 and num2. This > is the greater than sign.
And it returns True because yes! 8 is greater than 4 mathematically. So let's do the opposite now by asking if num1 is less than num2:
We just used two comparison operators. Let's take a look at other comparison operators we have in Python. Let's look at other comparison operators we have in Python:
1. > Greater than
2. < Less than
3. = = Equal to. Don't forget to use two equal signs when writing your code.
4. != Not equal to
5. > = Greater than or equal to operator, which tests two conditions at the same time. So if the first number is the same as the second it returns true and if the first number is greater than the second number, it also returns true
6. < = Less than or equal to
So let's go back to the code and make some more comparison:
Alright, the answer was False.
Let's change the value of num2.
Now let's look at how we can use Booleans inside conditional statements. So let's start a new program by clicking a new file and minimizing the page.
Now, let's ask the user for two numbers.
The ''if'statement is conditional. This is how it is used: we use the keyword "if" and inside parenthesis (), it is expecting a true or false value. We can do that by entering a comparison there. So let's compare num1 to num2.
So if the highlighted value is true, it is going to execute the print function.
Now let's go to the next line (break a new line) and get out of this indentation.Type another line of code: print (This is out of the if structure"). Note that this is for testing purposes.
Now let's see what happens when we run this program. Don't forget to save it as 02_booleans.py and run it.
And our program is working.
Output:
It only executed this message, "This is out of the if structure," because this is outside the if structure.
This message: print(num1, "is greater than," num2), was not printed because this comparison: if (num1 > num2) resulted in false. So if we have a false value here, that code is not going to be executed.
Let's erase "this is out of the if structure" because we already know what happens and run the code again.
4 is greater than 2. This is how conditionals work! We can also use an "elif" statement to create more conditions. So after the if, let's go back and start an elif statement. The elif is part of the entire structure, but it has another condition.
So how does this work?
If this (num1 > num2) is true, it's going to execute this code: print(num1, "is greater than," num2), and it's going to ignore all the rest: elif(num1 == num2):
print(num1, "is equal," num2) # Let's use 'else' if none happens.
else:
print(num1, "is less than",num2)
However, if this (num1 > num2) is not true, it is going to continue in the structure. So then it is going to test the ELIF statement. And if that is true, it is going to execute this code: print(num1, "is equal to," num2) and ignore the rest of the code.
But if this one is also not true in any other case, this is the code that is going to be executed:print(num1, "is less than," num2)
Let's save this and test out the program.
Okay, this was all about booleans for the moment. We are going to explore more details on how conditionals work later in the course, and that's when we are going to start writing real programs. I can't wait! Leave your questions in the comment section. I'll see you soon.