- 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.
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.py
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())At the heart of every if statement is an expression that can be evaluated as True or False and is called a conditional test. We tell Python with our code that if the condition is True the we execute the code following the if statement. If the codition is False then Python will ignore the code following the if statement block.
Most conditional tests compare the current value of a variable to a specific value of interest.
car = 'bmw'
car == 'bmw' # this line will return True(==) equality operator returns True or False if the values on the left and right side of the operator match or don't.
Testing for equality is case sensitive in Python:
car = 'Audi'
car == 'audi' # will return FalseYou can covert the varaible's value to lowercase before doing the comparison:
car = 'Audi'
car.lower() = 'audi' # will return TrueWhen 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.
How to use it:
# toppings.py
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")Most of the conditional expressions you write will test for equality, but sometimes you’ll find it more efficient to test for inequality.
Comparing numerica values done by ==
Example:
age = 92
age == 92 # this will return TrueYou can also see if two number are not equal:
#magic_number.py
answer = 17
if answer != 42:
print("That is not the correct answer. Please try again!")You can also include mathematical comparisons:
age = 19
age < 21 # True
age <=21 # True
age > 21 # False
age >= 21 # FalseAll these comparisons can be and should be used as a part of an if statement.
Sometimes you would 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.
To check whether two conditions are both True simultaneously, use the key-word and :
age_0 = 22
age_1 = 18
age_0 >= 21 and age_1 >= 21 # False
age_1 = 22
age_0 >= 21 and age_1 >= 21 # TrueTo improve readability you can use parentheses areound the individual tests, but are not obliged to do so. Example:
(age_0 >= 21) and (age_1 >= 21)The keyword or allows you to check multiple conditions as well, but it passes when either or both of the individual tests pas. An or expression fails only when both individual tests fail.
Example:
age_0 = 22
age_1 = 18
age_0 >= 21 or age_1 >= 21 # True
age_0 = 18
age_0 >= 21 or age_1 >= 21 # FalseSometimes you would want to check whether a list contains a certain value before proceeding. To find out whether a particular value is already in a list, use the keyword in.
requested_toppings = ['mushrooms', 'onions','pineapple']
'mushrooms' in requested_toppings # True
'pepperoni' in requested_toppings # FalseOther 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:
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()}, you can post a response if you wish.")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.
game_active = True
can_edit = FalseWhen 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.
Form of if statements:
if conditional_test:
do something
Code example:
age = 19
if age >= 18:
print("You are old enough to vote!")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 follow-ing the if statement.
Often, you'll want to take one action when a conditional test passes and a different action in all other cases. If-else statements make it possible.
if-else block is similar to simple if statement, but the else statement allows you to define and action or set of actions that are executed when the conditional test fails.
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")At times you will need to test more than two possible conditions, and to evaluate those you will use Python's if-elif-else syntax.
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 $40.
age = 12
if age < 4:
print("Your admission cost is 0$.")
elif age < 18:
print("Your admission cost is 25$.")
else:
print("Your admission cost is 40$.")
# another way to write the program:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
else:
price = 40
print(f"Your admission cost is {price}$.")
# It's more efficient less print() statements to edit if needs beYou can use as many elif blocks in your code as you like.
age = 72
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f"Your admission cost is {price}$.")Python does not require an else block at the end of an if-elif chain. Some-times an else block is useful; sometimes it is clearer to use an additional elif statement that catches the specific condition of interest
age = 74
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >=65:
price = 20
print(f"Your admission cost is {price}$.")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.
The if-elif-else chain is megapowerful, but it's only appropriate to use when you just need one test to pass. As soon as Python finds one test taht passes, it skips the rest of the tests. This is very efficient and allows you to test for one specific condition.
However if you need to check multiple conditions, it is simpler to use if statements with no elif or else blocks.
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")This code would not work properly if we used an if-elif-else block, because the code would stop running after only one test passes.
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
elif 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
elif 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making 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
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. You can use if statements to check whether your code works as you intended.
#toppings.py
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == "green peppers":
print(f"Sorry, we are out of {requested_topping} right now.")
else:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")We can can use if statement to quickly check if a list has any entries and is not empty before running a for loop or any other piece of code:
requested_toppings = [] #empty list
if requested_toppings: # empty list returns False
for requested_topping in requested_toppings: # this block will be omitted because the list is empty
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?") # this will be printed insteadYou can also use if statements with multiple lists. For example we can use two lists to check them against each other.
Example:
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!")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: