Transcript for:
Mastering Logical Operators in Python

Stop before you watch this video, we gotta do this. If you like Python. Cool. And. You enjoy coffee. Continue. Let's do this. Else. I wasn't prepared for this. Why are you here? Welcome to episode six of my Python right now, series, where I'm gonna teach you everything you need to know to become dangerous and Python in this episode. Woo. Get ready. We're gonna get more control over our Python programming. And so far, we've done great job because we've kept evil Ben from coming into our coffee shop, but we have a new problem, evil Ben, the only person we have to worry about now, now we have evil, Patricia. Patricia. Get it together, sweetie. We have a. Show this weekend, evil Patricia is way worse than Ben and we have to keep her from entering our coffee shop. But to do this, we have to gain some new skills skills. We're gonna talk about here in this video. We're no longer looking for just Ben. If name equals Ben. Now we're like if name equals Ben and also Patricia, how, how are we gonna do that? It's a thing called logical operators and yeah, they're fun. So get ready. And the best part is you don't need a dang thing to do this. Just like every episode in this course you'll have access to a free Python lab right here in your browser. So check that link below and get signed up. And uh, oh, you know what? Forgot one thing. You need coffee by now. You should know that. I mean, how else did you get past my, if statement at the beginning of this video, if you're here, you already have coffee, right? And a massive shout out to our sponsor of this entire series. And the reason it can be free here on YouTube, it pro TV. If you really wanna get serious about your it career, or you're like, Hey, I want to get into it. You need to check out it pro TV. They're the, by just by far the best learning platform out there, I use it pro TV to keep myself up to date on all the new technologies and just to learn new stuff, wherever you're at in your it journey. They have you covered from foundational skills. If you jump in there real quick, dude, check this out. That's pretty slick. So it fundamentals. Stick your toe in the water of it. Then build up some Linux skills, routing and switching customer service mean, dude, this is so cool. So for those of you who ask me, Hey, how do I get started in it? Follow this stinking path right here. Because not only will you get to learn a ton of stuff, but you're doing things hands on. They have hands on labs built into this, which is really handy when you go for the more advanced things like, uh, I don't know, Python, if you wanna learn more Python than what I'm showing you here, they have a ton of stuff. Python or networking, Python for security, where to go. Yeah. Right here. And pretty much anything you want to learn in it. So if you're ready to take your it career seriously, and you wanna learn it like me, check it out. Link below, use the code network, Chuck, and you'll get 30% off Forever. So do it now anyways back learning Python. Okay. Let's get started. So if you haven't already opened up a lab for Python episode six day one, and you're like, I don't know where that is. Well then check the link below and get signed up, man. Come on. It's free now. So far, our robot barista bouncer is doing a fantastic job and we did a great job in telling him how to do a great job because in the last few videos we said, Hey, let's do some if statements to figure out and filter through the evil bends. If their name is Ben and they are evil, cuz not all Bens are evil. Remember they can come in. But now, now we got a different problem. Don't we? Because evil, Patricia is on the scene are her robot barista. Bouncer is not prepared to prevent evil Patricia from coming in. So we gotta change that right now. So if we look at our code, we can see that we're already saying, Hey, if the variable name is equal to Ben, if that evaluates too true, then kick him out. Or also if he's evil, but now we have to evaluate, Hey, not only does name have to equal Ben. It also could Patricia then kick her out too, to do that new concept, tie something called logical operators and watch this. This is pure magic and it's so simple, too elegant over here. Let's go ahead and do this together where it says, if name equals Ben right before our colon, let's get a space and then we'll put in this type in or, or check that out as we finish that R it changed colors and now we can type in name equals the string. Patricia. Ooh, powerful. Right? With one simple. If statement we're evaluating, if one thing is true or something else is true. If Ben comes in, dude, this is true. If Patricia comes in, dude, this is true. And now we have the power to kick Patricia out. Let's test it out. Let's run our code. Click on run. What's your name? Let's test our code real quick. Let's say our name is Ben. Ben. I'm evil. Ben. I'm not welcome here. Get out. Let's run it again. Let's test an average person like camera. Cameron's a good guy, right? Oh, Cameron came right in. But now let me stop that script. The beard thing. We'll we'll talk about that here in a bit. Let's run it again. This time we are evil Patricia. So my name's Patricia, are you evil? Yeah, we got it. And again, this is so cool because we don't have to have name equal both Ben and Patricia and know it can be either or so powerful man. So many use cases and I may have noticed, and this is kind of annoying. It's annoying to me anyway, uh, here in our print message. We're like, you're not welcome here, Ben, but that was Patricia. Patricia at this point is super confused. Like I'm not Ben. I am evil. I am Patricia, but I'm not Ben. So real quick use your skills that you've learned so far in this course and fix that for me real quick, pause and UN pause. Let's fix super simple. Right? If you watched all the previous episodes, we basically have to do what we're doing here in our L statement. We have to use the name variable and connate that with our print statement. So I'll do that real quick. Instead of putting Ben right here, we're gonna move all that right there. I'll make that first part of string on its own. And I'll say, plus the variable in name, but should be Patricia or Ben plus the other string. We're concatenating, all kinds of stuff all over the place. Five Ben, Patricia, and we'll yell at her. Get out of here. Good work. Let's try it out. Oh, forgot my closing pro on the print statement. There we go. Run. I am Patricia. I am indeed evil. I was like, wait, what? Why does he Patricia twice? I just realized what I did here. That's what happens when you try to teach and type. Now let me run that. That was weird. Perfect. That works now. So we cover the logical operator or super powerful, but not the only one. And it's a good thing because we have another case we have to look at here. Another requirement for our robot, barista bouncer. The other day, I was watching Lokey on Disney. Plus you ever seen it? It's amazing. And in watching that show, I realized that being evil, isn't black and white look because Lokey does bad things. He certainly is evil, but he also does good things, which that reminds me Lokey is totally evil. So if Lokey ever comes to our coffee shop real quick challenge for you. Let's make sure Lokey that's a different color. Let's make sure Lokey can't come in. Can you add that to our script? Real quick, pause the video. See what you got. UN pause. I'm gonna add it real quick. This is what you would do. Ben or Patricia or? Yeah, you could do another or three or, or no, that's only two, two ORs. I can count. I promise name equals Lokey. Let's try that out real quick. Lokey comes in. Yeah. He's evil. Got it. Okay, cool. So back to my story. Yes. Lokey is evil, but sometimes he does good things and I want to account for that. I wanna factor that in. So essentially here's what I want. We're gonna make this super complicated cuz we have to we're it. People here know I want, if Ben comes in and Ben is evil, he answers. Yes. And let's say if he's done four or more good deeds that day he can come in. Even if he is evil. So how would we do that now? I know things are getting a bit confusing. That's why we're going to flow chart mode. Here we go. Flow charts. Dun dun, dun. It's not scary at all. So here is the logical view of what we're doing in our script. What's your name is your name? Ben. Patricia. And also Lokey. Let me add that. If the answer is no, then come on in. If the answer is yes, let's make sure you're not evil. Are you evil? No. Come on in. Yes. Get out. But now we wanna say with our new logic, how many good deeds have you done today? If less than four, get outta here. You're still evil. But if you've done four or more good deeds, then Hey, come on in. I can work with that. I love me some anti heroes. Yeah. Now this should be a fun one because you can do this in probably a million different ways, but I wanna challenge you to use logical operators to make this happen. I remember so far we've already covered the or logical operator and, but let me introduce a new one to, for you real quick. And I'm not gonna tell you how to use it. Cause I think it's kind of intuitive and I want you to try it out. Just mess with it and break it. Another one is, and I want you to try and use the and logical operator. Oh, and also let me do this. I'm gonna put you into a tighter box on what you can do. Only change this one. If statement to make this happen. Only that one. Okay. For go pause me and see what you got. UN pause. Let's work through this real quick. So here at our task, we pretty much have two things we have to accomplish. First. We have to find out, Hey, how many good deeds have Lokey, Patricia or Ben completed today? And just like their Eagle status. All we have to do is just ask them because why would they lie about that? Duh man, they're they're not crazy. They're just evil. So just under evil status, we'll set a new variable. We'll call it good underscore deeds and we'll have that equal and input cuz we're gonna ask them, Hey, how many good deeds have you done today? And we'll do a little back slash and end to make it pretty, put some space there, but hold up quick quiz. This ain't gonna work. And why and yeah, I did say ain't it's okay. Why won't this work? It's because again, the input function will use a string by default. We have to convert that to an integer. So we'll just wrap that little input by function there with an integer function around it. Perfect. So now how many good deeds they have done will be stored any good deeds variable. So now for the second part of this task, we need to add some logic here. So let's get to our, if statement over here and make some changes. So just after this, yes, I'm gonna put a new operator in. We just talked about it. It's going to be, and so I'll go a and D color change. We know something's happening, magic's happening hit space. And I'll see if Eagle status is yes and good deeds. And I'll use one of our comparison operators less than four. That should do it. Yeah. I think you should do it. So check it out. Here's what's happening in order for this, if statement to evaluate to true both the evil status must be yes. And their good deeds must be less than four. So if they're evil and they've only done two good deeds, then bam, this statement's true. And will print, get your stinking, hide it outta here. You're not welcome. You're two evil for this place, this establishment. And that's what the and operator does for us. It's not just saying this or this either could be true saying both of these suckers have to be true in order for us to continue. So let's try it out real quick. Run this time. I'm just gonna be a lowkey. Why not? I'm lowkey am my evil. Of course. Yes. But how many good deeds have I done today? Let's say we've we've been good. We did seven good deeds today. We made it in. Yeah, we were evil, but Hey, we made up for it. That's how that works. Right? I don't know. Probably not, but now I'll see what happens if we didn't do enough. Good deeds. Let's run it. What's my name. I am Patricia. Am I evil? Of course I am. How many good deeds I've only done two good deeds today. I now welcome here. Less than four D who man, take yourself a coffee break. Cuz right now with the power of Python, we saw the problem of evil kind of, we didn't solve it. We were able to detect if evil people are walking into our coffee shop and now our robot bar a bouncer is doing a killer job, but also real quick, I kind of have a, a brain breaking scenario here. It's gonna hurt a little bit. So we covered the or logical operator and the, and logical operator. But there's another one that I don't really use often, but it's there. But I think it's worth mentioning cuz it'll help you understand if statements even more. And this is the not operator now to talk about this, let's go ahead and launch lab two for this episode, episode six and we got a bit of a clean slate here and here. We're gonna use the same logic as before. If Ben comes in, don't let him in. And we strip things down. It's very simple here so I can illustrate this. My pen. Sorry, evil. Ben strikes again. That rhymed a lot anyways. And I'll just run the code just to make sure it works. What's your name? My name is Ben. Get outta here. Very simple. Now I wanna do something with our not logical operator. I'm gonna throw 'em in right here. Let's try it out. This is so weird. It really is. If not notice that change color. It's a real logical operator. If not name equals Ben, what's gonna happen. Well only one way to find out, right? And also guess what you think will happen. Positive video. Think about it. I'm not gonna wait for you. Let's run the code right now. Run. What's your name? Let's try Ben. Come on in what just happened? Well with our, if statement, what we're saying is if the name doesn't equal, Ben, then do this. If it does equal Ben, then do this. So we just basically did the opposite of what our original statement said. If net equals bend, do this. Now we're saying if name not equal, Ben do this. Now, if that broke your brain a bit, me too, because even now I have to sit here and go, wait, wait, if not, okay, I got it. Now it takes a bit right? And I think it might help if we talk about bulls to make it make more sense. Let me explain this whole time. When I've been talking about controlling the flow of bar Python programming, we've been dealing with if statements, right? We'll say if whatever this junk is evaluates to, what do we say? True. Do something. If the statement's false, do something else. These two bits of data here true and false are what we call boos, which I don't know why, but when I say boos, it makes me hungry and I really don't have a reason anyways. So I'm probably gonna eat after this video. And boos is actually a data type. So let's play with them for a second. Let's do it. So right here in our terminal here, I'm gonna clear my stuff out. Did you know that we can actually just type stuff in our terminal and do Python in real time? So do this. I want you to set a new variable, say the variable me and have that equal your name. So for me, it'll be network check the string network. Check for you. It'll be Bob or, or Ben, sorry, Ben. I set that variable. Bam. So right now, while we're in this terminal, in the context of just being here before we reset it, that's where the variable equals me is you. Huh? So if I call that variable just by typing in me, it calls network. Chuck. Now try this type in me and then do double equal sign equals equals do a space and let's do the ring of your name network, Chuck that we just did before. So the same exact thing before, but we have one extra equal sign. What do you think will be the output here it out. True. How cool is that? Because the double equal sign is evaluating. If something is true or false. And we said, Hey, does me equal network Chuck? And because we just said it there, it totally does what happened. My voice just, I don't don't know what happened now let's try this. I want you to print the type of that data. Now this, if you recall back from one of our earlier episodes, we did that. Let's try it real quick here. I'll say print parentheses type. We're printing our type function. Parentheses me equal equals network. Chuck I'll close out my string of the quote then parenty and I think I need one more parentheses. Yes. Okay, cool. So we're all good. When I print the type of that data, check this out. A new data class and earlier episodes, we saw strings, we saw integers and we saw floating point numbers and all kinds of stuff. I think, think just those three. Now we have the Julian true or false. And that's the data type we're working with when we're dealing with lot of widths. I just said if statements, so now one more thing, try this. I want you to do me once more. We'll do our equals equals our comparison operator and compare that to a false thing. I'll do Nick. I'll do one for you, Nick. Like Nick, I'm not Nick. So when I run that is pairing the me variable to Nick and saying, Hey, is this me variable equal Nick, it should return false. Right? And it certainly does. That's a false statement, but now let's break your brain, hit the up arrow and bring that up again. Put that knot operator in front of it. That not logical operator, not me equals Nick. What will that evaluate to? Let's try it out. True. It's not, it's just a better way to understand it. Play with it, play around with things here. The reason this evaluated to true is we're saying, Hey, is this statement true? Is me not equal to Nick? It's kinda like you saying, Hey, I don't not want coffee. Right? That's kinda, that's kinda what it is in Python. Not me equals Nick. Not one more quick, fun thing. Last thing I promise here in our script, I use the knot to basically do the opposite. Is the name not equal to Ben? There's actually another way to do it. And a more common way to do it. It's actually something I use more often take the knot away, no more knots. And let's work with our comparison operator, remove your equals and let's do exclamation point equal. Sign that right there. Pretty much accomplishments. The same thing. We just use a logical operator. Now we're using a comparison operator to do the same thing. This means not equals. If name does not equal Ben, then that makes more sense. Right? I feel like if saying, if not name, that's like speaking pig Latin, but if we run it again, it does the same thing as not right. Who, okay, one more coffee break. Cuz that was crazy because now don't you just feel more powerful in Python because now you can control the flow of your programming comparison, logical conditional operators to just do all kinds of stuff and don't stop there. I'm gonna have in the link below some challenge labs for you to complete and accomplish because honestly with Python and programming and anything in it, pretty much anything in life repetition will help you get there. It'll cement those ideas into your brain before law. I'm gonna be coding like a wizard anyways. That's episode six. That's all I got massive. Shout out to it. Pro TV, the sponsor of this video, the series. The other reason, this is free on YouTube here. So if you haven't already check them out, if you wanna study it like me and get 30% off forever use code network, Chuck and also, Hey, have you hacked the YouTube algorithm today? Just make sure you do at that like button bell comment. Subscribe. You gotta hack YouTube today. Ethically of course. And yeah, that's really all I got. I'll catch you guys next time.