Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody in this video we'll be learning about conditionals and how we can control what statements get executed depending on whether certain values evaluate to true or false and we've mentioned in previous videos that these true and false values are called boolean z' so let's look at one of the simplest conditionals that we can write so we're going to write an if statement and we'll say if and now the condition that we want to check whether it evaluates to true or false so we're just going to make this obvious for now and just say true then we'll put in a colon and hit enter and now in the next line we want to be sure that we're indented over so that we're writing code within our if block so now we'll just print out a string here and I'll just say conditional was true so now if I save that and run it then we can see that it printed out that our condition was true now this print statement will only be executed if the condition after our if statement evaluates to true so what if I was to instead change this over to false now if I run this and we can see that it didn't print out the statement within our if block now conditionals are usually not hard-coded to true and false values like this we really want to put in some code that evaluates to true or false so for example I'm going to create a variable here and I'll just call this variable language and I'll set this equal to Python so now let's say that we only want to execute this print statement if the language is equal to Python so to do this we can say if language equals equals Python now notice that we have a double equals here so this tests for equality now this is different from the single equal sign which is for assigning values so with this double equals here we're testing for equality and this will evaluate to true or false and that will determine if the code in our if statement is executed so if we run this then we can see that it executed our print statement that the condition was true now there are a lot of different comparisons that we can test for and I've got these written out and a snippets file here so I'm just going to copy these and paste them in so that we have them as a reference now we went over some of these and our numbers tutorial but let's go ahead and just look through here really quick so double equals test for equality exclamation point equals test if something is not equal greater than sign is for greater than less than sign is for less than greater than equal to is for greater than or equal to less than equal to is for less or equal and then we have this object identity now some people wonder what the difference is between this and the double equal signs but when we use this is keyword check we're actually checking if values have the same ID or basically whether they're the same object in memory and we're going to look at an example of this in just a bit but right now let's move on to else statements so what if we wanted to execute one portion of our code if our language was equal to Python but another portion of our code if it wasn't so to do this we're going to use an else statement so first I'm going to change my print statement here and just print this to say that the language is Python and now we want to execute some code if the language is not equal to Python so to do this we're just going to put in an else statement and make sure that your else is back here on our base line and not within our if statement so now we'll say else and now within our else block we will print out a string that just says no match so now if we run this we can see that it printed out that the language is Python so our check if the language is equal to Python is evaluating to true so it's printing the code within that block and since it meets that conditional and never executes the code within our else block now if I was instead to change this language and set that equal to Java and rerun this and we can see that it didn't meet the conditional for our if statement so that evaluated to false so then it dropped down to our else statement and executed that code okay so what if we wanted to check for multiple languages and execute different code for each one that we encountered so this is where an l-lift statement comes in so let's say that we wanted to check if the language was equal to Python and if it wasn't then we wanted to check if it was equal to Java and if it was neither of those then we would just print out no match so we'll add our additional check after our if statement by putting in an L if and again make sure that you're inventing is back to this base level because we're no longer in the if block so now I'm just going to copy this code here and say if the language is equal to Java and put in our colon and then we'll grab this and just say print language is Java so basically what this is saying is if the language is equal to Python then execute this code if it's not then run a second conditional and see if it's equal to Java and if it is then run this code and if none of those conditionals are met and print no match so now if we run this then we can see that it executed the print statement that the language is Java and none of these other statements were executed now if you're coming from another language you might be wondering if python has a switch case statement now if you don't know what a switch case statement is then it's not a big deal basically it's just a way to check multiple values and Python doesn't have a switch case and the reason is because the if LF and else statements are plenty clean enough to do this already so if we wanted to check another language then we could just keep adding LF statements so if I wanted to add a JavaScript check to the list and I could just come down here if I copy all of this then I could just do another test here for JavaScript and then say if the language is JavaScript and execute this code here and this basically gives us the same functionality as a switch case from another language okay so now I'm going to remove some code here so that we can take a look at another example now in addition to these comparisons that we have here we also have some boolean operations that we can use and these are and or and not so for example let me create two variables here and I'll call one of these user and set this equal to a string of admin and then I'll create a another variable here called logged in and I'll set that equal to true now let's say that we only wanted to execute some code if the user is equal to add and logged in is equal to true now to do this we can use the and keyword so I could say if user equal equals admin and then we'll use this and keyword and logged in and now we can ride the code if this is true so I'll just print out a string that says admin page and now we can put in an else block and then for the else block I'll just say if neither of these are true then print out the string that just says bad creds for credentials and let me bring this down just a little bit here and just to give us a little bit of extra room I'm actually going to delete these comparison comments up here that we have as a reference but I will have a link to these to the github page so that you can download those if you want to have those as a reference okay so now if we run the code that we currently have and we can see that it printed out our admin page because both of those evaluated to true our user is equal to admin and our login is equal to true but if I changed our logged in variable to false and rerun this we can see that it executes our bad credentials statement because both of these didn't evaluate to true so this user equals admin evaluated to true but log n was equal to false so and make sure that both of these have to be true now if we only care if one of these evaluate to true then we can use the or keyword so I could change this and to an or and now if I run this then you can see that it printed out our admin page statement because that evaluated to true because only one or the other needed to be true and our user was equal to admin so it didn't matter if the logged in was false or not because it only had to be one or the other now this not operation is just used to switch a boolean so it'll change a true to a false and a false to a true so for example if we were to say if not logged in then we will print a string here that just says please log in and else print welcome so we can see that currently our logged in value is equal to false now when we say not logged in then it will evaluate to true because this not just switches that false to a true now I know that that can sound a little confusing but basically you can just think of it as saying not false if not false which would evaluate true so if we run this then it prints out please login because not logged in evaluated to true so it ran what was in our if statement so when I had the conditionals pulled up here as a reference before so now I'm in my snippets here we remember that we had this object identity with this is keyword and I said that we've looked at the difference between that and the double equals which tests for equality now like I said before that object identity test if two objects have the same ID which basically means if they're the same object in memory so two objects can actually be equal and not be the same object in memory so for example let me create two different lists here so I'll just call one list equal to a and I'll put in the values of one two three another list equal to B and put in the values 1 2 3 and now I will print out a double equals B so this should evaluate to true because these two lists are equal so if we run this then we can see that we got true which is what we would expect because a and B are equal but instead if we say a is B and then we run this then that returns false now the reason is because these are two different objects in memory and we can print out these locations with this built-in ID function so right above printing that a is B I'm also going to print out the ID of a and I will also print out the ID of B so I'll save that and run it and we can see that these IDs are different so really this is comparison is really checking whether these IDs are the same so up here instead of creating a new list if I just said B equals a and save that and now we can see that the ID of a and iid of B are the same and then when we print a is B that evaluates to true because now these are the same object in memory and if I check equality then they're also equal so that's basically the difference there behind the scenes the is comparison if you say a is B it's really the same as saying I D of a equals equals the ID of B so if I run that that's equal to true that's basically what the is operator does okay so basically now all the conditionals that we looked at completely depend on what Python evaluates to true or false so let's see what all Python evaluates to true or false and there are a few things that may be unexpected to us so I have a couple of things pulled up here in my snippets and let me grab these really fast so to determine what Python evaluates to true and false it's easier to show everything that evaluates to false and then everything else will by default evaluate to true and we have a quick if else statement here to test all of these so we're going to make a few different conditions here and if that condition evaluates to true we'll print out evaluate its true else evaluated to false and my comments here are all the things in Python that evaluate to false values so the first one is the most obvious if we set a conditional equal to false then it's going to evaluate to false and this one would include all the comparison operations that we just saw so they would return true or false so if I run this then we can see that obviously our conditional evaluated to false here since we set our condition equal to false now the next value in our list here is none so none actually evaluates to false as well so if we put that value in as our conditional and run this and we can see that with our condition equal to none that that condition also evaluates to false now this next one here isn't always so obvious so if you have a numeric data type and pass it into a conditional then zero will die wait to false so if we set this condition equal to zero and run this that we can see that that evaluated to false but if we set this to any other number so if our condition is 10 instead of zero and run that and we can see that that evaluated to true so that's something to keep in mind there when working with numbers because you don't want to accidentally pass in a zero that you think would be true but it evaluates to false okay so moving on down the list if we have any empty syncwords and pass it into a conditional then that will evaluate to false so this can be an empty string an empty list and in the tuple so for example if I just set this condition to an empty list and run this and we can see that that evaluates to false and if you have an empty string that evaluates to false also and now lastly here in our list empty mapping so an empty mapping which is basically an empty dictionary this evaluates to false as well so if I pass in an empty dictionary here and run this and we continue at that empty dictionary also evaluated to false now knowing these types of things can be useful because let's say that you have a function or something that is supposed to return some values now sometimes it's needed to check if those values are actually there so you could just pass in these sequences into a conditional to check whether a string is empty or a dictionary is empty and you don't actually have to call any specific method you can just pass in the empty sequence or the empty dictionary and it will evaluate the true or false if that is empty now there are also some user-defined classes that can evaluate to false but these are the majority of the conditions that will be checking so now that we know everything that evaluates to false and everything else is obviously going to evaluate to true so you know for example just to do another example here if I set this condition equal to a string that just says test now an empty string would evaluate to false so if we ask that in that we can see that that a string with some characters evaluated the truth so that's really important when working with these conditionals is just having an idea of what is going to evaluate the true and what's going to evaluate to fall okay so I think that is going to do it for this video I hope now everyone has a clear understanding of how these conditionals work and all the different ways that Python determines what values are true and false now in the next video we'll be learning about loops and iterations but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those now if you enjoyed these tutorials and would like to support them and there are several ways you can do that the easiest way is to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you
Info
Channel: Corey Schafer
Views: 384,727
Rating: 4.9745603 out of 5
Keywords: Python, If else, if elif else, Python Conditionals, Python Conditional, Boolean, Python Booleans, Python if else, Python if elif else, True False, Python Control Flow, Control Flow, Switch Case, Python Switch Case, Python for Beginners, Absolute Beginners, Python for Absolute Beginners, Python Basics, Getting Started with Python, Python 3.6, Python 36, Python 3, elif, if else python, if statement condition booleon python, python conditional statements, python tutorial
Id: DZwmZ8Usvnk
Channel Id: undefined
Length: 16min 28sec (988 seconds)
Published: Wed May 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.