98-381 Microsoft Introduction to Python – If-Statements and Conditional Logic

  1. If-Statements and Conditional Logic

Hey guys, welcome back. Apart from the sequence structure, there is also the selection structure: conditional statements where the commands do not run in the same order, but the computer takes decisions after one condition by executing the corresponding command block. In other words, there are times when we want the computer to do one thing and then another thing if something else happens. So for example, to make a simple calculator, we are initially reading two numbers and then telling the user to give another price. One, for example, if it wants to perform the addition, or two if it wants to perform the removal. So we must, depending on the actual third command he has given us, add or subtract the first two. This is what happened with the Vietnam declaration. And the command in most of the languages is something like this: It’s very simple. We say that if something happens, for example, if x is equal to Y or if A is equal to or more than B, then we would like a set of commands to be executed. But they will be executed only if this is true.

If the result is false, all these will be omitted. So actually, we can see a simple example in C. Here we have a variable named condition. It can actually keep boolean values, which means that it can only have either true or false. So here it is: So this command will be executed. This means that these methods will be printed on our screen. So, if the condition is false, the methods for setting the variable to true will be written in our console and displayed on our screen, and these will be executed.

Now that that is true, it will simply be omitted. So this is what will be printed, and then we can see an “if else” statement, which means that if the condition is true, then it will happen to this one, or else if it is false, we will have another statement. We could also have a next statement exactly after the previous if statement. We don’t have any limitations on this, and in this if statement without an else, we just have an if condition and just some commands inside this condition. So if we want these commands to be executed, then this should be true, or they will be omitted. It’s a very simple example because it has all these commands and if statements. Actually, we will see many examples later that will help you understand if statements more thoroughly. Thank you, and I look forward to seeing you in the next lesson.

  1. If-Statements and Conditional Logic in Python

I have two tabs right here, so that will be inside the if statement. Print VAR 2, which is 4000. So VAR 2 is 4000 and will be executed only if this is true. And, of course, if this is true, I will execute right now. And as you can see, there was an error because I forgot this one and this one. And I’m currently executing again. And as you can see, it says that VAR 2 is 4000.

But if I change this and then change something else, printing VAR two is not 4000. So I’ll right-click run Python filing terminal, and only this one will be executed this time. If I’m going to change this one to 1000, then this means that these commands will not execute because this statement is false. So I will right-click, and as you can see, nothing is printed after this last command. So I think that this was a good example, a good overview of the if statements, the nested if statements, the IFEL if else statements, and the IFLs statements. So I’m looking forward to seeing you in the next lesson.

Don’t forget that you can combine these statements and have nested if statements. which means that you can say that VAR one is 3000. If VAR one is 3000, then I would like to have VAR two with 4000, and then I’m sorry that this is equal with 4000, so print carefully with the tabs. I have two tabs right here, so that will be inside the if statement. Print VAR 2, which is 4000. So VAR 2, which is 4000, will only be executed if this is the case.

And, of course, if this is true, I will execute right now. And as you can see, there was an error because I forgot this one and this one. And I’m currently executing again. And as you can see, it says that VAR 2 is 4000. But if I change this and then change something else, printing VAR two is not 4000. So I’ll right-click run Python filing terminal, and only this one will be executed this time.

If I’m going to change this one to 1000, then this means that these commands will not execute because this statement is false. So I will right-click, and as you can see, nothing is printed after this last command. So I think that this was a good example, a good overview of the if statements, the nested if statements, the IFEL if else statements, and the IFLs statements. So I’m looking forward to seeing you in the next lesson.

img