Explore the essentials of Python conditional logic and master handling multiple conditions effectively. Gain insight into combining logical operators and using lists to create dynamic programming solutions.
Key Insights
- Understand Python conditional syntax clearly, using if, elif, and else statements, always ending condition lines with a colon and ensuring consistent indentation to avoid errors.
- Use logical operators like and and or to evaluate multiple conditions simultaneously, enabling flexible decision-making scenarios such as checking if a user has sufficient funds based on price and available money.
- Manipulate Python lists effectively by employing methods like append, extend, and insert to dynamically manage list content under conditional statements, as demonstrated in scenarios involving adding or adjusting fruit lists.
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.
Okay, we're back to do the solution on this challenge. We're going to make an input called price, save the inputted price to a variable. We'll say price equals float(input("Price: ")).
And then we're going to say, if price == 0, we're going to print "free". elif price is less than 10, so let's say it's false. It's not zero, but is it in the 1 cent to 9.99 range, right? If it's between zero and 10, then we would print "cheap".
And if that's false, it's got to be more than 10. We'll check to see if it's less than 20, meaning it would be in the 10 to 19.99 range. And in which case we print "reasonable"; else, there's nothing else to do but print "expensive" because the price must be 20 and up.
Run: 0 → free; Run: 5 → cheap; Run: 15 → reasonable; Run: 25 → expensive. So it works, multiple elifs. Note the syntax: always the colon at the end of the line, always the next thing indented, all the indents must align with each other.
Python is extremely finicky about indents. And the reason for that is it doesn't use any other way to indicate nested content. Like how else do we know that this print "free" thing is inside the if statement so that it only runs if the if is true?
If you had these being like all different amounts of indentation, it would get confused. It’s actually going with it—that’s interesting, okay. It's okay with that one, rolling.
Now, multiple conditions. A lot of times in real life, you consider two things, right? You're going to go to the restaurant if you like that kind of food and it's open and you're hungry, right? There have to be multiple things going on and all be true for the stars to align for you to do things in certain scenarios. So in this next example, we're going to look at evaluating two conditions with and—the and operator, where they both must be true—and evaluating two conditions where only one needs to be true. So I have enough cash or I have enough credit on my credit card—whatever, my debit card—or I have enough money one way or another, either through cash or credit.
So that would be an or situation, right? You can buy the sneakers if you have enough cash or credit on you. But in an and scenario, you can buy the sneakers if you have enough money and the store is open and they have your size. So sometimes you need multiple conditions.
So in this case, what we'll say is we'll have two inputs: price and money. We'll say price is going to equal the float of the input price. So you're going to be prompted to put a price in there.
And then we're going to input money. This will be your money—how much money you have.
And the amount of money you have has to be at least equal to the price for you to buy stuff. We're going to say if price == 0, that's still going to be free. So give me—we're going to say elif price is less than 10.
That's your standard for cheap. And money is greater than or equal to price. You would have to have enough money. So, to buy the cheap thing, we're testing to see if you're going to buy the cheap thing.
Well, to buy the cheap thing, it has to be cheap (less than 10), and you'd have to have enough money. And then we could say "Cheap, and I have enough money to buy it."
Or to buy the reasonable thing, it has to be reasonable. Price must be reasonable, and you must have enough money. We're going to say elif price is less than 20 and money is greater than or equal to price.
You could have the exact equal amount of money to buy it. Don't have any extra—completely tap you out. And else: the item is not free, and I don't have enough money.
Let's try this one. I don't have enough money. So price is less than 20, let's run.
22,25—oh, what are we doing here? We'll cap our—oh, this doesn't support a max beyond 20. Okay, fine. We'll say 12,13 → reasonable. Okay. We'll say 5, then 7 → cheap. And we'll say 5, then 4 → don't have enough money.
All right, so that's kind of working. Next up, we're going to do conditional logic with on-list conditions. We'll say fruits = [], let's turn these guys into a list.
And we're going to say, if there are six fruits in the list, add a lemon and print the updated list. We'll say if len(fruits) == 6, right? That's how you check how many items there are. If that's true, we're going to say fruits.append("lemon").
And then we would print the fruits. Len(fruits) == 6. Oh, I got to run the fruits, okay.
There we go, it worked. Challenge now, so pause and do this challenge. If the first fruit is apple and the last fruit is lemon, add orange and peach to the end of the list in one line of code and print the updated list.
So the first item is apple and the last item is lemon. So you'd have to check both of those: at index 0 and -1. And if that's the case—that apple's the first thing and lemon's the last thing—then what you do is you add orange and peach at the end in one line, which means you have to use extend.
So let's say if fruits[0] == "apple" and fruits[-1] == "lemon", this is true right now. We're going to say fruits.extend(["orange", "peach"]). Remember, extend takes a list, so we put them in a list, and then when we rerun fruits, we should have the two new items at the end.
Ooh, ow, why didn’t—oh, append. We don't want to do append. All right, we wanted to extend, which I just said. All right, so we're going to back up, rerun fruits.
There it is—they're in—you've got to use extend
. append
adds a nested (child) list, as we saw in the last lesson. Now the or
operator requires just one of multiple conditions to be true.
If the first fruit is blueberry or there are at least 12 fruits, add apricot to the beginning without losing apple and print the updated list. Well, that is not true. Oh, well, the first fruit is blueberry—that's false—but there are fewer than 12 fruits. That is true.
So one condition is true. So you would add apricot, but we want to put it at the beginning. We use insert
at index zero.
We're going to say: if fruits[0] == "blueberry"
(which is false), but we only need one condition with or
, or len(fruits) < 12
(that one is true), then we're going to say fruits.insert(0, "apricot")
and then print fruits. We should have an apricot at the beginning now, and we do.
So one condition is true. Now, getting a little fancy, you could combine and
and or
. So let's try: if (condition one or condition two) and condition three
.
We're going to do an or
with an and
. So the first condition of the and
will be an or
check, kind of nested. And then the second will be an and
check.
So what we're going to do is we're going to say: if the first fruit equals blueberry or the length is less than 12 (one of them is true—the length is still less than 12), that will be our first or
check.
So that's true. And now the second thing has to be true: the first letter of the first fruit is A. We're going to say: if the first fruit is blueberry or the length of fruits is less than 12 (which is true), and fruits[0][0] == "A"
. That is also true.
"Apricot" is the first fruit and it starts with A. We would do fruits[0]
to get "apricot" and fruits[0][0]
to get the first letter. Remember, you can look up index on strings as well as lists. We're going to say if that's true, we're going to add "kumquat".
We're going to add "kumquat" right after "kiwi". I’ll do that if the condition is true. We're going to say: if fruits[0] == "blueberry" or len(fruits) < 12
—right? Do we still have fewer than 12 fruits? We should. But now, we're going to require another condition: fruits[0][0] == "A"
, right? fruits[0]
is "apricot"; fruits[0][0]
is "A". If all that’s true, then we're going to add a "kumquat" right after "kiwi". We have to find "kiwi" first.
We'll say kiwi_index = fruits.index("kiwi")
. And we'll insert "kumquat" at kiwi_index
. We'll say fruits.insert(kiwi_index, "kumquat")
.
That's the item right before "kiwi". And that shall be a "kumquat". And if we print fruits, we'll have a "kumquat" before the "kiwi".
And we do—whoa, oh, not minus one. Because you basically let the "kumquat" take over the index that used to belong to "kiwi" and you push the "kiwi" down one. If you subtract one, you go too far before.
All right, so we're going to rerun just the fruits so we can get our "kumquat" in the right spot. There it is: kumquat, kiwi. We could do a nested version of this as well.
We could first do our check on "blueberry" or the length. And we don’t need parentheses because we have only one top-level condition. Now, if that checks out, we could go on to do the second check as a nested if
.
Let's add—so that's true. We have an if
inside an if
. And if that’s true, we will—let’s just say—add "strawberry" at the end.
We'll just say append("strawberry")
. But we're only doing that if the condition we checked before in the last example is true. In other words, the first fruit is either "blueberry" or the list has a length of less than 12 (which should still be the case—it’s on the cusp, it’s 11).
And the first fruit starts with A—that's still the situation. We will append "strawberry" in that case.
Run and we’re good to go—that worked. All right, this is for you to pause and type. That’s why I went a little fast—because it’s all about you hitting the pause button.
Multiple conditions—sit here. I mean, the video is not so long for this lesson, but you could and should be on this for hours, really, to truly learn this. And/or—just know you have it as a resource and come back now and again. Also, we will be doing if
logic a lot going forward—tons of it.
So it's not like this is the only time you're going to see this. Okay, good work everybody. Thank you very much.
That's the end of Lesson Two. We will see you in the next lesson.