3.2 Identify true and false statements - Video Tutorials & Practice Problems
Video duration:
8m
Play a video:
<v ->We've looked at quite a few different concepts,</v> and one of the main things that's been happening in the codes that we write, is it just reads from top to bottom. Do this line, do this line, do this line, and then just keeps going until there are no more lines left. And now, I want to introduce the concept of control flow, and it's how a computer will determine, like, a different path to take, and it'll branch off or, like, loop over and all that kinda stuff. So, the first thing we're gonna need to know are how to compare things. So, let's actually see. Can Python compare apples and oranges? So I'm gonna go back to my Python console, 'cause these are all gonna be just single line examples. So, let's see, fruit one equals apples. Fruit two equals oranges. So, the first comparison we wanna make is, are two things equal to each other? And, so, we do that by using this double equals sign, and that's because the single equal sign is already used to assign variables. So, then in order to ask are these two things equal, we need a different symbol. So, that's why we use two equals. Okay, if we had fruit one equal equal to fruit one... Well that will be true, that's expected. One equals one. One equals one point zero. All of that's also true so they don't necessarily have to be the same type, but they should be equivalent. And, what about this? That's also true, so in the back end true and false are also equivalent to zero and one, one and zero. So, I could also say false equals zero, and that would also be true. So, if you wanted to ask are these things not equal, you can use the exclamation mark equal. So, that'll be not equal. So, you can also do less center and greater than. And, less than or equal to. So, one is not less than one, but it is less than, or equal, to one. And, let's see, about these. That's false, so I'm assuming this is true. Oh, that's not true. Okay, so it doesn't, oh know that's fruit. Let's see... Okay, so when I compare things that are unequal, it looks like fruit one is less than fruit two, apples is less than oranges, you know, there's probably some reason it's calculating it that. It might be based on alphabetical order... Yeah, and that's true. Let's see if it even has, like, if it matters how long the string is. No, it doesn't, so it does look like it's by alphabetical order. Those are the, like, numbers are greater than equals equal to operators that we have. But, there are also more operators. There are lots of things that produce, like true or false. Some of them are string methods, so if we had... So, remember fruit one is a string, it's apples. And, all of these ones that start with, "is", actually return true or false. So, is alpha, so is this just alphabetical characters? And, that's true. What about, is numeric, we expect that to be false, and it is. So, you know, you can use these things to say, like, do validation on some input that the user gave. All of our previous examples were assuming that the user input, the thing that we expected, but we can also check that, oh yeah, they actually did put in only numbers and that's what we're expecting. If they didn't, we can give an error. And, besides that, we can also combine things, using "and" and "or". So, one thing is, like, one is less than two, and one is less than, I don't know, negative one. So, that's gonna be false because it'll compare the things on the left side and the right side of the end, and they both have to be true in order for the statement to be true. But if I do "or", that's true because the left one is true and then the right one isn't. It actually, once it finds a true one, it just stops and doesn't even evaluate this one. So, like, I could do this... and this would normally give a divide by zero error. You can see that here, but it doesn't because it stops after evaluating this is true. You can also use, "not". One is less than two and not one is greater than, or less than, negative one. So, that's true because this is false. The "not" makes it true and then both things become true. And then you can do more, you can even use, like, parenthesis to group things and have "and" and "or" in the same line but that's, you know, we won't worry about that for now but it is something you can do. And here is a table of the different values. So, you can see that A and B is only true if both of them are true. And A or B is only false if both of them are false. And then NOT A just returns the opposite of whatever the A value is. So, these are called Logical Operators. The last one I wanted to show you was "in". And, you can use "in"... You can use it for a to see if something is inside of a string. And that's false, but if I asked for, "s", that would be true. Sorry about that, that's also true so you can do sub-strings. You can also ask if it's inside of a list, and we haven't looked at lists yet. But, you can use it to see if an element is inside of a list, and I'll give you a sneak peek of what that syntax looks like... So, what the value of one is inside of this list and the one is right here. So, that's it for operators and, like, the rest of the concepts I have quite a few examples, here in the examples. And this one there is actually, it's split up into Math Boolean and other comparisons.