From the course: Certified Entry-Level Python Programmer (PCEP-30-02) Cert Prep
Using conditional blocks with if - Python Tutorial
From the course: Certified Entry-Level Python Programmer (PCEP-30-02) Cert Prep
Using conditional blocks with if
- [Instructor] So far when we have a Python code, we know that all the lines are going to be executed. So if I run this code, it's asking me for a price, waiting for my input, and then it's executing line three and then line four making the print, and when there are no more lines, the program is ending. Well, now it's time to see how we can make conditional execution. For example, we could do something different for when the user is entering zero as the price. So instead of saying price dollar zero, we can send a different message. So for that we are going to use a conditional block. We use a conditional block by using if. If, is the keyword that we are going to use. After the if keyword, we use a space and we need to enter a Boolean condition. That Boolean condition can be a bool variable or we can use an operator that returns a bool. For example, we can check if the price it's equals to zero and be careful because here we are using double equals because we are actually asking if the price is equals to zero. We are not assigning a zero. We use one equals to assign the value, two equals to ask about the value. After the condition we should add a column. And now after I added the column, you can see that I have an error here on the print. Why is that? Because Python needs a code block. What is a code block? We need to indent the next line. So what we are going to do when the price is zero, but actually that's not what we want. If the price is zero and look at that, if I press enter, it's indenting automatically for me and it's using four characters to do that following PEP 8 style guidelines. So if the price is zero, I'm going to print, "You didn't enter a price." So now if I run this code, let's separate the if from the rest, remember we can add as many lines as we want. It's asking for the price. If I say zero, it says you didn't enter a price and then price zero, so it's actually executing both lines. Why is that? Because this is a code block. The code block ends when you have a new line that is actually without the indentation. So if the price is zero, the only line that is going to execute is this one. What if I add indentation to that print as well? What will happen is that now if I enter zero, I'm seeing both prints. But if I enter something different such as 10, I'm seeing nothing. Not even this one because the price is not zero. So now I have a code block that is executing these two lines only if and only if the price is zero. Also I could say, okay, if the price is greater than zero colon, I can do something different. For example, I can move my print here. I need to adjust the indent. So now I have two conditions. If the price is zero, it says you didn't enter a price. If it's 10, it says price 10. So you can add as many conditional if statements as you want. But for these situations where you have one condition and another condition, that seems to be the opposite in this case, it's not the opposite because what happens if I use a negative number? Hmm, so I'm not doing anything for that particular condition. But let's forget about the negative value for a second. Another way to do this is to use the else. The if statement has a companion. That companion is optional so I don't need to use it on every situation. It's called else. Else also receives a colon, but it doesn't receive any condition. So in this case it says if the price is zero, you didn't enter a price else, we're going to print the price. So let's clear the console and try this again. What's the price? Zero. You didn't enter a price, correct. If it's 10, price 10, if it's negative, well it's going with the else because it's not zero, which is okay, but maybe for the negative I want to do something else, something different. And for that we can nest conditions. So on the else we can do another if and check if the price is less than zero colon. So if it's less than zero, we're going to print invalid price, else, so if not, we are going to print the price. So now we have nested code blocks here. And remember indentation is everything. So this is one code block. Then we have here another code block. And within this code block we have nested code blocks here and here. Whatever you put in the same code block goes with the if or with the else. So now let's say here I can print end of program. So let's try this. If I try this now what's the price? Two. I say price two end of program, okay, cool. But if I say zero, I don't see end of program, why? Because end of program has a double indentation, so I can see here I have a double indentation, so when the price is zero, it's not else, so it's not going to execute that line. I can go back one indentation and now it's going to print end of program only on this else because I have two elses. If I want to print end of program for every situation it must be in the column zero, so no indentation, that means it will print end of program on every possible situation. So now if I say zero it says you didn't enter a price. You can nest as many if conditionals as you want, but at one point it gets messy when you have a lot of indentation and you don't know where you should be adding your code. So for that situation when you have if else, and then another if, Python offers another syntax that will make this a little better. So instead of using an else and immediately an if, we can use another sentence called elif that is actually mixing both together, it's an else with an if. So and then instead of the if, which you remove the if, I need to put the price there, the condition there. And now this else will go one indentation back, the same as the other statements. So this is how you read this. If the price is zero, it will execute this code block printing, "You didn't enter a price" then if not, I'm asking another question. If the price is less than zero, it will print invalid price and on any other situation that's the final else, I can print the price and of course I can add more elifs. I know I can check if the type of the price is wherever. If the price is one, let's say for example we can say if the price is less than 10, maybe we have a discount, okay, so I will print discounted price, the dollar sign, and then the price multiplied by 0.9 so 10% off. I need to write a comma here as well. So I can have as many elif statements as I want. So zero, you didn't enter a price minus one, invalid price. 19, the price is 19, and if I'm saying eight, the price is actually seven 20 because it's a discounted price and I have a typo on discount. And that's how you work with conditional blocks using if elif and else.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.