 In this video, we're going to learn in detail about how the if statement works. So let's create a new file and rename it into if statement. And let's go ahead and come up with an example. So the example goes like this. First of all, we are given a list of numbers just as we are in previous videos. Then we go ahead and we choose a random number. And then determine if, let's use here a nested list. So if the number is divisible by two only, okay. Also the number is determined, maybe let's copy paste this up here, divisible by two only divisible by three only. The third alternative would be divisible by both two and three. And the last alternative would be divisible by neither two nor three, okay. So this is basically the example. So let's go ahead and construct a list of numbers. And as in previous videos, I'm going to use the numbers from one to 12. This time, I'm simply going to write them down in order because since we're drawing a random number, it doesn't really matter in what order they are. So how do we do that? Well, in another previous video, we saw that there is a random module in the standard library and the random module comes with a choice function. And the choice function takes one argument. So let's give it the numbers list. And it draws, so to say, a random number out of the list, okay. So that is what random choice does. And it is with with replacement, so to say, so in other words, whenever we call random dot choice, we simply get a reference to a number in the numbers list, but the number is not removed. So let's go ahead and assign that number to a variable called number singular. With that, we are going to work. And now what we want to do is we want to write an if statement that basically prints out a nice message determining on which of the four cases there are. So one thing that we should do before we come up with some sort of if logic is probably we should go ahead and create a nice drawing. We could also of course do that in our heads only to see how the problem looks like. So let's say we have all the numbers. And let's say if I wanted to model all the numbers that are divisible by two, well if that is if that here is all the natural numbers, so maybe let's think of that as the mathematical set n. So if that is all the natural numbers, then what we could say is that roughly half the numbers or exactly half the numbers should be divisible by two. And now if I use a different color and I want to show only the numbers divisible by three, it would probably look like this somehow. So here in orange, these are the numbers divisible by let's say three. And we see there is an overlap here. So let's keep that in mind. And let's first of all go back into the problem set and see how we could write that. So let's go ahead and write what we remember from the very first example in this course, the if statement. So let's go ahead and write if number modulo divided by two double equals zero. So modulo division gives us the rest of the division. So in other words, what this basically says if the number divided by two has no rest, so in other words, if it is divisible by two, and then let's go ahead and say print. And let's give out the number that we draw and a nice message and say it's divisible by two only. But the only already bothers us a bit, but we'll see soon how we can how what we need to learn and how we can make that better. So if now I go ahead and run the cell. So whenever I draw a random number that is divisible by two, I see this message. So let's say let's go ahead and copy paste this line. And let's try to formulate a second condition. The number is divisible by three only. And the only thing we need to do is we need to modulo divide the number by three, check if there is no rest, if there is no rest, the number is divisible by three. OK, so if I execute this now, I sometimes when I execute this, I don't see output. So I don't see output whenever I draw a number that does not fulfill either one of the two conditions. But some numbers like a do so eight is divisible by two. Let's go a bit further. Let's execute this a couple of times. And at some point here, when we draw the numbers 12 or also six, the numbers 12 and six are both divisible by two and three. And so therefore we get two messages. So what do we learn from this? We learn from this is that whenever we are facing a situation where we have to model a situation that has more than one case, then doing that with two if statements. It's not a good thing. OK. The reason why is because those two if statements, they work independent of each other. So now the question is, how can we make Python go ahead and only execute one of them? Well, by connecting them together. And the way to do that is by using an elif clause instead of an if clause. So what this means is before that here, I have two independent if statements. But by using the elif clause, I have one big if statement. That has an if clause and an elif clause. Okay. So let's go ahead and see if that works. And now for the number 12, it says the 12 is divisible by two only. We kind of don't like that because now if I go ahead and let's say I want to add a third condition where I check that the numbers divisible by both two and three, how I could do that is by doing the following. I just add another elif clause. I say number, modally divided by two has no rest. And now I use the concept we learned in the previous video, the end operator. So I connect that with a second condition and number divided by three has no rest. Then let's copy paste the print message here. The number is divisible by two and three. If I now execute this a couple of times, we will see that whenever I hit six and 12, only the first print line is executed. Okay. So the number six is obviously divisible by both two and three. However, only the first message is shown. And the reason for that is quite simple. The reason for that is because when there is one if statement like this, where it has several clauses, only the clause that only the first clause that is true has its branch executed. Okay, so we say that this is a condition sometimes also called the entire thing is called a clause and the code blocks within the clauses is what we call the branches that are executed. So now only the first condition that is true leads to the corresponding branch to be executed, but none of the others are executed. So now we have a problem that is basically a problem of order. And the way to fix that is by simply going ahead and copy pasting this condition to the top and renaming the LF clause into if and renaming the second if clause here into LF. If I now go ahead, I'm better. So now for six, it says six is divisible by two and three. Let's go ahead. Eight is divisible by two only. That is good. And nine is divisible by three only works also. And sometimes I don't get output. And whenever I don't get output on my diagram, I'm drawing a number from this empty area here. Okay, so that's basically it. And now what we see in a diagram and that is also why for a beginner in particular, it is always worthwhile to try to come up with such diagrams is what we see here is there are only four cases, right? There is no fifth case in particular. So there's only a case where let's say down here where the numbers fulfill both conditions, the red and the orange one. We have numbers that fulfill only the red condition. We have a couple of numbers that fulfill only the orange condition. And here we have a couple of numbers that fulfill neither condition. Okay, and there's no fifth set here. So what that means is we only have one further case to handle. And now what we could do is we could do the following. We could write another Alice clause and we could say if the number is not divisible by two, so let's simply copy paste this condition and put a not in front of it. Not we also learned in the previous video. And let's connect that together with number divided by three and also negate that with not. And now what we could do is we could also simply copy paste the print message is divisible by neither two nor three. And if I execute this, now I get back a message for every number that I draw, because every number I draw is always in one of the four cases for sure. However, this code does not communicate clearly that there are only four cases because there is for programmers another kind of clause, the so-called else clause. So written like this, simply else colon and then we write dot dot dot here. And the else clause is always going to be last in the if statement and it's always going to be the one that is executed if none of the conditions apply. Okay, if none of the conditions above are true. So if I execute this, in this example, the else clause will never fire because there are always only four clauses and the four clauses here, they cover the four cases in real life. So what we could do is we could simplify this by removing the last elif clause, replacing it with an else. And also, of course, let's get rid of the else here. And now we have not really changed anything. So the program works just as before, but we have now made it clear to the reader of this program that there are only four cases. Okay, so whenever you see an if and an else, you know that there's no additional case that is not modeled here. Okay, so what we learn from this is the if statement in the simplest form can only contain one if clause in a more complicated form. It can have several elif clauses that are checked after the if clause. And it could also have an optional else clause. And all the combinations are possible. So in other words, I can have either the if clause on its own or I can have one elif that also is legal to do. I can have two elifs that is legal to do. I can have, as we see here, if elif, elif, else that is also true working. But I could also simply write if else. This is probably the most common way of what you see in practice. So you have an if condition and then you have the other case. Because that is so common, this has a special name. This is called binary choice. So either we are basically executing the first line, the first code block, or the other code block, either one of them. But one of them will always be executed. That is why we call it a binary choice. But in this example, we have two elif clauses in between. Okay, and it always goes from top to bottom in order. And the first condition that is true is going to be determined which branch is executed. This is the one thing that beginners often confuse. Okay, so what else is there? I'm not going to show this here in this video, but it is actually possible to put an if statement inside an if statement. So we could go ahead and write another if statement inside an if clause here and inside any of the other clauses. This works, but you should always be careful in constructing an if statement such that you can later on still understand what it really does by simply reading it. Don't make it too complicated. Don't put like 10 if statements within each other. This will make code basically unreadable. And yeah, you should keep things simple. So that is what you need to know about the if statement. And yeah, so in the next video, we are going to learn about a similar concept, the try statement, which is kind of like an if statement regarding if something goes wrong in Python. So I see you in the next video.