Conditional Statements¶
Programming often involves examining a set of conditions and deciding which action to take based on those conditions. Python’s if statement allows you to examine the current state of a program and respond appropriately to that state.
In this lecture you’ll learn to write conditional tests, which allow you to check any condition of interest. You’ll learn to write simple if statements, and you’ll learn how to create a more complex series of if statements to identify when the exact conditions you want are present. You’ll then apply this concept to lists, so you’ll be able to write a for loop that handles most items in a list one way but handles certain items with specific values in a different way.
A SIMPLE EXAMPLE¶
The following short example shows how if tests let you respond to special situations correctly. Imagine you have a list of cars and you want to print out the name of each car. Car names are proper names, so the names of most cars should be printed in title case. However, the value 'bmw' should be printed in all uppercase. The following code loops through a list of car names and looks for the value 'bmw'. Whenever the value is 'bmw', it’s printed in uppercase instead of title case:
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
Audi BMW Subaru Toyota
cars = ['audi', 'bmw', 'subaru', 'toyota']
# first variable dosen't matter (can be anything, e.g. car can be instead of b)
for b in cars:
if b == 'bmw':
print(b.upper())
else:
print(b.title())
Audi BMW Subaru Toyota
cars = ['audi', 'bmw', 'subaru', 'toyota']
# != means unequal.
for b in cars:
if b != 'bmw':
print(b.upper())
else:
print(b.title())
AUDI Bmw SUBARU TOYOTA
Additional exercise¶
students = ['geography', 'chemistry', 'studio art', 'math', 'geography']
for student in students:
if student == 'geography':
print('you need do extra work')
else:
print('you are good to go.')
you need do extra work you are good to go. you are good to go. you are good to go. you need do extra work
CONDITIONAL TESTS¶
At the heart of every if statement is an expression that can be evaluated as True or False and is called a conditional test. Python uses the values True and False to decide whether the code in an if statement should be executed. If a conditional test evaluates to True, Python executes the code following the if statement. If the test evaluates to False, Python ignores the code following the if statement.
Checking for Equality¶
Most conditional tests compare the current value of a variable to a specific value of interest. The simplest conditional test checks whether the value of a variable is equal to the value of interest:
car = 'bmw' #assign value to variable
car == 'bmw' #either true or false
True
result = car =='bmw'
result
True
result == True
True
result is True #check something true or not
True
car = 'BMW' #assign value to variable
car == 'bmw' #case sensitive
False
Ignoring Case When Checking for Equality¶
Testing for equality is case sensitive in Python. For example, two values with different capitalization are not considered equal:
car = 'Audi'
car == 'audi'
False
If case matters, this behavior is advantageous. But if case doesn’t matter and instead you just want to test the value of a variable, you can convert the variable’s value to lowercase before doing the comparison:
car.lower()== 'audi'
True
car.lower()== 'audi'.upper #demo, only need to change one side
False
Websites enforce certain rules for the data that users enter in a manner similar to this. For example, a site might use a conditional test like this to ensure that every user has a truly unique username, not just a variation on the capitalization of another person’s username. When someone submits a new username, that new username is converted to lowercase and compared to the lowercase versions of all existing usernames. During this check, a username like 'John' will be rejected if any variation of 'john' is already in use.
Checking for Inequality¶
When you want to determine whether two values are not equal, you can combine an exclamation point and an equal sign (!=). The exclamation point represents not, as it does in many programming languages.
Let’s use another if statement to examine how to use the inequality operator. We’ll store a requested pizza topping in a variable and then print a message if the person did not order anchovies:
request ='mushroom'
if request != 'sugar':
print('Hold on until further notice')
Hold on until further notice
Numerical Comparisons¶
Testing numerical values is pretty straightforward.
age = 18
age == 18
True
answer = 17
if answer != 42:
print('This not the right answer. Please try again!')
This not the right answer. Please try again!
You can include various mathematical comparisons in your conditional statements as well, such as less than, less than or equal to, greater than, and greater than or equal to:
age = 19
age < 21
True
age<= 21
True
age>=21
False
age> 21
False
age<21
True
result = True
result is True
True
Checking Multiple Conditions¶
You may want to check multiple conditions at the same time. For example, sometimes you might need two conditions to be True to take an action. Other times you might be satisfied with just one condition being True. The keywords and and or can help you in these situations.
Using and to Check Multiple Conditions¶
To check whether two conditions are both True simultaneously, use the keyword and to combine the two conditional tests; if each test passes, the overall expression evaluates to True. If either test fails or if both tests fail, the expression evaluates to False.
a = 22
b = 18
a >= 21 and b>=21 #need to fulfill all conditions
False
# example
major = 'geography'
GPA = 3.91
if major=='geography' and GPA >= 3.8:
print('You can get a scholarship from Geography department.')
You can get a scholarship from Geography department.
b = 40
a >= 21 and b>=21 #need to fulfill all conditions, find the first false
#check the condition for first one, if its already false, the python program
# will do not check the second one.
True
a = 22
b = 18
a >= 21 or b>=21 #need to find the first true
True
To improve readability, you can use parentheses around the individual tests, but they are not required. If you use parentheses, your test would look like this:
(a >= 21) and (b >= 21)
False
c=19
a>=21 and b>=21 and c>=21
False
a>=21 and b>=21 or c>=18
True
a>=21 and (b>=21 or c>=18) # change the execute sequence
True
Using or to Check Multiple Conditions¶
The keyword or allows you to check multiple conditions as well, but it passes when either or both of the individual tests pass. An or expression fails only when both individual tests fail.
a >= 21 or b>=21
True
Checking Whether a Value Is in a List¶
Sometimes it’s important to check whether a list contains a certain value before taking an action. For example, you might want to check whether a new username already exists in a list of current usernames before completing someone’s registration on a website. In a mapping project, you might want to check whether a submitted location already exists in a list of known locations.
To find out whether a particular value is already in a list, use the keyword in. Let’s consider some code you might write for a pizzeria. We’ll make a list of toppings a customer has requested for a pizza and then check whether certain toppings are in the list.
request = ['onion', 'apple', 'pear', 123]
'onion' in request # just check if something in the list
True
'peach' in request
False
a = 'onion'
a in request
True
123 in request # can check number, text, anything within list
True
Checking Whether a Value Is Not in a List¶
Other times, it’s important to know if a value does not appear in a list. You can use the keyword not in this situation. For example, consider a list of users who are banned from commenting in a forum. You can check whether a user has been banned before allowing that person to submit a comment:
user_bank = ['Eric', 'Deng', 'Emily']
user = 'Hsu'
if user not in user_bank:
print(f"{user.title()}, you cannot post anything before register.")
Hsu, you cannot post anything before register.
Boolean Expressions¶
As you learn more about programming, you’ll hear the term Boolean expression at some point. A Boolean expression is just another name for a conditional test. A Boolean value is either True or False, just like the value of a conditional expression after it has been evaluated.
Boolean values are often used to keep track of certain conditions, such as whether a game is running or whether a user can edit certain content on a website:
#Boolean only have two outcome True or False, no anything in between.
IF STATEMENTS¶
When you understand conditional tests, you can start writing if statements. Several different kinds of if statements exist, and your choice of which to use depends on the number of conditions you need to test. You saw several examples of if statements in the discussion about conditional tests, but now let’s dig deeper into the topic.
Simple if Statements¶
The simplest kind of if statement has one test and one action:
if conditional_test:
do something
You can put any conditional test in the first line and just about any action in the indented block following the test. If the conditional test evaluates to True, Python executes the code following the if statement. If the test evaluates to False, Python ignores the code following the if statement.
Let’s say we have a variable representing a person’s age, and we want to know if that person is old enough to vote. The following code tests whether the person can vote:
age = 19
if age >= 18:
print('You are old enough to vote.')
You are old enough to vote.
age = 17
if age >= 18: #if condition doesn't meet, it will be done.
print('You are old enough to vote.')
age = 17
if age >= 18: #if condition doesn't meet, it will be done.
print('You are old enough to vote.')# indetation
print('haha')
haha
Indentation plays the same role in if statements as it did in for loops. All indented lines after an if statement will be executed if the test passes, and the entire block of indented lines will be ignored if the test does not pass.
You can have as many lines of code as you want in the block following the if statement. Let’s add another line of output if the person is old enough to vote, asking if the individual has registered to vote yet:
age = 17
if age >= 18: #if condition doesn't meet, it will be done.
print('You are old enough to vote.')# indetation
print('haha')
age = 19
if age >= 18:
print('You are old enough to vote.')
print('Have you registered to vote yet?')
You are old enough to vote. Have you registered to vote yet?
if-else Statements¶
Often, you’ll want to take one action when a conditional test passes and a different action in all other cases. Python’s if-else syntax makes this possible. An if-else block is similar to a simple if statement, but the else statement allows you to define an action or set of actions that are executed when the conditional test fails.
We’ll display the same message we had previously if the person is old enough to vote, but this time we’ll add a message for anyone who is not old enough to vote:
age = 17
if age >= 18:
print("You are old enough to vote.")
print('Have you registered to vote yet?')
else:
print('Sorry, you are not enough to vote.')
print('Please register to vote as soon as you become 18!')
Sorry, you are not enough to vote. Please register to vote as soon as you become 18!
age = 19
if age >= 18:
print("You are old enough to vote.")
print('Have you registered to vote yet?')
else:
print('Sorry, you are not enough to vote.')
print('Please register to vote as soon as you become 18!')
You are old enough to vote. Have you registered to vote yet?
The if-elif-else Chain¶
Often, you’ll need to test more than two possible situations, and to evaluate these you can use Python’s if-elif-else syntax. Python executes only one block in an if-elif-else chain. It runs each conditional test in order until one passes. When a test passes, the code following that test is executed and Python skips the rest of the tests.
Many real-world situations involve more than two possible conditions. For example, consider an amusement park that charges different rates for different age groups:
- Admission for anyone under age 4 is free.
- Admission for anyone between the ages of 4 and 18 is $25.
- Admission for anyone age 18 or older is $60.
- Admission for anyone age older than 65 is $10
How can we use an if statement to determine a person’s admission rate? The following code tests for the age group of a person and then prints an admission price message:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 60
elif age >= 65:
price = 10
print(f"Your admission cost is $ {price}.")
Your admission cost is $ 25.
# only can have one else, but many elif
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 60
elif age >= 65:
price = 10
else: #meaningless, will never be execute
price = 1000
print(f"Your admission cost is $ {price}.")
Your admission cost is $ 25.
# only can have one else, but many elif
age = 120
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 60
elif age < 100:
price = 10
else: #meaningful, because the above schema doesn't cover all the possibility
price = -10
print(f"Your admission cost is $ {price}.")
Your admission cost is $ -10.
The else block is a catchall statement. It matches any condition that wasn’t matched by a specific if or elif test, and that can sometimes include invalid or even malicious data. If you have a specific final condition you are testing for, consider using a final elif block and omit the else block. As a result, you’ll gain extra confidence that your code will run only under the correct conditions.
Testing Multiple Conditions¶
The if-elif-else chain is powerful, but it’s only appropriate to use when you just need one test to pass. As soon as Python finds one test that passes, it skips the rest of the tests. This behavior is beneficial, because it’s efficient and allows you to test for one specific condition.
However, sometimes it’s important to check all of the conditions of interest. In this case, you should use a series of simple if statements with no elif or else blocks. This technique makes sense when more than one condition could be True, and you want to act on every condition that is True.
Let’s reconsider the pizzeria example. If someone requests a two-topping pizza, you’ll need to be sure to include both toppings on their pizza:
request = ['beef', 'cheese']
if 'beef' in request:
print('adding beef.')
if 'mushroom' in request:
print('adding mushroom')
if 'cheese' in request:
print('adding more cheese.')
print("finished makeing your pizza!")
adding beef. adding more cheese. finished makeing your pizza!
#not work well with elif, because elif only process if first if doesn't work
if 'beef' in request:
print('adding beef.')
elif 'mushroom' in request:
print('adding mushroom')
elif 'cheese' in request:
print('adding more cheese.')
print("finished makeing your pizza!")
adding beef. finished makeing your pizza!
if 'mushroom' in request:
print('adding mushroom.')
elif 'beef' in request:
print('adding beef.')
elif 'cheese' in request:
print('adding more cheese.')
print("finished makeing your pizza!")
adding beef. finished makeing your pizza!
In summary, if you want only one block of code to run, use an if-elif-else chain. If more than one block of code needs to run, use a series of independent if statements.
USING IF STATEMENTS WITH LISTS¶
You can do some interesting work when you combine lists and if statements. You can watch for special values that need to be treated differently than other values in the list. You can manage changing conditions efficiently, such as the availability of certain items in a restaurant throughout a shift. You can also begin to prove that your code works as you expect it to in all possible situations.
Checking for Special Items¶
This lecture began with a simple example that showed how to handle a special value like 'bmw', which needed to be printed in a different format than other values in the list. Now that you have a basic understanding of conditional tests and if statements, let’s take a closer look at how you can watch for special values in a list and handle those values appropriately.
Let’s continue with the pizzeria example. The pizzeria displays a message whenever a topping is added to your pizza, as it’s being made. The code for this action can be written very efficiently by making a list of toppings the customer has requested and using a loop to announce each topping as it’s added to the pizza:
request = ['beef', 'cheese','mushrooms']
for a in request:
print(f"adding {a}.")
print("finished making your pizza.")
adding beef. adding cheese. adding mushrooms. finished making your pizza.
The output is straightforward because this code is just a simple for loop. But what if the pizzeria runs out of green peppers? An if statement inside the for loop can handle this situation appropriately:
for a in request:
if a =='beef':
print("Sorry, we are out of beef right now.")
else:
print(f"adding {a}.")
print("finishing making your pizza.")
Sorry, we are out of beef right now. adding cheese. adding mushrooms. finishing making your pizza.
Checking That a List Is Not Empty¶
We’ve made a simple assumption about every list we’ve worked with so far; we’ve assumed that each list has at least one item in it. Soon we’ll let users provide the information that’s stored in a list, so we won’t be able to assume that a list has any items in it each time a loop is run. In this situation, it’s useful to check whether a list is empty before running a for loop.
As an example, let’s check whether the list of requested toppings is empty before building the pizza. If the list is empty, we’ll prompt the user and make sure they want a plain pizza. If the list is not empty, we’ll build the pizza just as we did in the previous examples:
a = []
if a:
for b in a:
print(f"adding{b}.")
print("finished making your pizza.")
else:
print("Are you want a plain pizza?")
Are you want a plain pizza?
a = ['beef','mushroom']
if a:
for b in a:
print(f"adding {b}.")
print("finished making your pizza.")
else:
print("Are you want a plain pizza?")
adding beef. adding mushroom. finished making your pizza.
Using Multiple Lists¶
People will ask for just about anything, especially when it comes to pizza toppings. What if a customer actually wants french fries on their pizza? You can use lists and if statements to make sure your input makes sense before you act on it.
Let’s watch out for unusual topping requests before we build a pizza. The following example defines two lists. The first is a list of available toppings at the pizzeria, and the second is the list of toppings that the user has requested. This time, each item in requested_toppings is checked against the list of available toppings before it’s added to the pizza:
available = ['mushrooms', 'olives', 'green peppers','extra cheese']
requested = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested:
if requested_topping in available:
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¶
In every example in this chapter, you’ve seen good styling habits. The only recommendation PEP 8 provides for styling conditional tests is to use a single space around comparison operators, such as ==, >=, <=. For example:
if age < 4:
is better than:
if age<4:
Such spacing does not affect the way Python interprets your code; it just makes your code easier for you and others to read.
SUMMARY¶
In this chapter you learned how to write conditional tests, which always evaluate to True or False. You learned to write simple if statements, if-else chains, and if-elif-else chains. You began using these structures to identify particular conditions you needed to test and to know when those conditions have been met in your programs. You learned to handle certain items in a list differently than all other items while continuing to utilize the efficiency of a for loop. You also revisited Python’s style recommendations to ensure that your increasingly complex programs are still relatively easy to read and understand.
Finished¶
References¶
- Matthes, Eric (2022). Python Crash Course, 3rd Edition: A Hands-On, Project-Based Introduction to Programming. No Starch Press. ISBN: 978-1593279288. (Publisher website)
- Dr.Qiusheng Wu online lecture