In the previous part of the course, we saw that a program is just a sequence of instructions that the computer executes one after the other. Sometimes we might want to execute one instruction in one case but a different instruction in a different case. This is known as conditional execution. In this case, the program decides which instruction to execute next based on the result of a previous operation. In Python, we achieve this using what are known as conditional statements. The simplest type of conditional statement is an if-statement or what we called an if-then statement' when we were looking at the Von Neumann architecture. In this case, we do something if the condition evaluates to true, but if it doesn't evaluate to true, then we don't do it. As we've seen in the flowchart, we'll assign the variable larger to hold the value stored in a variable called a, and then we'll see if b is greater than that value using the greater than operator. If b is greater than larger, then over here, we'll assign larger to hold the value of b, but otherwise, we just won't do anything. In either case, when we get to this part of the flowchart and we print it, it will print the larger of the two values. So, now let's see the Python code. First, let me introduce these two lines up here. They start with the pound sign or the hash character and are known as comments. They are there in the code to tell the human reader what this code does. They're ignored by Python but are very useful for communicating to someone else what the code is doing. As we've seen before, we'll declare and initialize a and b to 25 and four respectively, and then declare and initialize larger to hold the value of a. Now we have our conditional statement which starts with a Python keyword if and is followed by the condition to evaluate. Here we're using the greater than operator to see if b is greater than larger. Note that we follow that condition with a colon. The expression b greater than larger is a Boolean expression. It evaluates to either true or false and we use it to decide whether to run the code in the body of the if-statement. So, if it's true, we'll execute the body of the if-statement which is indented. It's supercritical to understand that the code that is indented is considered to be part of the body of the if-statement, and if it's not indented, it will just be considered regular code. So, here we indent the line larger equals b because that should only run when b is greater than larger. The print line here is not indented because that line of code runs no matter what. It's worth pointing out here that this line would print 'larger is 25' because it will replace the variable larger with its value when it's printed. There are six comparison operators that we can use in Python as part of conditional statements. Note that when we're comparing two values, we use the double equal sign because the single equal sign is the assignment operator. That's the one that's for setting one variable to the value of another but not for comparing them. Let's see another example where we want to know whether one number is divisible by another. To determine if one number is divisible by another, we can use the modulus operator that we saw last time. This operator returns the remainder when one number is divided by another. If the remainder is zero, then the number is divisible by the other. So, we can calculate a modulo b or a mod b, and then see if the result is zero. As a reminder, note that we use the double equals to check for equality not single equals which is assignment. As we saw previously, the print is indented because it's in the body of the if-statement. Again, note that this would print the value of a followed by the words is divisible by, and then the value of b. As we saw in the last example, this last line is not indented because it is not run conditionally, it runs no matter what. In this example, we printed a is divisible by b conditionally. But what if we wanted to print a different message if a is not divisible by b? That is, we want to execute one statement if the condition is true and a different one if it's false. In that case, we would use an if-else statement. We still calculate a mod b and compare it to zero, and if it equals zero, then we print a is divisible by b. But if that condition is false, then we'll do this else block. The keyword else indicates that this is the code to run if that condition evaluated to false and represents the no arrow coming out of that decision point in the flow chart. Now, this comment is here to remind the reader that we're doing this because a mod b is not equal to zero. In this case, we print a is not divisible by b. Note that both of these lines are indented together because they're part of this else block, and as before, no matter whether a was divisible by b, we'll print the end down here. Now we've seen how to execute a statement if one condition is true, and a different statement if it is not true. But what if we want to do a third statement under a third condition. In this case, we want to check whether b is zero before doing the modulus operation, so that we don't get a zero division error which is what we would get if we try to mod it by zero. So, first we compare b to zero and print this message if it's true. Now, we want an else to go with that if, but we don't want to do that else block unconditionally, we only want to do it if a mod b is zero. So, we do else if which in python is shortened to elif. If b is not equal to zero, then Python will check this condition which is to see whether a mod b is zero. If it is, then we'll run this code down here, and if not, we'll come down here. This is an unconditional else, so if nothing else above it was true, then we'll run this code and as before, we'll print the ends no matter what. Keep in mind that only one of these three print statements can ever be executed, because we only run each under certain conditions as you can see in the flowchart. We can of course have an if-statement within an if-statement or what we've called nested if-statements. For instance, let's say we're trying to find the largest value of three variables x, y, and z. We might first compare x to y, and then depending on that result, do a different comparison. For instance, if we know x is greater than y, then we can compare it to z, and whichever is larger must be largest. But if x is not greater than y, then we come down to this else statement where we compare y to z and whichever is larger must be largest This kind of works fine but it can be a little hard to read and understand, as there are a few expressions to keep track of. What we really want to know is whether x is greater than y and x is greater than z, and so on. We can express this in Python using logical or Boolean operators which take Boolean expressions as their operands and return true or false. This is essentially the same code but a little simpler. In this first if-statement, we want to check two things, whether x is greater than or equal to y and whether it's greater than or equal to z. If both of these are true, then we'll print that x is the largest. However, that entire expression will only be considered true if both parts are true because we wants this and this. If either part is false, then the entire expression will evaluate to false and we'll come down here where we have an elif statement that checks whether y is greater than or equal to x, and whether y is greater than or equal to z. In this case, if both are true, then y must be largest. However, if one or both is false and we've made it down here, then z must be largest. Going forward, we'll see a lot of conditional statements in our Python code, and in this lesson we've seen the most important ones, the if-statement, the if-else statement, and what we call in Python the if-elif statement. These all use comparison operators like equal to, greater than, et cetera, when determining whether an expression is true or false and they run the corresponding code based on that expression. When we want to combine these expressions, we can use logical operators like 'and' to join them together.