Dive into Python programming with this concise exploration of If-Else logic, and discover how Boolean comparisons control your code's flow. Learn to write conditional statements clearly and effectively to handle various coding scenarios.
Key Insights
- Explore Boolean operators and comparison logic, including equals (
==
), not equals (!=
), and less than or equal (<=
), demonstrated through numeric and string examples. - Understand Python's
if-elif-else
control structures through practical examples, such as checking pet types (cat, dog, bunny) to demonstrate conditional code execution. - Practice conditional logic through a price-checking coding challenge, using nested
if-elif-else
blocks to output whether a numeric input falls into categories such as "free," "cheap," "reasonable," or "expensive."
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Hi, welcome back to Python for Programming and Data Science. This is lesson two, If-Else Logic. My name is Brian McLean.
Thank you for coming back for the next lesson. All right, let's dive right into our file. We are in O2 If-Else Logic start.
I made a copy of it called Prog for Progress and here we go. So If-Else Logic is for running code based on a condition being either true or false. You use what are called these Boolean operators to compare two values and whatever the result is, true or false, you can do, you can execute code based on whether the condition is true or false, kind of like branching logic.
So let's come down here and type this. We have x equals five. That is known as an assignment.
We're taking x and setting it equal to five. But here we're saying x double equals six. We're not changing the value of x, right? We're not setting it equal to six.
We're just checking to see if it is six. We're using the comparison operator. And in this case, the comparison operator is going to return false, right? The equality check operator, because it is not true that x is five, right? So the check for x equals six is false.
But the check for x is less than 10 returns true. The check for x is less than or equal to five, that returns true, because x is five, so less than or equal to is satisfied. The check for x is not equal to eight, that's also true, because x is five, it's not eight.
And let's, we can also try these with strings. Let's say pet equals bunny. And then we could say print pet not equal to cat, that is true.
And let's make a list pets, bunny, cat, dog. And we'll print a len. Remember len returns the length of a list, which is three.
We could check the length of pets and see if it's four, and that's false. And we could also check to see if the first pet at index zero is bunny, which is true, double equal on the index zero. And we can also check to see if the last item at negative one is cat, which it is not.
The last item is dog, so that should be false. Run all that, and you get false, true, true, true, true, false, true, false. Now, why would you do these? You're not going to just run these in isolation.
The point of doing the Boolean comparisons is when you get your Boolean answer, you decide to do something if the condition is true and something else if the condition is false. So that is where if comes in. We're going to move on to the next cell, if comparisons.
The keyword if followed by a Boolean comparison, that's the syntax. We're going to say if, and then put one of these Boolean comparisons right after the word if. We could say if pet equals cat, and then you put a colon, and then inside that you would do stuff if the condition is true.
Or we could say if x is greater than three, and then do stuff if x is greater than three, or do something else if x is not greater than three, and so on. At the end of the Boolean comparison check with the if, the if Boolean, you put a colon, and when you hit ENTER, the next line will auto indent, which is automatically going to indent itself. The code inside the if block will run only if the condition is true.
If we ask if pet is equal to cat, you can run this meow line, but that'll only execute if the pet is cat. So let's go ahead and try this out. We're going to say pet equals cat.
This is an assignment, so it's single equal sign, and then we'll say if pet double equals cat. Now we're checking to see cat, and if that's true, we're going to run this meow, and it prints meow because it does run. Now let's check if, okay, let's try it false.
Pet's bunny, right? We already knew that. Nothing happens. We haven't set up something to run if the condition is false, so if the condition is true, we're going to say meow, but the condition is not true, so it does not run.
Now we'll switch it to true. Well, I did not even switch it, just keep it true. Well, it's still bunny, but I mean, let's switch the condition we're checking.
If pet equals bunny, which is true, then we're going to print crunch as he eats his carrot. There it is. So pet equals cat, false.
Code inside the if statement following the colon indented does not run. Condition is true that if code does run. Now what to do if the condition is false? We still have our pet.
Let's set our pet to bunny again, even though it's still bunny, but just like reaffirm the bunny value there. So pet equals bunny. Now we're going to say if pet double equals cat, which is false, we're going to say meow is a cat, but else let's do something if the condition is false.
We're going to say else print. Pet is not a cat, it is a, and we'll just concatenate on the pet. Pet is not a cat, it is a bunny, right? Because if pet equals cat is false, so we're running the else part.
The else runs if the condition is false. Do else, run else, or else runs if condition is false. If runs if condition is true.
If pet equals cat is not true so that meow does not run. Elif, we can go in between an if and an else and put in what is called an elif, an else if, kind of like a sandwich. Think of if and else as bread on a sandwich and anything in between to get up another condition that you evaluate.
You don't just say if, you know, either or, right? Like every test is not a pass-fail test. You could have an A, and if you didn't get an A, maybe you got a B, maybe you got a C. We can do intermediary evaluations and Boolean checks. We're going to first check to see if the cat is, if the pet is a cat.
Let's change the pet. We'll say pet equals dog now, and then we'll say if pet equals cat, which is false. So this is not going to run, this meow.
Then we're going to check, we're going to say elif pet equals dog, and that is true, that will run. Then we're going to do a little woof, and then we'll say else pet is neither cat nor dog. It's a pet, right? So that's not going to run.
The middle one will run, elif pet equals dog, and indeed. But if you change it to bunny and run it, that is neither cat nor dog, it's a bunny. You change it to cat.
So you have three different things that can run, the if, the elif, or the else, and you can have as many elifs in between the if and the else as you like. Note the else never takes a condition. It's just whatever else.
All right, challenge. Let's make an input called price. The user is going to type a price, that's you.
You're going to type a price, and you're going to convert the price to a number, and then you're going to use if elif elif else, so two elifs, to check the price. If the price is zero, print free. Else, if the price is less than 10, print cheap.
Else, if the price is less than 20, print reasonable. Else, print expensive. If you type a zero in there, you'll get free.
If you print a five in there, you'll print cheap. If you put a 15 in there, you'll print reasonable, and if you put a 25 in there, it'll say expensive. So here we have our print statements.
So challenge, go ahead and pause and see if you can do that. You know how to do an input, and you understand now hopefully a little bit. I mean, we just started.
It'll be a little bit difficult, but it's the same mechanics as is if elif else with the pet. Pause, struggle with it, see if you can get it.