How to Use If Else Statements in Python (Python Tutorial #2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey CS dojo its YK here welcome to video number two of my Python tutorial for absolute beginners in this video first of all I'm gonna answer a question I received on my last video and then I'm gonna give you some additional commands you can use interpreter notebook for example for adding and deleting cells and remember that you don't necessarily have to use to put a notebook to follow this course you can use for example part charm which is a popular IDE for python after that I'm gonna cover if-else statements how to use them and what they are and by the end of this video you'll be able to create a simple BMI calculator which can tell if a person is overweight or not given the person's height and weight I'm gonna put an outline of this video below so you don't have to watch the whole thing if you don't want to and you can go to CS dojo io / Python - to download the sample files I create in this video alright first of all let me answer a question I received on my last video domani Ostrom sorry for the pronunciation says currently I'm learning web double mint what do you think about it should I keep going or start another language like Python so I wanted to answer this particular question because I thought some other people might have the same question maybe you started learning C++ or Java already and you're wondering should I keep going with it or should I take this course for Python instead I would say keep going with whatever you're doing as long as you enjoy and then if you want you can take this course at the same time because some of the concepts I'll teach are going to be the same regardless of which language you're using so for example the method for swapping two variables I talked about in the last video you can use that in C++ or Java or in any other language you're learning okay with that out of the way let's now talk about Geppetto notebook first of all launch Jupiter notebook through anaconda navigator or with any other method you'd like to use and then navigate to desktop and let's create a new folder here I'm gonna call this python tutorial to navigate to that folder within desktop and click new at the top right corner click Python 3 and let's call this file if-else statements click rename and then I'm just gonna maximize my browser window so the only command we learned in the last video was this button right here for running or executing yourself once I click it it executes or runs all the commands within the given cell this green box right here so if we have hello one here and then hello to here and the print function will execute both of these lines and you might have noticed that after running this cell it automatically adds a new cell below it and what if we wanted to delete the cell for that you can use this button that looks like scissors and for adding cells you can use this button right next to it that looks like a plus button and you can add as many cells as you want and one interesting thing here is that you can execute these cells in any order you like so let's say you have print a in this cell and a equals 1 in this cell you might say well if we try to execute this cell it will give us an error let me actually try it again because a is not defined yet so it'll give us an error saying name error name a is not defined but before executing this cell print a if we execute this cell right below it to define a to be 1 it won't give us an error anymore ok let me now delete a bunch of cells here with this scissors button and let's add one cell below the first cell and let's say we have B equals to here and let's run it with the Run button right here and of course this way B is defined to be 2 now what happens if we delete the cell with this one right here and if we try to print be right here with print parentheses B what happens is actually we can still print it because even though the cell containing this statement B equals 2 is deleted the state of this Jubran notebook is actually still preserved so when I say the state of this Jupiter notebook what I mean is pretty much all the variables that are stored in this Jupiter notebook and the state of this Jupiter notebook is not stored on this browser window because like I say in the last video this browser window is just a user interface for the Jupiter notebook the state of distributor notebook is stored in something called the kernel of this Jupiter notebook that's basically represented by this command-line interface this might look slightly different if you're using Windows and the kernel is what execute your code once you click the Run button and it also stores all the variables that you've defined so to clear out the state of this Jupiter notebook or to clear out all the variables you have defined you need to click colonel at the top and then click restart actually I like to use the command below restart and clear output because it restarts the kernel and clears out everything that's been printed so this kernel has been restarted now and if we try to print B now it will give us an error because name a is not defined yet also remember that there is one kernel for each notebook file so if we had another notebook file and if we restart the kernel for this notebook file that won't affect the other notebook at all because that notebook will have a separate kernel okay so just to recap we've learned how to use for commands in Tibetan notebook the first is running a cell and then deleting cells adding cells as well as restarting the kernel and clearing the output okay let's now delete all the cells we have here and restart the kernel and clear the output and let's dive into our main topic today if-else statements let me give you a quick example here a is equal to 1 and B is equal to 2 and what if we wanted to show a message that something like a is less than B by printing this string only if is actually less than B we can do this with if a less than B Colin so that's if space a space less than space B Colin enter into a new line and print a is less than B and you need to indent this new line by four spaces so that's one two three four you could actually use a different number of spaces for example three spaces or five spaces but using four spaces is a convention in Python so let's stick with four spaces in this video so these two lines if a less than B print a is less than B this whole thing is called an if clause and after this if clause you could have more code for example print not sure if a is less than B and so this if condition only applies to this line in this example and Python knows that because of the colon sign here as well as the four spaces that we have before this print mine so let's try running the cell because a is currently us than B we should be able to print a is less than B and we do and let's see what happens if a is three in that case a is not less than B so this cell should only print this line right here not sure if a is less than B and that's indeed the case and it's actually possible to have multiple lines within the if clause so instead of just having print a is less than B we're gonna write print double quotes a is definitely less than B and in this statement Python knows that these two lines are part of the if clause because both of these lines have four spaces in front of them so let's see if that works by changing a to one again and since a is less than B we should be able to print these two lines again so we see that a is less than B and a is definitely less than B so in a different language you might see an if clause with parentheses and with curly brackets but in Python we're using a column as well as four spaces in front of each line you need to be really careful about the number of spaces in front of each line because if we had for example four spaces in front of the first line and five spaces in front of the second line it will give us an error so the error says indentation error unexpected indent let's fix that by deleting one space here and now we have four spaces in front of each line so it's gonna work again okay let's take a look at a few more examples here the first one is this C is equal to three and D is equal to four if we write C less than D : four spaces print double quotes C is less than D and let's add something outside this if block by writing print outside the if block and once we write this will print this line C is less than D only when C is less than D what if we wanted to add a new line here that says C is not less than D and what if we wanted to print it only if C is not less than D we can do that by writing else : right after the if clause and note that there's no spaces before this else line and insert four spaces before this line to show that this line is part of the else Clause we could also have multiple lines within the else clause and write print double quotes I don't think C is less than D so this whole block says if C is less than D then print C is less than D and if that's not the case or else print these two lines instead so currently C is definitely less than D so we should see this line printed C is less than D and this line outside the if block and we see that C is less than D outside the if block and once we change the value of C to five so that C is not less than D we should be able to see these two lines instead and we do see it's not less on T and I don't think C is less than D take a look at another example here E is equal to 7 and F is equal to 8 so we're gonna write pretty much the same thing after that so if E is less than F then print is less than F and else : print is not less than F so here what if we wanted to deal with three cases instead of only two cases for example what if we wanted to deal with the case where is less than F and is equal to F and is greater than F separately so those three cases we can do that with all F which stands for else if so write L if e double e cosine F newline print parenthesis double quotes is equal to F and let's change what's in the else Clause to e is greater than F so this block now says if E is less than F then print is less than F and if that's not the case check the next condition e is equal to F and not here that we're using the double e cosine instead of a single a cosine so the difference is that when we use the single a cosine for example right here we're assigning what's on the right-hand side to the left-hand side so in Python and in many other languages the single a cosine is an assignment operator now if you want to check if e is equal to F similar to the way we can check if a is less than F we need to use the double e cosine so if you're not sure about which one to use just remember that in this line we're not assigning F to e we're evaluating is that if E is equal to F so this whole block says if E is less than F print this line and if that's not the case check the next condition if E is equal to F then print this line right here print E is equal to F and if none of these conditions are true so it's not true that E is less than F and E is equal to F then else print is greater than F because that's the only possibility that's left here now let's just make sure this whole block works currently E is less than F so we see is less than F and what if is equal to F so E and F are both equal to 8 then we see is equal to F right here and what if E is greater than F so is 10 and F is 8 we see a is greater than F right here and actually after the if Clause we could have multiple L if clauses so after the first L if we can write well if let's say e greater than F plus 10 so this just means e is greater than EF by more than 10 so right now he is not greater than F by more than 10 we should still see he is greater than 10 once we run this cell and we do and once we change e to for example 20 we'll see he is greater than F by more than 10 so there's actually another way to deal with 3 or more separate cases without using the LF clauses so to show you let's define new variables here H and G 7 and 8 so we're gonna write if G is less than H : print G is less than H and so far it's the same but after this we're gonna write else instead of LF and within this else clause we can create a new if clause and say if G is equal to the EE cosine print G is equal to H and after that else colon print G is greater than H so let's see how this block works first we are checking if G is less than H and if that's the case we print G is less than H and if that's not the case we'll go to the else clause and there are four lines in the else clause and if you look at this line the if clause there are four spaces in front of it and if you look at this print line that says G is equal to H that's within the if clause within the else clause so there are four spaces here and then there are four additional spaces here before the print line so there are eight spaces total here and similarly for else there are four spaces here because this else Clause is within this else clause and then there are eight spaces before the print line here because there are already four spaces before the else clause and then there are four spaces after that to show that this print line is within this else clause okay let's make sure this works right now G is less than H so we should see G is less than H we do and if we have G being equal to H we should see G is equal to H and we see that and if G is greater than H we should see this line being printed and we do okay let's now create a simple BMI calculator BMI is basically a number that can tell if a person is overweight or not depending on their height and weight so let's say hypothetically speaking we have a person named YK and his height in meters again hypothetically speaking is 2 meters and his weight in kilograms is 90 now if you only know someone's height in feet and inches and weight in pounds just use an online calculator to convert it so you can just search for feet and inches to meters and you should be able to find a good online calculator for that okay now the formula for finding the BMI is the person's weight in kilograms divided by this person's height in meters squared and to express height in meters squared you could either write height m times height M or you can write height m double star two and this double star just means to the power off and let's print this person's BMI here with print double quotes BMI and then BMI so this person would be not overweight if this person's BMI is less than 25 so in that case if BMI less than 25 let's print this person's name is not overweight and else if BMI is greater than or equal to 25 we're going to print this person is overweight let's see if YK is overweight here okay he's not but what if he had 110 kilograms of weight then his BMI would be twenty seven point five and YK would be overweight okay if you'd like to support me so I can keep making videos like this full-time just go to CS dojo io / PT to find my patreon page and if you want to make sure that you don't miss my future videos like this one just go to CS dojo dot io / news to sign up to my newsletter and if you want to download some sample files from this video go to CS dojo da yo / Python - I'm YK from CS dojo and I will see you in the next video
Info
Channel: CS Dojo
Views: 2,391,984
Rating: undefined out of 5
Keywords: python tutorial, if else statements, how to use if else statements, conditional statements python, conditional statemets, conditionals
Id: AWek49wXGzI
Channel Id: undefined
Length: 19min 44sec (1184 seconds)
Published: Tue Jan 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.