Python- Day 17- IF Statements with Lists
Combining lists with if
statements enable powerful control over how you process data.
You can treat specific values differently, manage changing conditions, and ensure your code behaves as expected in a variety of scenarios.
Checking for Special Items:
You can use if
statements inside loops to handle special values in lists.
Let’s continue with a pizzeria example where the toppings are listed and the program announces each one as it's added to the pizza.
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
>>
Adding mushrooms.
Adding green peppers.
Adding extra cheese.
Finished making your pizza!
If a topping is unavailable, you can use an if
statement to provide an alternative action:
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry, we are out of green peppers right now.")
else:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
>>
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.
Finished making your pizza!
Checking if a List is Empty:
Before processing a list, it’s useful to check whether it contains any items.
This is especially important if the list can sometimes be empty.
For example, you can ask if the customer wants a plain pizza if no toppings are requested:
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
>>
Are you sure you want a plain pizza?
If the list had toppings, the output would show the toppings being added.
Using Multiple Lists:
You can also use multiple lists to handle customer requests and available items.
For instance, checking if a requested topping is available before adding it to the pizza:
available_toppings = ['mushrooms', 'olives', 'green peppers', 'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print(f"Adding {requested_topping}.")
else:
print(f"Sorry, we don't have {requested_topping}.")
print("\nFinished making your pizza!")
>>
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.
Finished making your pizza!
Styling Your if
Statements:
Following PEP 8 guidelines, ensure proper spacing around comparison operators for readability:
if age < 4:
is better than:
if age<4:
This doesn’t affect how Python interprets the code but improves its readability.
You learned how to write conditional tests that evaluate to True
or False
, and how to construct if
, if-else
, and if-elif-else
chains.
You applied these structures to lists, efficiently handling special conditions and user input.
Finally, you saw how Python’s style guide helps make your code cleaner and easier to read.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!