Conditional Statements (if, elif, else) in Python - Beginner Python Tutorial #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys and welcome back to the second video in my Python tutorial series in this video we will cover everything you need to know about conditional statements and pythons so there are three conditional statements there's the if statement there's the elephant which just means else if and there's the else statement and we use these statements super frequently when we're programming to allow our code to do different things based off of different conditions so for example if let's say I wanted certain code around if a number was greater than a thousand I could do if number greater than a thousand do this and then I could do else if that number is less than 1000 do some other code I'll push the outline for this video in the comments so if you're trying to find a specific thing look there and also I'll post a cheat sheet to all the commands we've covered in this video in the description if you haven't already make sure to subscribe so you don't miss any of the future tutorials but without further ado let's just jump into this tutorial alright to begin as always open up sublime text 3 and say V new Python file with the name something like lesson 2 dot py so in the last video we went over variables and we show the things like a equals 1 and then printing a would give us 1 down here just wanted to expand upon that and just mention a couple more advanced things about it variables real quick goes to I can print right below it print B and as you can see 1 & 2 I also want to mention that you can also like set a new variable as this was probably shown in the last video but not maybe explicitly made clear because a plus B and that won't give us we print see down here that will give us 3 because it's 2 plus 1 so yeah you can set variables that have come nations of other variable names one thing that you might not know understand right off the top right from the start is that if I did something like a equals five after this it doesn't change what C is once C has been set it's pointing to a concrete number three and this a equals five doesn't change it so even though this Phi this a are here now prints five this B still prints to this see right here was set to one at the time it was added and set so it just prints out three down here so I just wanted to quickly go over that might have made since already to you but in case it didn't I thought it would be helpful to show that all right so let's start with conditional statements if Elif which means else if and else so will will do this with the concept of grades so I mean most people are familiar with grades zero is the worst hundreds of the best and you can get anything in between so we're gonna say we're gonna save a variable called grade here I'll make this a little bit bigger and I'm gonna say like let's say we wanted to print out different things based on what your grade was so start off with grade equals ninety or something like that and we can write the an if statement that prints out like you passed if you got over a seventy let's say so we to do this we use the statement if grade is greater than seventy and then you always at the end in an if statement with a colon and then I can say print you passed so now I'm going to run this and you see down here it says you passed so we can do this is the greater than sign we can also do so if I set this to seventy here it doesn't say I pass so we can also in addition to the greater than sign we can do the greater than or equal to sign you passed a and if you did like 50 here now it doesn't say that we can also do instead of the greater than or equal to sign it's probably a pretty intuitive we do the less than sign so if grade is less than 50 or less than 70 well right you failed come on do better you failed so this is the if statement its if then you have an operator here and then I end with a colon and then you'll have to make sure whenever you use an if statement you have to make sure it's indented properly so you see how this is indented so on the screen it's pushed in one one tab so whenever we're using the Python language to know if something is inside of it is whether or not it's indented so right now I try running this it doesn't work because the if statement by the nature how its constructed expects an indented block so make sure to press tab to put it inside so whenever you want something inside you really you really have to be careful to indent it and I'm always pressing the tab key when I want to indent I don't want to use spaces because it's a little hard to get proper and it will give you some trouble if you're mixing spaces with tab so I recommend pressing the tab key all right so if grades less than 70 you failed and we could say something like if grade is less than or equal to 69 you failed and if I put this as 70 now or something like you won't say that so that's the if statement but you know what right now it's kind of annoying because we're printing out you failed but we're not like we can't print you passed and you failed in the same little stanza I guess so that's where the else statements comes in so we have the if statement and we're gonna also want to have the else statement so that's just whenever you use the else statement you never use a condition so you don't like do like greater than 69 that's for the L if it's not the else so whatever we use else just use else and then seem with if you have to end it with in : so I'm going to change this up again I'm going to just change this back to 70 and it changes to greater than or equal to and I'm going to change this to 90 right now else so basically what this is saying is if grade is greater than or equal to 70 right print you passed and you can write whatever message you want in here you can change up grade to be something else doesn't matter and then else so what else means in this case is if grade is greater than or equal to 70 the else condition would be everything that's not greater than or equal to 70 so another way to say that would be everything that's less than 70 print you failed so 90s as you passed if I change this to 50 control less control B you failed I could change this to something like negative 100 which wouldn't make sense but it still say you failed I could change this to something like a huge number but say you passed so we also might want to have like so one thing we'll move towards is how do we get it to say also whether or not the greatest ballot maybe we would want to like handle non ballot grades because great thought it'd be 2 between 0 and 100 and we'll take care of that in a sec first before we do that I want to kind of make specific the elf's if block so in addition to if we have LF so maybe I would want to say L if grade is greater than or equal to 80 friend you got a a B do better next time life's actually pretty good let's just say pretty good job I don't want to hate on these these are fine totally fine great good job and I'm also going to switch this this would be greater than n equal to 70 I'm gonna make this greater than equal to 90 just to kind of go from big to small so it's gonna be you got in a super I don't know why this facing so we're definitely that's gonna know some people make it better all right so now if I put grade as something like 100 you got an a super if I made it something like 85 that would fall into this Elif block so Ulf block that this is all part of the same execution flow if that makes sense so if I do grade greater than 85 you got to be pretty good job and the difference between it falling into that and like something like 95 is it only executes one so even though the grade is greater than 90 and it's also greater than 80 because it's part of this if/elif/else it's only ever gonna enter one of these so it enters the first one that it valid is valid in and if it's not valid in any condition it will print you failed that would be the else block so if none of these evaluate to true so what happens when you if I was to print something out like this so if I was just a print grade is greater than or equal to ninety and I was just to comment all this out temporarily when I ran this it would just say true so this is the boolean variable that we talked about in the last the last video and so if these boolean czar true it enters it and it enters it if it's indented and also make sure to have this : so the difference between the if and the Elif is that if I change this instead of L left if like another F if I ran the 95 again it now enters both of these so it enters that grade equals not greater than or equal to 90 and it also enters greater than equal to and so if you want it to be separate things and be able to evaluate to true multiple times you'd use multiple if statements but if you only want one thing to ever execute you'd use if/elif/else okay let's see what else should we do also we could switch this up I've showed you greater than I've showed you less than I should be greater than equal to I've showed you less than or equal to another important one is just equal to and so you might initially just be like okay let's just say we got a perfect score and that's all we care about we were super strict with come with how we did and only a hundred was acceptable for us so you might think to just put if grade is equal to 100 print this is an acceptable grade as we're super strict only a hundred is acceptable acceptable grade and then I do like else print you are a failure yeah just if you didn't get a hundred you're a failure totally joking but you get the point just making an example so it gives me this error down here if grade equals 100 gives me invalid syntax so it's like why did it do that that kind of annoys me why I won't accept an equal sign it accepts greater than signs it accepts less than signs so the thing is here this is something super important to remember is that when we set variables we only use one equal sign so the one equal sign is reserved for setting variables if we want to actually use an equal sign as like an operator to compare two things we need to use a double equal sign so now I have if great equals equals 100 and I'll run that and as you can see I remember one more time you are a failure sorry you got a 95 not good enough even if you read out like a 99.999 it's not equal to 100 so it's gonna still say you are failure and so this is equal to and then there's one more operator you know and that will be not equals to so if the grade is not equal to 100 this is very much the same exact thing but we're just switching around the order so now it's gonna run it's going to hit this block so it's not equal to 100 so this is not equal sign up exclamation point then the equal sign is not equals to so to recap that we have four different operators we have or what I'm gonna say four we have less than we have greater than we have less than or greater then we do we have less than less than or equal to greater than greater than or equal to equal to and not equal to so remember these operators are super important and that's the we also went through the flow of if else if and else okay let's uh delete that for a second oh actually one more thing I probably should go over so we're going to go back to the greater then example so for a second we'll do greater than 70 that's and we'll go back to saying you passed and else you failed so one thing that's important to note is that in addition to using these operators that I'm highlighting on the screen we can also use to kind of conjunctive stay I don't know if that was our word at all to two words that can combine expressions so one thing I mentioned before is right now this would accept the grade one how many zeros is that I don't know I'm too stressed out I could pass in okay I'm actually gonna count 1 billion okay I passing the grade 1 billion and it still says he passed I want it to instead say like this is not about grade so I could do something like if grade is greater than 100 print not a valid grade right and I run that and we'll make this see I bring both of them that's because this is an if statement instead of an LF so now it just says not about grade but still this doesn't quite fix our problems so if I typed in negative 100 which no no maybe here you did really badly the test at me you get negative 100 but that's just gonna say you failed right now yes but we'd rather just say the same thing not a valid grade so what I'm gonna do here is I'm going to use a statement called or so I'm going to say if grade is greater than 100 or so or is an acceptable freeze and Python grade is less than 0 print not a valid grade as you can see it says not a valid great hair so that fix our problem so now I can type in only valid 0 to 100 numbers to get you pastor you failed so this order right here one thing to note about it I might have this pop up on the screen I'm not sure but only one of these things has to be true so so either a grade has to be greater than 100 so if I did like a thousand that's true but grade less than zero is not true in that case so true or false works false or true works and then true and true so like if they're both true it still passes the there's a statement also called and so this make means that both of them have to be true so right now it says you pass because and is the improper statement to use here because I would say grade has to be on greater than 100 and grade has to be less than zero okay okay so and means both have to be true or just means one has to be true but both can't be true so that's useful also very useful too now let's move forward if statements they don't just work with numbers they also can work with strings for examples so I could do something like if let's say our name so my name as says in the YouTube description is Keith and make sure you remember when we were using strings these are called strings the actual text is called strings make sure that you surround them with quotations and it doesn't matter if you use single or double quotations both work just fine but you here's how to be consistent so I could say something like name equals Keith if name equals equals Keith then print let's say will print Wow what a cool name so run that and what the heck it didn't work so be careful when you're comparing strings you have to make sure that cat realization is exactly how it is in both things because this doesn't have the same capitalization it didn't actually run this so I'm gonna fix this I'm just going to say if name equals equals Keith and there are ways that I'll cover different in future videos that you can you can like lowercase every letter in a word and how just make sure that the letters are the same that will be in a video on strings now it says wow what a cool name so that worked with strings you could do something like let's say let's say we were making a game and we wanted it to so this have a variable called game over equals true and I wanted I could say something like this to game if game over equals equals true then we want to print [Applause] we'll just say like let's say this game you lost or something right you lost so if I run that says you lost and this is called a boolean but one thing to note with when you're using boolean Ziff you want to have like the proper coding syntax or proper Python like convention is it's redundant to say if game over equals equals true because game over is true right and this if statement works on whether something is true or false so when I before I'm gonna do it one more time just to show you guys when I before did some of them like grade equals ninety and then I didn't print grade is greater than or equal to 85 let's say this whole statement evaluates to true by itself like so if I did that it valuates the truth this is printing true so when I go back and I take this line here if game over equals equals true it's redundant to do equals equals true we should just do if game over now that still works enough game over was false because this is basically saying if game over game over set to true in the last example and so it's basically saying if true and whenever something is true in an if statement it enters it and it always remember indents friender I can't do this won't be proper syntax cool and also note that you could have nested if statements so if we were going back to our grades example I could say greed equals 87 let's say I could have a set of statements that are that look like this if grade is greater than 80 kind of long that will go big to small so if grade is greater than 90 print you got an A and then Elif great is greater than Elif greater is greater than 80 prints you got a B right so I could do that I run it worked oh shoot what happened whenever we have a LF we have to have what is the problem here oh I just said Elif grade 80 I left created greater than 80 putting you gonna be and so in this case it says you gotta be we could nest if statements with within this LF so I could say if grade is less than 83 I'll say print you got a b-minus and I can do the same thing LF grade is less than will say 86 I will say less than 87 actually I don't really know what the technical convention is my high school actually didn't use letter grades really at all my college it's a MIT used mostly numbers - I guess letter grades on our transcript but it was mostly like we just had a s B's and C's not a minus a plus etc L if grade is less than 87 you got a B and then finally else so that only case because this is saying greater than 80 and these are all less than 83 less than 87 the only other case would be like less than 90 so I don't even have to ever specifically write that I can just say print you got a b-plus alright and now I run this I'm gonna okay let's see what happens so 87 you got a b-plus I did 81 you got a b-minus so I did the if statements within another set of if statements and that's totally fine else I'll just print you got a C or worse and this wouldn't handle the those weird cases the above 100 and below zero that I mentioned previously but cool that's if statement we've gone over if stamens we got over L if we've gotten over ELLs we've gotten over nesting them we've gone over down here all the different operators you can have and then I also just want to explicitly write out there's the two statements to combine them and an or also it's worth mentioning that you can specify the ordering of these and and or with parentheses so I'm going to add this to this little comment so what I mean by this is if I delete all of this code up here I could write a statement like this I got it right if false and false or true so we have an an statement you need both things around it or both everything in the end statement to be true when you have an or statement need either one or the other to be true so we'll do print past or something so we're gonna see if this goes through and as you can tell it does go through and the reason for this one to go through is because it does if false and false evaluates that first and that evaluates to false and then it does if false are true and has the one true so it a value eights to true however if I added in parentheses right here I would do if false and false or true this would now evaluate first and false or true becomes true and now it it runs and it doesn't pass and I can show you it doesn't pass so parentheses can be used and are super important and making sure you have kind of can group things together that need to be grouped together and also I realize the dismiss of one other word you can also write not so I could do something like print not true and it'll just do the reverse of true which is false I did the same thing with false not false true and this could be useful in the case that maybe we were playing a game and I had game over equals true or equals false and you wanted something to happen if not game over you know you would like have your keep playing code in the middle here so like keep playing alright so not and or parentheses all of this pretty good all right I'm gonna end this video with a challenge problem I'll answer this challenge problem in the next video I post in this series and the challenged problem is as all those so I want you guys to try to figure out if you can set read a set of conditional statements so if Elif and Elsa's to find out whether or not a number is so-called a special number so and I'm going to define special numbers so write a set of conditional statements to find out whether or not a number is a special number I'll make this a little smaller so you can see all right a special number so I'm gonna say that special numbers are numbers that are less than 100 or greater than or equal to 300 and they are perfectly divisible by 3 7 or both so I'll write that down here so special numbers are defined as number is less than 100 or greater than or equal to 300 that perfectly those about three silver bowl and I want you to do the following if it is divisible by both and it is a special number I want you to print out divisible by both if it's just divisible by three and it's a special number I want you to just do divisible by three so this is divisible by 3 but not 7 and then finally the last one is you I want you to print out divisible by 7 if it's a special number divisible by 7 but not 3 and then finally if it is not less than 100 or greater than equal to 300 or not divisible by 3 or 7 I want you to print out not a special number so this is the task and I'll put this in the description so this challenge problem in the description so try this out and if you could figure it out post your code at the comments here and I'll try to get back to everyone that posts in the comments on whether or not you are correct and what you can improve on I'll also put the answer to this problem in the start of the next video and if you want a hint I'll tell it right now if you don't want the hint just start trying this problem but here is the so a hint is if you want to find if something's perfectly divisible you can use this % so I covered this in the math video so if let's say we had number equals 52 if I wanted to see if that was perfectly divisible by 2 I could do if number modulo divided by 2 equals equals 0 print like even so as you can see it's an even number I switched this like 57 it would not say even and so it's basically just seeing it's dividing it by 2 and looking at the remainder this is just saying if the remainder is 0 so you can use this with 3 and 7 and that gets you good start to this problem alright thank you guys for watching I hope you learned something as always make sure to like this video and also subscribe if you haven't already I'll be posting the next tutorial video in this series by next week so stays to stay tuned for that that's all I got peace
Info
Channel: Keith Galli
Views: 31,432
Rating: 4.9744139 out of 5
Keywords: KGMIT, Keith Galli, MIT, elif statement, if else python, python, python programming, python programming for beginners, python for kids, easy, simple, conditional statements in python, nested if statements python, not equal python, if statements python, python 3, beginner tutorial, beginner python tutorial, python basics, easiest python tutorial, best python tutorial, if elif else, if else if else, simple explanation of if statement in python, 2018, python tutorial for 2018
Id: vsVGPcfxEiA
Channel Id: undefined
Length: 31min 41sec (1901 seconds)
Published: Thu Mar 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.