Python 3 Programming Tutorial: If Statement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to my tutorial on the if statement as well as assignment operators I've decided to just combine the two since they're used very often together anyway in programming can be useful very quickly to begin adding some degrees of logic to your program and one of the most basic forms of logic that you can use is just the if statement so the if statement is used to check to see if something is the case if it is the case do something otherwise continue along and you know don't do anything under the if statement so one of the most common things that you're going to use with your if statement is going to be some sort of assignment operator so these things are like greater than less than greater than or equal to less than or equal to or just straight-up equal to so let's go ahead and cover a few of those so to start let's say x equals 5 and we'll say y equals 8 and so a basic if statement would go something like this like if X is greater than y : enter and it'll just go down here for you print well if that's the case X is greater than 1 so we can save and run that and we would see here if X was greater than Y it would have done something but that's not the case so it did nothing but if we change this if X is less than Y and we run it this time we'll we got X is greater than Y but that wasn't the truth there we go so anyways it runs the code so the next thing that we can do though is we can add another variable so I'm just going to leave this and when we're going to add a Z whereas a Z equals 5 and now what we can do though is we could say if Z is less than Y which is greater than X what do we want to do well we can print Y is greater than Z and greater than X save that run it and sure enough that is the case because Z and X are equal to 5 and Y is equal to 8 but you're able to do this kind of stuff and what if we did a equals three and then we did something like this a save and run that and yet again we get Y is greater than Z and greater than X we didn't say anything about a but you can see that it was indeed the case here so as you can see this could get really hairy really quick and all we're using is these greater than or less than signs but it turns out that you can also use there's a couple other things that we can do so let's go ahead and cover some of those so at the same time we could say if Z is less than or equal to X what you know do something so we'll say print um Z is less than or equal to X we'll save and run that and sure enough that is the case because Z is actually equal to X they're both 5 so we know that's the case and so we can see here that the less than or equal to sign did its part there's another thing that we could do though let me do this Z is equal to X and you can say if Z is equal to X but you can't do this you can't say if Z equals x because it's almost asking like is that the variable definition or what not so what you have to do is a double equal sign and that means if it equals x so there are some languages where you would you could say like if something equals something and then like the double equals means like is it fully equal so like if one and then string one those would equal each other but then they wouldn't you know double equal each other but in this sense in Python the way that you would do it is if Z equals using double equal signs then we would say Z is equal to X save and run it sure enough that is the case the other thing that you can say is does not equal and the way that you do that is with an exclamation mark equal son so if Z does not equal um x then we would run this but it does equal x so it will run but if Z does not equal let's say Y we can save and run that and sure enough it because it is is indeed not equal so those are the basics to the if statement as you can see it's very simple statement but then you can start adding other things to it and the next video we're going to add to it is the else so stay tuned for the else statement very exciting a change to the if statement so stay tuned for that hopefully you guys enjoyed hopefully you learned something new as always thanks for watching thanks for all the support the subscriptions and until next time
Info
Channel: sentdex
Views: 279,713
Rating: 4.8736844 out of 5
Keywords: Python (Software), programming, tutorial, basics, python 3, python 3.3, python 3.4, python 2 and 3, python 2.7, 2.7, 3.3, 3.4, 3.3.3, if, if statement, Computer Programming (Conference Subject), Python (Programming Language), Conditional
Id: 4u2ClNCtcgY
Channel Id: undefined
Length: 4min 54sec (294 seconds)
Published: Thu Jan 23 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.