How To Check Two Conditions In If Statement In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up everybody my name is zach welcome back to case digital in this video we're answering the question how to check multiple conditions in an if statement in python so without further ado let's hop right in and start coding what up everybody so welcome back like i mentioned in this video we're tackling the question of how to handle multiple conditions in an if statement so i kind of want to give everyone a breakdown of how this video is going to go first for those that weren't with us on one of our previous videos where we talked about creating an if statement in python i want to kind of give a brief overview of how to do that real quick and then i want to go into how many conditions how many conditions can you actually have in an if statement in python and then lastly how to actually handle all of those multiple conditions at once so without further ado let's hop right into the first part so like i mentioned the first part of this video we're just going to recap what an if statement is in the syntax for it and so really quickly this is the basic syntax for an if statement in python um granted a lot of times you'll see it without the parentheses so you'll see something more along the lines of if condition colon and then you do your everything underneath that falls underneath is indented the stuff that's that you want to be ran in the if statement is indented and then you do your code that you want to run so say let's let's just say we want to print out uh hello world so we just say print hello world so now um and we actually have to give the condition variable variable a value condition is equal to true so now if we run this that's going to say like hey if condition print out hello world and so if we do that here we can say python the name of our file which is python examples um we run it it says hello world and why does it why does it run if when it just says if condition because what happens is an if statement it will it'll run the if code under the premise that whatever you're trying to compare to is equal to true so for instance we set condition equal to true so automatically just says if condition so that's true so if true then print hello world if we change this to false and ran it you'll see that we get nothing but now if we change this condition e and we want to say if condition equals false which we know does print this and we get hello world if we change condition value back to true we'll see that this this boolean uh this uh boolean logic or this out this logic here is not true because true does not equal false because that's what we're substituting with that variable and so it won't run so that's our recap on what an if statement is in python and how to create one in python okay so we just recapped how to create an if statement in python we've gone over the syntax the keywords how it all how it all looks and what how it performs so now let's uh before we dive into how we can combine the multiple if statements now we can check multiple if statements um in python let's first answer the question of how many conditions are allowed in an if statement and i think this is really key because for instance the technical answer is one now before you turn off the video and think i'm crazy let me answer why the reason being is an if statement is basically just evaluating some boolean algebra expression whether that's just checking a flag like we did here whether it's true or false or if you want to do something where it's like checking in between saying uh for example checking if someone's age is between one value or another so let's have that example so i can have i can break this question out so say we have a person who's 20 and we want to see if they're above 18 or if they're um 21 or below and essentially how you do that is you can say um hey condition one is this person above or equal to 18 okay perfect condition two is this person below or equal to twenty one now that's all fine and and good and if we run these we'll see that um i can i can do a uh condition one and i can say we'll just have it print the age so if i say python python examples i run this we'll see that we got it it printed out 20 and that's because well condition one is true like i stated here because 20 is greater than or equal to 18. now well okay that's one condition let's go to the other one we try that and if i print this again i get the age because 20 is less than or equal to 21. well technically we wanted to ch check the um we wanted to check to see if a person is between an age and each of these individual conditions um are great but they're only half of the problem so you have to kind of make them you have to kind of combine them like we did in condition three to see if they are true and whether you're just putting in variables like this or you're actually writing out this whole thing you can see that technically i'm only putting in one boolean algebra expression into an if statement and it's evaluating it that boolean algebra expression though may have multiple parts or conditions to it but technically i'm only putting in one condition into an if statement so let's dive into this here in a sec on how you can handle multiple conditions in this statement hey i just want to jump in real quick say thank you so much for watching the video so far if it's providing you value please click that like button below and if you haven't already please subscribe to my channel so we can learn more about software development and programming and well speaking of programming let's get right back to it i know that is probably kind of blowing your mind that you technically only can have one condition in an if statement but let's talk about what you're really asking is when you ask the question of how can i combine um or how can i check multiple conditions in if statement there's essentially two ways you can go about this there's like i mentioned before you can combine them all into one like this you can essentially say hey like let me create um let me have this condition and this condition and and then how do i combine it well you're combining it through the use of and and or and usually or so you can also do or here and what's the difference between the two well and essentially is going to say both of these conditions have to evaluate to true before it will execute this print so if condition one is false that means it's not gonna do it so for instance if i change this um to two you'll see that it's not gonna print out and that's because two is not greater than or equal to 18. um even though it is less than or equal to 21. so when you're when you're talking about checking multiple conditions in if statement you're essentially going to combine all these things together all these individual conditions together you're going to piece them all together until they get you kind of what you're trying to do and if that's checking an age rate uh checking in between an age difference or certain values essentially that's what you can do now if you don't care whether either one certain condition or another is false you can do an or so what an or basically says is if at least one of these is true then execute so even though that's two we run this again you're gonna see that we get two printed out because it didn't meet condition one but it did meet condition two so we're gonna print it and you could do this multiple different ways um you could do another and here with like condition condition number five and then an or and that could get pretty complicated and what a lot of times people will do is essentially though this is kind of where if parentheses and if statements come in where you'll do this type of thing like hey like as long as one of these are true and that means that matches um this condition say six then i wanna then i wanna do this you know print this out that's that's pretty much how you check multiple conditions and if statements another way people go about a lot of times people go about it this way they put it all on one on the first initial condition sometimes though that can get kind of complicated um or can kind of get confusing because you're looking at and be like oh condition one or two and this and this and how does this all work together and blah blah blah a lot of times what people can do as well is you can do if condition one then what people will do they'll do what's called a nested if and it'll say if condition two so if both of these two are evaluated then then print this out and as you can see here we didn't get anything printed out and that's because this is the same thing as doing this right here so this um excuse me this these lines 10 through um 13 are the same as if you just did if condition one and condition two print h now if we ran this again you're gonna see that nothing happens and that's because two is doesn't match this condition and since we're using an and here that says well it doesn't both of them are not going to be true so no use in evaluating this one go on and that's the same thing down here it says this is false so we that's not what we wanted so go down and then if this goes to 20 though you see that we get age printed out twice and that's because this is true this is true based off of up here and what happens down here is it goes okay condition one age is greater than or equal to 18 age is 20. so yes that's true okay next line oh we're gonna we hit another if statement so let's evaluate that that says if age is less than or equal to 21 well age is 20. so that's true okay now we'll go in here and evaluate this and you can do people do like you could do if you know condition three and so on and so forth just as as we did here and sometimes you may have a thing you may have a situation where you want to evaluate hey did i hit a certain value perfect okay now let's combine and condition three down here and then you do something like this so that is essentially the basics on how you check multiple conditions in an if statement essentially you're only creating your disk by taking by having multiple conditions you're basically combining them into one condition that you want it to check um technically and it's evaluating that boolean algebra because technically the boolean algebra or the if statement will evaluate one boolean algebra expression and that can just be made up of modal conditions so i hope that helps if it's providing value click that like button below subscribe if you aren't subscribing and keep on watching
Info
Channel: Case Digital
Views: 162
Rating: 5 out of 5
Keywords: Software, software developer, developer, python, python tutorial, programming, programmer, python programming, how to check two conditions in if statement in python, python if statements, multiple if conditions, if statements, multiple conditions in if statement python, how to check multiple conditions in if statements in python, how to check more than one condition in python if statements, python if, python if else
Id: R5-NMAAl_Ms
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Tue May 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.