Operators in Python take operands (also called inputs) and carry out some computation on them. The three logical operators (and, or, not) in Python, when applied to input(s), come up with a true or false value based on the definition of their operation and their input(s). And takes two operands and returns true if both of these inputted conditions result in true. Or also takes two operands and returns true if one or both of the operands result in true. Not only takes one operand and returns the opposite value of the condition that was inputted - hence, it will return true if the given condition is false or false if the given condition is true. These are usually used as part of if-statements or conditional loops and can be helpful if you need to know whether a given condition or series of conditions are true or false.
- [Instructor] Before we get into logical operators, let's review what a Boolean is. A Boolean is a binary variable that has two possible values, and in this case, it's true or false. This is really important to know, because you're going to be using Booleans a lot in your future programming. In this example, we are going to be using the weather, so we're going to see if it's raining or not, if it's sunny or not, and based on those values, we're going to do stuff. Here we're going to have a variable. It's going to be called isRaining, and this will hold a true or false value based on whether it's raining or not.
In this case, we are going to say it's raining, so we'll have that be true. Then we're going to create another variable, it's going to be called isSunny, and we're going to say it's not sunny and the fact that it's raining. And so, these are Boolean values, but what's really special about them is the fact we can use logical operators on them. A logical operator takes one or more Boolean values and operates on them. And in this case, And, Or, and Not, are the main logical operators that we are going to be working with.
And they're super important to know because they can condense your code. And so for the first logical operator, we are going to work with And. And in this case, we're going to be using it in the context of an If statement. So we're going to say if_isRaining and_isSunny then we're going to print out, We might see a rainbow. And so what does this all mean? Well, And is our operator, and we're taking two Boolean values, isRaining and isSunny, and this is kind of like a little truth table that allows us to see what will happen, what will the outcome be, based on what our Boolean variables evaluate to.
And so in this case, if our first Boolean Variable is true and so is our second, then this entire phrase or expression will evaluate to true. And if at least one of them is false, and so that could be the first or the second variable, or if they're both false, then the entire thing evaluates to false. And in that case, since this is an If statement, our print block will not run. And so isRaining and isSunny have to be true in order for our print statement to run. So let's go ahead and run it. And we see it doesn't work because isSunny is false.
But let's go ahead and change this to true. And we see We might see a rainbow because both isRaining and isSunny are true. Moving on to the Or logical operator, we can use this again in an If statement, and say if_isRaining or isSunny, we're going to go ahead and and print It might be rainy or it might be sunny. And so in this case, if they're both true, we evaluate to true. If one of them is false, that's still okay because one of them is true.
For Or, only one of them has to be true in order for the entire expression to evaluate to true. But if both of them are false, meaning none of them are true, then the entire expression also gets evaluated to false and we will not run this print block. And so right now they're both true, but let's say it's not raining. We'll run. We see, It might be rainy or it might be sunny, because at least one of these is true. We don't know which is true because in the context of an Or statement, we just want to see if one of them is true.
It doesn't matter which one. Our last logical operator will be Not, and this will return the reverse of what you give it, and so it is also going to take a Boolean variable, and in this case we'll say Raining. And so, if isRaining is true, then this entire expression will return false. If isRaining is false, then this entire expression will return true. And in this case, the only way that this If block gets run is if isRaining is false, because if isRaining is false then the entire expression returns true, and the print block is run.
And so that means, It must be raining, because isRaining would be false in order for the whole expression to return true with the Not in there. Let's go through one last example that combines everything we've been talking about. So we'll start off by creating a variable called Ages, and inside of Ages we are going to have some numbers that are going to represent ages. And so we'll have 12, 18, 39, 87, seven, and two.
And then we're going to have a For loop that iterates through all of these ages, and so we'll have for, and what iterates means is it's basically going to go through each data point inside of this data structure here called Ages. And so we'll have for age in ages, and so for each data point, each age inside of this list, we're going to go through it and for each age, we want to see if that age represents an adult. And so we're going to have age is greater than 17. If this happens to be true, then isAdult would be true, because the person is an adult if their age is greater than 17, meaning it's 18 or higher.
If the person is seven, then they are not an adult, and age is greater than 17 would return false; isAdult would return false as well. In the next line, we'll use a logical operator Not, and we'll say if not isAdult, meaning the person is not an adult, then we'll go ahead and print Being, plus, and we'll convert this to a string so we can print it out, does not make you an adult.
And so we're saying Being two does not make you an adult, or Being 12 does not make you an adult. So let's go ahead and run this code, and here we go, we have all these ages that are not adult ages, saying Being 12 doesn't make you an adult. So that's how you can use Not in a real life example. So in the next video, we'll talk more about how these comparison operators work.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content