From the course: Programming Foundations: Fundamentals

Working with simple conditions - Python Tutorial

From the course: Programming Foundations: Fundamentals

Working with simple conditions

- With if statements, we've discovered a way to add a bit more flexibility and excitement to our programs, but so far, if the condition is false, then we just don't do anything. But typically, we'll want to take some action in this case as well. This is done with the if-else statement. It looks very similar to our normal if statement, except that it has the else keyword along with a colon, followed by a code block. In programming, this is called the else clause, because you never find it without an accompanying if statement. Just like in English, if I said, every morning before I go to work, and that's it, that would be an incomplete thought. It's just a clause. I'd have to add something like, every morning before I go to work, I enjoy a cup of coffee. Now and the thought is complete. Similarly, we don't use the else clause without an if statement. Let's look at an example. If five is greater than six, then we're going to print five is greater than six, weird! Otherwise, if it's not true that five is greater than six, then we're going to print five is not greater than six. When this code is run, first the condition of the if statement will be checked, and since five is not greater than six, then it'll evaluate to false. Since the condition is false, it's going to skip over the if's code block and drop down to the else clause. The result will be the output of the else's print statement. Five is not greater than six. Let's switch over to VS Code to see a few more examples of the if-else statement. We are here in our 04_03 exercise file. Starting on line number one, we've created a variable named plant, and we've set its value equal to Cacti. Then, coming down to line number three, we have our if statement. Here, our condition checks to see if the value of the variable plant equals Cacti. If it does, then we're going to execute what we see here on line number four. That is, we will print the name of the plant and the fact that it doesn't need a lot of water. But if it's not true that the value of our plant variable equals Cacti, then we're going to print the name of the plant and the fact that it loves water, which is what we see here on line number six, but regardless of what the value of our plant variable is, we're going to print thanks. Let's go ahead and run this to check the output. And there you have it. Cacti don't need a lot of water. Thanks. Is that what you expected? I hope so. Our variable plant was assigned the value of Cacti. So when we came to our if statement on line number three, Cacti does equal Cacti, and so we print it out, Cacti don't need a lot of water. Now let's go ahead and change our plant variable to something else. Let's change it to Irises for example. Let's save it and run it again. This time, we get Irises love water. Thanks. That's because Irises does not equal Cacti, so we skip over the body of the if statement here on line number four, and instead, we execute what's inside of the else clause, Irises love water, but the important thing to remember is that regardless of what the plant variable's value is, we always print out thanks. With if-else statements, we will always execute one of the code blocks depending on the result of the condition test. This makes it perfect for when we need some action to take place in our programs.

Contents