# 4 - If Statement and Elif Statement in Python - Python 3 Practical Beginner Tutorials

https://www.youtube.com/watch?v=M9pNOkJtByA

[00:00] All right, welcome back.
[00:02] Now it's time to go over the if statement.
[00:05] And just the word if is one of the most important words in programming in general.
[00:10] It's basically making a decision.
[00:12] It's how your computer has, I think it's like flow control is what it's called.
[00:16] Basically, it looks at some condition and then it makes some logical decision based on that.
[00:21] So this is sort of the building blocks of computer programming because right now we're just printing things in this agreement, not really doing anything useful.
[00:28] But now let's do something useful.
[00:31] So first, let's make a coffee mug variable, coffee mug bar, set it equal to a number five.
[00:40] All right, now we're just gonna test that with an if statement.
[00:47] So we're gonna say if, with the keyword if, and notice how it turns orange.
[00:53] If coffee mug bar is equal to five, now notice how I use two equal signs here, that's.
[01:00] Because if I just used one equal sign, it would try to set coffee mug var equal to five.
[01:06] So we already set it up here.
[01:08] Now it's giving us this error because it's like, "I don't know what you mean."
[01:12] Well, what's a colon, first of all?
[01:13] Then it's still gonna give us an error because it doesn't, within this context, within this if condition, it doesn't make sense to set a variable.
[01:24] So that's why it's giving us an error.
[01:25] So we don't want to set it; we want to compare it, so you do that with two equal signs.
[01:27] So now it's saying if the coffee mug var is equal to five.
[01:34] Now see, when I hit enter there, it automatically indented.
[01:37] Python, that's how it works.
[01:40] Python's whitespace sensitive, meaning this indentation means something specific.
[01:47] So in other languages, how you would do it is you would use like curly braces like this and then put your code in here.
[01:55] It didn't matter if it was indented; it could, you could start it here, or you could start it way out here.
[02:02] It doesn't matter, but that's not how Python works.
[02:04] So Python's one of a few languages that use whitespace, and I'm a fan of it.
[02:10] It's that makes it look cleaner.
[02:14] So right now we have our condition, so if coffee mug bar is equal to five, that's our condition.
[02:18] Now what do you want to do if that's true?
[02:21] This is our execution code, or are just the block of code that would get executed if that's true.
[02:28] If that's true, what do you want to do?
[02:30] We're just gonna print, we'll just say print, "Wow, that is true."
[02:34] Okay, now if everything works right, this should print out because it's gonna look at this and say it is five, so go ahead and execute this code.
[02:44] So let's run this.
[02:47] Yep, "Wow, that is true."
[02:47] So if we change this to 50, let's look at it again.
[02:52] Nothing prints out, so that's a basic if statement.
[02:56] So very important just to understand the concept.
[02:58] It's pretty simple, so if that's true, do this now.
[03:03] We're gonna throw with something called else.
[03:05] So else if that's not true, else what do you want to do?
[03:12] And now we're not going to give it a condition.
[03:13] Right now we're just gonna say else print Wow.
[03:20] That's not true, true, that's not true.
[03:25] Sooo we get here, well that's not true.
[03:28] Change it back to five, we get wow that is true, so that's pretty interesting.
[03:35] This is very, very important to understand.
[03:39] I think you guys do, it's not too crazy.
[03:42] So we just said equal, so we could also get used greater than or less than signs here.
[03:46] So if coffee mugs bar is a little greater than five, so if we make this fifty, now it's gonna say is that greater than five?
[03:53] And it should be, so it's gonna print Wow that is true.
[03:57] Yep, now let's make it negative five, well that's not true, okay.
[04:08] So that makes sense, so that's pretty cool.
[04:12] Now I want to cover else if statements.
[04:17] So basically, let's let's just look at it.
[04:20] So let's get that let's get that coffee of our back.
[04:26] If coffee mug or if coffee bar is greater than five, print while that is true.
[04:32] Okay, now let's say e Li F which means else if.
[04:37] Else if, now remember how before we just had that else, we didn't give it a condition.
[04:43] Now we're going to give it another condition.
[04:44] So else if coffee mug var is less than five, looks less than five, print.
[04:56] Wow, that is sure.
[04:58] Actually, let's not get these, that's not used less than or greater than.
[05:01] Let's use equals equals five equals five.
[05:03] So if hopefully give us five, let's make it if it equals fifty or.
[05:12] Equals fifty else if coffee mug var equals sixty.
[05:15] And now you can just chain on additional else ifs.
[05:20] So else if so we don't need that indentation.
[05:24] Else if coffee mug var equal to five, print this is the correct value.
[05:30] Actually we want that to print out.
[05:38] Yeah, we just went wow, that's true to print out if that's right.
[05:44] All right, and the thing with else ifs is at the end of it we can tack on an else and say else print I don't know that's the wrong value.
[05:49] So let's run this and see what we get.
[05:59] We should get negative five here or we should get I don't know that's the wrong value because there's no solution in here where it's correct.
[06:09] So let's run it.
[06:12] Yep, I don't know that's.
[06:15] The wrong value, so basically what happened here is it looked at this, said that wasn't true, so it didn't execute this and just moved on to this one.
[06:25] Try this condition; that wasn't true.
[06:27] Moving on, tried that; that wasn't true.
[06:30] Moved on and then finally got to this last case, this last scenario, and just said, "All right, I give up."
[06:33] There were no cases and my little owl stiff, so we're just going to use this basic default else condition.
[06:37] So, else, just do this.
[06:40] So you might be wondering, how is this different than just having a bunch of ifs?
[06:49] So let's comment this out.
[06:53] Another cool thing in PyCharm is you can highlight the whole thing, go to code, and say comment with line comment for the commenting the whole thing out.
[07:04] So you might be wondering, why can't we just say if coffee mug var equal to 50, print that this is true, if coffee mug var is equal?
[07:18] To negative 5 print this is true and so on and so on.
[07:26] If coffee mugs are equal to negative a lot of keep using negative is equal to 4.
[07:33] Let's make this one for this one 5.
[07:44] This is true one more just just to illustrate it.
[07:49] Okay so with a bunch of ifs here it's going to evaluate every single if regardless of if it was correct.
[08:01] So it's gonna say if coffee mug var is 50 print this.
[08:08] Well it's not so it's gonna move on to this next one if it's for print it.
[08:13] Well it's not sure either so it's gonna move on to this next one if it's 5.
[08:18] It's gonna realize that's true and print this.
[08:19] But then it's also going to go down and try this if again, so it's kind of its kind of wasteful in that sense.
[08:26] So what if this 5 condition, let's make this 150 and make this one 5?
[08:33] So what if this 5 condition was up here and it found it in the first try?
[08:35] The program would still go through all these if, so it's kind of wasteful and redundant.
[08:40] So that's kind of what the else if, how that comes in handy.
[08:45] Doesn't waste code, and sort of in this scenario, all of these statements are tied to this first condition in here.
[08:53] These are all totally separate; they're not dependent on the outcome of another solution or another condition, and that makes sense.
[09:04] So if we say else here, else print I don't know, that else is only tied to this statement here.
[09:12] It has nothing to do with all these, so that's cool.
[09:15] And one other thing before this video ends just.
[09:21] Get a little long, but let's just go over.
[09:23] Is the else if, that's just that's just a shorthand way of writing literally else if.
[09:29] So we could say if coffee mug bar is equal to 50, print this is true.
[09:33] Else if coffee mug bar is equal to 60, oh no, we have to indent that.
[09:53] So else if coffee mug bar is equal to 60, print this is true.
[10:03] Same thing here, now we're gonna say else.
[10:07] Call one if call or now idea with its condition automotive are equal to negative five, print this is true.
[10:16] So you.
[10:24] See, it's nesting, so it would just keep going on.
[10:27] So if you had like 20 of these else ifs, it would be way out here to just get really crazy and hard to maintain.
[10:33] So this else if is just a nice clean way to write it, and it avoids unnecessary code indentation which makes the code sloppy.
[10:43] So just want to let you know that this is the exact same thing as these else ifs; just this is a cleaner way to see it, and you'll probably see it this way.
[10:51] All right, let's cut this off, it's getting a little long.
