What I use to learn (the BEST IT training): https://ntck.co/itprotv (30% off FOREVER) *affiliate link
In this episode, NetworkChuck teaches you how to control the flow of your Python programming using IF/ELSE statements.
FREE Python Lab: https://ntck.co/pythonep4
Support the course: https://ntck.co/pythonrightnow
🔥🔥Join the noob army: https://ntck.co/Premium
**Sponsored by ITProTV
0:00 ⏩ Intro
0:47 ⏩ what you need
1:33 ⏩ Ad read
2:35 ⏩ using IF to control flow
5:46 ⏩ using ELSE
7:10 ⏩ Python SPACING!! (it really matters)
8:37 ⏩ comparison operators
11:30 ⏩ challenge lab
13:02 ⏩ outro
We’re about to use one of the most powerful and fun concepts in Python. And we’re gonna use that power to prevent Ben evil Ben from entering our coffee shop. Cuz we don’t want Ben here. Get outta here. Ben. You know who you are.
This is episode four of Python right now my free Python course, where I’m gonna teach you pretty much everything you need to know to become amazing and awesome and dangerous in Python. So yeah. Get your coffee. Ready? Get your French press. Get your coffee pot. We’re gonna dive in here. And our first task will be preventing evil. Ben from entering our coffee shop. We’re kind of putting in a robot bouncer barista. Now just like episodes one through three and pretty much this entire course, she won’t need a dang thing to do this tutorial. Nothing. I’ll provide a lab for you inside your browser. Check it out. Link below. It’s so cool. And yeah, you’re gonna be doing this hands on. You’re gonna be following along with me on this because that’s the only way to learn Python and programming in general. Pretty much anything in it.
Hands on LABA. Actually. Hold on. I take that back. I said nothing, but you actually do need the most important ingredients in anything in it and that’s coffee because everything in it requires delicious coffee. Never Chuck coffee. I’ll put it this way. If you drink some coffee, you can continue else leave and go make some coffee. Then come back. You’ll get that here in a bit. Now you’re probably watching this episode and wondering, Hey, oh, where’s the rest of it? Why is Chuck producing this course so slow and you’re right. I’m sorry. Cause I know you wanna go deeper into Python right now. You don’t wanna wait for me and you shouldn’t. So check out the sponsor of this video in this entire series. It pro TV, it pro TV is the best it training platform out there. They are what I use to train in it.
So if you wanna dive deeper into Python right now, cuz you should, they’ve got you covered with some amazing courses. Hands on Python for networking professionals. Super cool. By the way, you gotta try that one out. Enter to Python programming. A great starter course. Ooh, check this one out. Python for security. What is that? Okay. Yeah, this is pretty cool. You can create your own network. Port scanner with Python basically create your own hacking tool. That’s amazing and they don’t just have Python. They have pretty much everything. You need to start your it career and then advance your it career. They got to cover from the comp T a plus to my personal favorite, the Cisco CCNA. So if you’re tired of messing around, you actually want to accelerate your career or change your career into it. Now’s the time. Check out it pro TV.
And if you use my link below or network, Chuck, you get 30% off. Hold on forever. So if you wanna study like me, check him out, link below. Okay. Let’s talk about Ben evil. Ben, this guy’s nasty evil. Ben hates coffee and he hates it. He hates technology. We can’t have this guy in our coffee shop. Here’s what we gotta do. We gotta program a robot barista bouncer to prevent him from coming into our coffee shop. So here’s a logic I wanna say, Hey, if Ben comes in a guy named Ben, so sorry, all Ben’s out there, it’s unfortunate that your name is Ben, but what are you gonna do? If Ben comes in, we want our robot barista bouncer to say, Hey, get outta here. We don’t want your kind here. And if their name isn’t Ben, then they’re cool. They can come in.
They can have some coffee and we’ll let them continue on. And this is the powerful stuff we’re learning in this video. We can change what our programs do based on certain things that happen. If Ben comes in, do something different. That’s cool. So go ahead and fire up your Python lab. Again, this lab is completely free right here on your browser or can even be on your phone. So check that link below and get signed up and let’s keep going and go ahead and fire up the first episode four lab. Okay, here we are in our Python lab should look just like this. And this might look familiar. This is the beginning of our robot brief, a script that we worked on in previous episodes. And now somewhere in here we wanna program a robot barista bouncer. Where do you think we should put him keeping in mind that first we have to learn the name of the person entering.
So if they are Ben and we’re like, get here, Ben. So where do you think you should go? I think you should go right around here right after we ask, Hey, what’s your name? Let’s put some space there. Now we’re gonna start with this. Tell hype in if and space. This is a huge concept, which it may not seem like much. It’s just two letters, but it’s huge. It’s referred to as an if statement and let me show you what it does. Check this out. We’ll say if name, the variable name, which we’re pulling from right here, this particular line of code is gonna say, Hey, what’s your name? And the person will answer. And it stored inside that variable name. So if Ben comes in, that name will equal Ben, and then we’ll add this. If name equals equals, and we’ll do a string Ben colon and we’ll stop there, right?
I, I don’t wanna keep going. Here’s what’s happening. What we’re saying here is if the variable name equals these string, Ben, that I want you to do something, do something different. So go ahead and put your cursor right after that colon head enter and we’re gonna do something. Let’s just say print and we’ll print out. You’re not welcome here. Evil, Ben, get out. That should do it. Now. There’s a lot going on, but just real quick, let’s run the code. So at the top, we’ll click on run. Our script’s gonna run. It’s gonna ask me my name. I wanna test the first and say my name is Chuck to avoid the yelling. Okay, cool. Just says, hello, Chuck. Thank you for coming in today. Awesome. Let’s run it again. Let’s see what happened this time. My name’s Ben. Ah, I yelled at myself. You’re not welcome here.
Evil, Ben, get out now. I know it seems like we’re a bit bipolar here because we’re like get outta here. But then we’re like, hello, Ben, thank you for coming in today. We’ll fix that. Now. If statements are powerfully simple, all we’re doing here is saying if whatever comes after it is true, then do something. That’s all we’re doing. So if the variable name does indeed equally string men, then do whatever we say after of that. Now do you wanna see something else? Powerful. Watch this. We’re gonna add one more little building block to this. So right now we’re saying if this statement is true, do something, but we can also make it do something. If that statement is false, watch this new concept time. I’m going to hit enter and do this with me. Enter. Then I’ll do shift tab to step back to where my cursor right in line with the if and I’ll type in else, colon and then enter.
And what I’ll put there is I’ll actually take my other print function down here. Just control X to cut that and paste that right here. So now do you see what’s happen? Oh, so powerful. So now if your name has Ben, if this does evaluate to true bam, we’re gonna kick you out. You can’t come in here, Ben, but if your name isn’t Ben, if this does evaluate to false, then we’ll do something else, else. And that’s the logic. If this is true, do this else do that kinda cool. Right? And we actually just fixed our issue that we had up here being bipolar. We’re like, Hey Ben, don’t come in here. I hate you by the way, come in. I love you. Like that’s really confusing, but we fixed it. Let’s run the code run. What’s my name? My name is Ben evil. Ben, look at Ben.
You’re not welcome here. Evil Ben, get out. And it just said that it didn’t welcome me. Then if I run the code again and I’m not Ben, I’m Patricia, a nice, friendly greeting. Is it that powerful? How cool is that to of all the things you can do. And we’ll talk about that here in a bit, but first let’s pop the hood a little bit on how this is working. Notice our spacing, for example, notice right after our, if statement the thing we want to happen, if this does evaluate the being true, it’s dented, it’s got space right here. And the notice the else, what else will happen is not indented. It’s on the same line as if, but then what we want to happen else. What else is indented after that? This is a key Python concept. Cuz Python does care about your spacing.
It will change how it evaluate your code, or it might just do give up and, and in an error saying, Hey, I don’t understand what you’re saying to me, whereas you’re spacing. So for example, if we did not use any good spacing here, if I were to go in here and do this with me, we’ll see the error together. If I step that back and do the same thing to else run the code. It’s like, whoa, whoa, whoa, ah, we don’t like that. Indentation error, an unexpected and dented block. But thankfully when we’re using code editors like this, they make it easy for us to kind of know when we have to do some spacing. So let’s go ahead and comment out all the code in this editor right now, just so we can cleanly play with some stuff and give you a good chance to practice your commenting out that way.
When we run this nothing runs and we have plenty of room to play. So just below all this mess, let’s practice our, if statements. So I’ll type in. If, and again, I love code editors cuz notice as soon as it became an if like right now, it’s I it’s just white text, but bam, if is something else and the code editor knows that. And at this moment, it’s waiting for us to give it a statement to evaluate. So I’ll try this. Another new concept. If four, the number four and an integer is greater than three colon. I’ve got a greater than sign. We’ll talk about that here in a second. Let’s keep practicing. So the colon means we’re done. If we enter after the colon, bam already indented, which is nice. And then we’ll just print out. Yep. It’s true. And then right here at the end of our print function, head enter and we can still do more stuff.
We could do more print functions. Let’s actually gotta do that. It’s still true. As long as our code is still indented underneath this, if statement it’ll only run, if this is true. So if we run that code, just do it, do it right now. It’s true. Yes. It’s still true. And most practice are L statement just after the second print statement will hit, enter and notice is still like saying, Hey, do you still wanna write some code? If this is true? We’ll say, nah, nah, nah, we’ll do shift and tab to step back and we’ll type in else and notice how it changes color. It’s expecting that. That’s good. It works. We don’t need it, but we can do it. And then we’ll hit enter once more to do something else will print. Nope, not true. Now. Real quick, little small challenge for you. Not crazy.
Make it to where this is a false statement. Just change the if statement real quick, it shouldn’t be too crazy. I mean, all you have to do is this. So go ahead and follow along. I’ll say if two is greater than three, which you know, two is obviously not greater than three. Then that statement evaluate to false. Let’s run the code. Bam. Nope, not true. So we got the spacing down. Now let’s talk about the greater than symbol right here, which is also known as a comparison operator. Big words. You may recognize these from math. I told you we weren’t done with math, but Python, math is fun. Remember? And you’ll see all the fan favorites we got our greater than are less than are less than or equal to and are. Oh wait, I wrote that twice. Let me change that to look, make it look like, uh, greater than you thought you almost had me.
I didn’t make a mistake. I fixed it greater than or equal to oh, almost forgot. There is one more. It was up here when we used it with Ben what’s with the two equal signs. Well, you may recall that when we define a variable and Python, for example, if I define the variable me equal to network, Chuck notice I’m using only one equal sign with one equal sign. We’re setting the variable me equal to network Chuck. But if we throw in a second equal sign, not plus sign equal. What we’re now telling you to do is evaluate if the variable is already equal to network Chuck, if it is, it will return true. And if it isn’t it’ll return false. I don’t know what happened to my true. I need to fix that. That looks ugly. There we go. That’s better now. Quick quiz. What do you think will happen when I run this code? Will I get an error? If you thought so then that’s the case. Watch this run the code. It’s like, whoa, whoa, whoa, whoa. That me variable. It’s not even defined. You’re trying to say it. Is it equal to network Chuck? And it’s like, it’s not equal to anything. You haven’t even defined the me variable. All right. Quick coffee break. Cuz that was a lot. I know. Here we go.
Mm. And one more thing. And then a challenge lab. Let’s go ahead and dive in real quick. Now go ahead and open lab two for episode four. And you’ll notice that we already have our bouncer barista robot or our barista bouncer in there. Here’s the code. If evil Ben comes in BA we got get outta here, Ben. But no, I want you to notice one thing about this and this is a new concept here. Let’s run the code. Let’s run it. What’s your name? Let’s say I am evil. Ben evil Ben coming in hot notice. It tells me you’re not welcome here, Ben get out. But then it’s like, let’s just continue. What kind of coffee do you want? Ben? It’s still a little confusing. How do we fix that? See if you can do it real quick now, hint, it’s not gonna be an overhaul of the entire script, but lemme see if you can get this and, and you can Google it.
Feel free to Google real quick and figure this out. All right, welcome back now had been inclined to go, oh, well the last time we hit this issue, let me stop a script is we just kept adding everything we wanna do in the L statement. Like, okay, well if Ben’s here, kick him out else. Let’s continue the script we could do that. We could nest all of this remaining script under the ELs, but that’s kind of messy, right? We don’t wanna do that. We had all this hard work, making all this look pretty. So as I mentioned before, you can do things in a lot of different ways in programming. One way is after we evaluate the, the name does indeed equal bend and we yell at ’em we’ll hit enter right after that print statement and then type in exit and parentheses. What we’re saying here is say, Hey, if N Eagles bend parent this up and then stop exit the program.
We’re here. Let’s try it out. Click on run. What’s your name? My name’s Ben and bam. The script is done. Sorry, Ben, you can’t have coffee in here. Whew. Okay. That was controlling the flow of your Python programming and that’s just the beginning. Like we have a lot more to cover with that and it gets more insane and more fun. Now, as I mentioned before, I’ve got two additional channel, just two additional labs that you can go through and test your knowledge and get better at this. So link below, go through that. I’ll even have a video walk through, so check it out. So if you enjoyed this video, make sure you hack that YouTube algorithm hit that like button notification, bell comment, subscribe, gotta hack YouTube today. Ethically of course else, those along your statement else. Uh, print I’m a E exit. I don’t know.
I don’t know where I was going with that, but that’s it for episode four, controlling our flow of our code and Python, if else, comparison operators. And like I said before, it only gets crazier. And also thanks again to our sponsor it pro TV. If you want to get into it or advance in your it career, they’re the place to get started again, they are with IU used to study and if you wanna check them out, link below, use code network, Chuck and get 30% off forever. Yeah, that’s all I got. I’ll catch you guys later.