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 02_If_Else_Logic_Start.py
.
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 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 = 5
. That is known as an assignment.
We're taking X
and setting it equal to 5. But here we're saying X == 6
. We're not changing the value of X
, right? We're not setting it equal to 6.
We're just checking to see if it is 6. 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 5. So the check for X == 6
is false.
But the check for X < 10
returns true. The check for X <= 5
returns true, because X
is 5, so less than or equal to is satisfied. The check for X!= 8
is also true, because X
is 5—it’s not 8.
And let's also try these with strings. Let's say pet = "bunny"
. And then we could say print(pet!= "cat")
—that is true.
And let's make a list: pets = ["bunny", "cat", "dog"]
. And we'll print the length. Remember, len()
returns the length of a list, which is 3.
We could check the length of pets
and see if it is 4—that’s false. And we could also check to see if the first pet at index 0 is "bunny", which is true—pets[0] == "bunny"
. And we can also check to see if the last item at index -1 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 that 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 == "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 > 3:
and then do stuff if X
is greater than 3, or do something else if X
is not greater than 3, and so on. At the end of the Boolean comparison check with the if
, you put a colon, and when you hit ENTER, the next line will auto-indent. The code inside the if
block will run only if the condition is true.
If we ask if pet == "cat"
, you can run this print("meow")
line, but that will only execute if the pet is "cat". So let's go ahead and try this out. We're going to say pet = "cat"
.
This is an assignment, so it’s a single equal sign, and then we'll say if pet == "cat":
. Now we're checking to see if the pet is "cat", and if that’s true, we’re going to run print("meow")
, and it prints "meow" because it does run. Now let's try a false example.
Pet is "bunny", right? We already knew that. Nothing happens. We haven’t set up something to run if the condition is false. 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 == "bunny"
, which is true, then we're going to print("crunch as he eats his carrot")
. There it is. So pet == "cat"
is false.
Code inside the if
statement following the colon (indented) does not run. If the 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 to reaffirm the value. So pet = "bunny"
. Now we're going to say if pet == "cat":
(which is false), we're going to print("meow")
, 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 " + pet)
. That prints: "Pet is not a cat, it is a bunny." Because if pet == "cat"
is false, so we're running the else
part.
The else
runs if the condition is false. The if
runs if the condition is true.
Now we can go in between an if
and an else
and use what is called elif
—short for "else if"—kind of like a sandwich. Think of if
and else
as the bread, and anything in between is another condition to evaluate.
You don’t just say if
—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 a C. We can do intermediary evaluations and Boolean checks. We're going to first check if the pet is a cat.
Let's change the pet. We'll say pet = "dog"
now, and then we'll say if pet == "cat":
(which is false). So this is not going to run—no "meow".
Then we're going to check: elif pet == "dog":
and that is true—that will run. Then we're going to print("woof")
. And then we'll say else:
print("Pet is neither cat nor dog")
. So that won't run in this case.
The elif
block will run. 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" and the "meow" will run.
So you have three different blocks that can run: the if
, the elif
, or the else
. And you can have as many elif
s in between the if
and the else
as you like. Note: the else
never takes a condition. It's just whatever is left.
All right, challenge time. Let's make an input called price
. The user is going to type a price.
You’re going to convert the price to a number, and then use if
–elif
–else
(two elif
s) to check the price. If the price is 0, 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 0, it will print "free".
If you type a 5, it will print "cheap". If you type 15, it will print "reasonable". And if you type 25, it will print "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 use input()
, and you now hopefully understand if
–elif
–else
a little better. I mean, we just started.
It'll be a little bit difficult, but it's the same mechanics as if
–elif
–else
with the pet. Pause, struggle with it, and see if you can get it.