Chapter 5: if Statements

Source: Python Crash Course, 3rd Edition by Eric Matthes

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 chapter, 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 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.py
cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:
    if car == 'bmw':  (1)
        print(car.upper())
    else:
        print(car.title())
1 The loop checks if the current value of car is 'bmw'. If it is, the value is printed in uppercase; otherwise it’s printed in title case.
Audi
BMW
Subaru
Toyota

This example combines a number of the concepts you’ll learn about in this chapter. Let’s begin by looking at the kinds of tests you can use to examine the conditions in your program.

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'
>>> car == 'bmw'
True

The first line sets the value of car to 'bmw' using a single equal sign, as you’ve seen many times already. The next line checks whether the value of car is 'bmw' by using a double equal sign (==). This equality operator returns True if the values on the left and right side of the operator match, and False if they don’t match. The values in this example match, so Python returns True.

When the value of car is anything other than 'bmw', this test returns False:

>>> car = 'audi'
>>> car == 'bmw'
False

A single equal sign is really a statement; you might read the first line of code here as "Set the value of car equal to 'audi'." On the other hand, a double equal sign asks a question: "Is the value of car equal to 'bmw'?" Most programming languages use equal signs in this way.

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 = 'Audi'
>>> car.lower() == 'audi'
True

This test will return True no matter how the value 'Audi' is formatted because the test is now case insensitive. The lower() method doesn’t change the value that was originally stored in car, so you can do this kind of comparison without affecting the original variable:

>>> car = 'Audi'
>>> car.lower() == 'audi'
True
>>> car
'Audi'

We first assign the capitalized string 'Audi' to the variable car. Then, we convert the value of car to lowercase and compare the lowercase value to the string 'audi'. The two strings match, so Python returns True. We can see that the value stored in car has not been affected by the lower() method.

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 use the inequality operator (!=). 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:

toppings.py
requested_topping = 'mushrooms'

if requested_topping != 'anchovies':
    print("Hold the anchovies!")

This code compares the value of requested_topping to the value 'anchovies'. If these two values do not match, Python returns True and executes the code following the if statement. If the two values match, Python returns False and does not run the code following the if statement.

Because the value of requested_topping is not 'anchovies', the print() function is executed:

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.

Numerical Comparisons

Testing numerical values is pretty straightforward. For example, the following code checks whether a person is 18 years old:

>>> age = 18
>>> age == 18
True

You can also test to see if two numbers are not equal. For example, the following code prints a message if the given answer is not correct:

magic_number.py
answer = 17
if answer != 42:
    print("That is not the correct answer. Please try again!")

The conditional test passes, because the value of answer (17) is not equal to 42. Because the test passes, the indented code block is executed:

That is not the correct 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

Each mathematical comparison can be used as part of an if statement, which can help you detect the exact conditions of interest.

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.

For example, you can check whether two people are both over 21 by using the following test:

>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 and age_1 >= 21  (1)
False
>>> age_1 = 22                   (2)
>>> age_0 >= 21 and age_1 >= 21
True
1 The test on the left passes, but the test on the right fails, so the overall expression evaluates to False.
2 After changing age_1 to 22, both individual tests pass, causing the overall expression to evaluate as True.

To improve readability, you can use parentheses around the individual tests, but they are not required:

(age_0 >= 21) and (age_1 >= 21)

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.

Let’s consider two ages again, but this time we’ll look for only one person to be over 21:

>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 or age_1 >= 21  (1)
True
>>> age_0 = 18
>>> age_0 >= 21 or age_1 >= 21  (2)
False
1 Because the test for age_0 passes, the overall expression evaluates to True.
2 After lowering age_0 to 18, both tests fail and the overall expression evaluates to False.

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.

>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False

The keyword in tells Python to check for the existence of 'mushrooms' and 'pepperoni' in the list requested_toppings. This technique is quite powerful because you can create a list of essential values, and then easily check whether the value you’re testing matches one of the values in the list.

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:

banned_users.py
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.")

The if statement here reads quite clearly. If the value of user is not in the list banned_users, Python returns True and executes the indented line.

The user 'marie' is not in the list banned_users, so she sees a message inviting her to post a response:

Marie, you can post a response if you wish.

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:

game_active = True
can_edit = False

Boolean values provide an efficient way to track the state of a program or a particular condition that is important in your program.

Try It Yourself

5-1. Conditional Tests: Write a series of conditional tests. Print a statement describing each test and your prediction for the results of each test. Your code should look something like this:

car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')

print("Is car == 'audi'? I predict False.")
print(car == 'audi')

Look closely at your results, and make sure you understand why each line evaluates to True or False. Create at least 10 tests. Have at least 5 tests evaluate to True and another 5 tests evaluate to False.

5-2. More Conditional Tests: You don’t have to limit the number of tests you create to 10. If you want to try more comparisons, write more tests and add them to conditional_tests.py. Have at least one True and one False result for each of the following: tests for equality and inequality with strings; tests using the lower() method; numerical tests involving equality and inequality, greater than and less than, greater than or equal to, and less than or equal to; tests using the and keyword and the or keyword; a test whether an item is in a list; a test whether an item is not in a list.

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.

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:

voting.py
age = 19
if age >= 18:
    print("You are old enough to vote!")

Python checks to see whether the value of age is greater than or equal to 18. It is, so Python executes the indented print() call:

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 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 = 19
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")

The conditional test passes, and both print() calls are indented, so both lines are printed:

You are old enough to vote!
Have you registered to vote yet?

If the value of age is less than 18, this program would produce no output.

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:                                              (1)
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")
else:                                                      (2)
    print("Sorry, you are too young to vote.")
    print("Please register to vote as soon as you turn 18!")
1 If the conditional test passes, the first block of indented print() calls is executed.
2 If the test evaluates to False, the else block is executed.

Because age is less than 18 this time, the conditional test fails and the code in the else block is executed:

Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!

This code works because it has only two possible situations to evaluate: a person is either old enough to vote or not old enough to vote. The if-else structure works well in situations in which you want Python to always execute one of two possible actions. In a simple if-else chain like this, one of the two actions will always be executed.

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, and admission for anyone age 18 or older is $40.

The following code tests for the age group of a person and then prints an admission price message:

amusement_park.py
age = 12

if age < 4:                                   (1)
    print("Your admission cost is $0.")
elif age < 18:                                (2)
    print("Your admission cost is $25.")
else:                                         (3)
    print("Your admission cost is $40.")
1 The if test checks whether a person is under 4 years old.
2 The elif line is another if test, which runs only if the previous test failed. At this point we know the person is at least 4.
3 If both the if and elif tests fail, Python runs the else block.

In this example the if test evaluates to False, so its code block is not executed. The elif test evaluates to True (12 is less than 18) so its code is executed:

Your admission cost is $25.

Rather than printing the admission price within the if-elif-else block, it would be more concise to set just the price inside the chain and then have a single print() call that runs after the chain has been evaluated:

age = 12

if age < 4:
    price = 0
elif age < 18:
    price = 25
else:
    price = 40

print(f"Your admission cost is ${price}.")

The indented lines set the value of price according to the person’s age. After the price is set by the if-elif-else chain, a separate unindented print() call uses this value to display a message reporting the person’s admission price. This code produces the same output as the previous example, but the purpose of the chain is narrower and easier to modify — to change the output message, you need to change only one print() call rather than three.

Using Multiple elif Blocks

You can use as many elif blocks in your code as you like. For example, if the amusement park were to implement a discount for seniors, you could add one more conditional test to determine whether someone qualifies for the senior discount. Let’s say that anyone 65 or older pays half the regular admission, or $20:

age = 12

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}.")

The second elif block now checks to make sure a person is less than age 65 before assigning them the full admission rate of $40. Notice that the value assigned in the else block needs to be changed to $20, because the only ages that make it to this block are for people 65 or older.

Omitting the else Block

Python does not require an else block at the end of an if-elif chain. Sometimes, an else block is useful. Other times, it’s clearer to use an additional elif statement that catches the specific condition of interest:

age = 12

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}.")

The final elif block assigns a price of $20 when the person is 65 or older, which is a little clearer than the general else block. With this change, every block of code must pass a specific test in order to be executed.

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’re testing for, consider using a final elif block and omit the else block. As a result, you’ll be more confident 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 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:

toppings.py
requested_toppings = ['mushrooms', 'extra cheese']

if 'mushrooms' in requested_toppings:
    print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:  (1)
    print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
    print("Adding extra cheese.")

print("\nFinished making your pizza!")
1 The test for pepperoni is another simple if statement, not an elif or else statement, so this test is run regardless of whether the previous test passed or not.

Because every condition in this example is evaluated, both mushrooms and extra cheese are added to the pizza:

Adding mushrooms.
Adding extra cheese.

Finished 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. Here’s what that would look like:

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!")

The test for 'mushrooms' is the first test to pass, so mushrooms are added to the pizza. However, 'extra cheese' and 'pepperoni' are never checked, because Python doesn’t run any tests beyond the first test that passes in an if-elif-else chain. The customer’s first topping will be added, but all of their other toppings will be missed:

Adding mushrooms.

Finished 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.

Try It Yourself

5-3. Alien Colors #1: Imagine an alien was just shot down in a game. Create a variable called alien_color and assign it a value of 'green', 'yellow', or 'red'. Write an if statement to test whether the alien’s color is green. If it is, print a message that the player just earned 5 points. Write one version of this program that passes the if test and another that fails.

5-4. Alien Colors #2: Choose a color for an alien as you did in Exercise 5-3, and write an if-else chain. If the alien’s color is green, print a statement that the player just earned 5 points for shooting the alien. If the alien’s color isn’t green, print a statement that the player just earned 10 points. Write one version of this program that runs the if block and another that runs the else block.

5-5. Alien Colors #3: Turn your if-else chain from Exercise 5-4 into an if-elif-else chain. If the alien is green, print a message that the player earned 5 points. If the alien is yellow, print a message that the player earned 10 points. If the alien is red, print a message that the player earned 15 points. Write three versions of this program, making sure each message is printed for the appropriate color alien.

5-6. Stages of Life: Write an if-elif-else chain that determines a person’s stage of life. Set a value for the variable age, and then: if the person is less than 2 years old, print a message that the person is a baby; if at least 2 but less than 4, print that the person is a toddler; if at least 4 but less than 13, print that the person is a kid; if at least 13 but less than 20, print that the person is a teenager; if at least 20 but less than 65, print that the person is an adult; if 65 or older, print that the person is an elder.

5-7. Favorite Fruit: Make a list of your three favorite fruits and call it favorite_fruits. Write five if statements. Each should check whether a certain kind of fruit is in your list. If the fruit is in your list, the if block should print a statement, such as You really like bananas!

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 efficiently manage changing conditions, 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 chapter 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:

toppings.py
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:
    print(f"Adding {requested_topping}.")

print("\nFinished making your pizza!")

The output is straightforward because this code is just a simple for loop:

Adding mushrooms.
Adding green peppers.
Adding extra cheese.

Finished making your pizza!

But what if the pizzeria runs out of green peppers? An if statement inside the for loop can handle this situation appropriately:

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!")

This time, we check each requested item before adding it to the pizza. The if statement checks to see if the person requested green peppers. If so, we display a message informing them why they can’t have green peppers. The else block ensures that all other toppings will be added to the pizza.

The output shows that each requested topping is handled appropriately:

Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.

Finished 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:

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?")

This time we start out with an empty list of requested toppings. Instead of jumping right into a for loop, we do a quick check first. When the name of a list is used in an if statement, Python returns True if the list contains at least one item; an empty list evaluates to False. If requested_toppings passes the conditional test, we run the same for loop we used in the previous example. If the conditional test fails, we print a message asking the customer if they really want a plain pizza with no toppings.

The list is empty in this case, so the output asks if the user really wants a plain pizza:

Are you sure you want a plain 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_toppings = ['mushrooms', 'olives', 'green peppers',
                      'pepperoni', 'pineapple', 'extra cheese']

requested_toppings = ['mushrooms', 'french fries', 'extra cheese']  (1)

for requested_topping in requested_toppings:
    if requested_topping in available_toppings:  (2)
        print(f"Adding {requested_topping}.")
    else:                                        (3)
        print(f"Sorry, we don't have {requested_topping}.")

print("\nFinished making your pizza!")
1 The customer has requested an unusual topping: 'french fries'.
2 Inside the loop, we check to see if each requested topping is actually in the list of available toppings.
3 If the requested topping is not available, the else block runs.
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.

Finished making your pizza!

In just a few lines of code, we’ve managed a real-world situation pretty effectively!

Try It Yourself

5-8. Hello Admin: Make a list of five or more usernames, including the name 'admin'. Imagine you are writing code that will print a greeting to each user after they log in to a website. Loop through the list, and print a greeting to each user. If the username is 'admin', print a special greeting, such as Hello admin, would you like to see a status report? Otherwise, print a generic greeting, such as Hello Jaden, thank you for logging in again.

5-9. No Users: Add an if test to hello_admin.py to make sure the list of users is not empty. If the list is empty, print the message We need to find some users! Remove all of the usernames from your list, and make sure the correct message is printed.

5-10. Checking Usernames: Do the following to create a program that simulates how websites ensure that everyone has a unique username. Make a list of five or more usernames called current_users. Make another list of five usernames called new_users. Make sure one or two of the new usernames are also in the current_users list. Loop through the new_users list to see if each new username has already been used. If it has, print a message that the person will need to enter a new username. If a username has not been used, print a message saying that the username is available. Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.

5-11. Ordinal Numbers: Ordinal numbers indicate their position in a list, such as 1st or 2nd. Most ordinal numbers end in th, except 1, 2, and 3. Store the numbers 1 through 9 in a list. Loop through the list. Use an if-elif-else chain inside the loop to print the proper ordinal ending for each number. Your output should read 1st 2nd 3rd 4th 5th 6th 7th 8th 9th, and each result should be on a separate line.

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 ==, >=, and <=. 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.

Try It Yourself

5-12. Styling if Statements: Review the programs you wrote in this chapter, and make sure you styled your conditional tests appropriately.

5-13. Your Ideas: At this point, you’re a more capable programmer than you were when you started this book. Now that you have a better sense of how real-world situations are modeled in programs, you might be thinking of some problems you could solve with your own programs. Record any new ideas you have about problems you might want to solve as your programming skills continue to improve. Consider games you might want to write, datasets you might want to explore, and web applications you’d like to create.

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 need 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.

In Chapter 6 you’ll learn about Python’s dictionaries. A dictionary is similar to a list, but it allows you to connect pieces of information. You’ll learn how to build dictionaries, loop through them, and use them in combination with lists and if statements. Learning about dictionaries will enable you to model an even wider variety of real-world situations.

Applied Exercises: Ch 5 — if Statements

These exercises cover the same concepts as the chapter but use context from real infrastructure, network security, and language learning work. Save each as a separate .py file using lowercase and underscores, e.g. ise_auth_check.py.

Domus Digitalis / Homelab

D5-1. Node Status Check: Create a variable called node_status and assign it a value of 'up' or 'down'. Write an if-else statement that prints Node is healthy. if the status is 'up', and ALERT: Node is unreachable. if the status is 'down'. Test both branches.

D5-2. VLAN Authorization: Create a list called authorized_vlans containing the VLAN IDs (as integers) that are allowed to reach a sensitive segment. Write an if statement that checks whether a specific VLAN ID is in the list and prints either Access granted. or Access denied.

D5-3. Service Health Chain: Create a variable called service and assign it one of: 'k3s', 'vault', 'freeipa', or 'unknown'. Write an if-elif-else chain that prints a specific health-check command for each known service (e.g., kubectl get nodes for k3s), and prints Unknown service — skipping. for anything else.

D5-4. BGP Peer Check: Create a list called active_peers containing the names or IPs of your active BGP peers (e.g., iFog, ROUTE64). Create a variable peer with a value to check. Use in and not in to print whether the peer is currently active or not established.

D5-5. Stack Boot Order: Create a list of Domus Digitalis services in boot order. Loop through the list. Use an if statement inside the loop to print CRITICAL — starting first: <service> for the first three services (core layer), and Starting: <service> for all others. After the loop, print Boot sequence complete.

CHLA / ISE / Network Security

C5-1. Auth Protocol Check: Create a variable called auth_protocol and assign it one of: 'EAP-TLS', 'EAP-TEAP', 'MSCHAPv2', or 'unknown'. Write an if-elif-else chain that prints whether the protocol is preferred, acceptable, deprecated, or unrecognized.

C5-2. Endpoint Group Membership: Create a list called quarantine_groups containing ISE endpoint group names that trigger quarantine. Create a variable device_group with a group name to check. Use in and not in to print whether the device should be quarantined or allowed on the network.

C5-3. Syslog Severity Router: Create a variable called severity and assign it an integer (0–7, following syslog conventions). Write an if-elif-else chain that prints the severity label: 0=Emergency, 1=Alert, 2=Critical, 3=Error, 4=Warning, 5=Notice, 6=Informational, 7=Debug. Any value outside 0–7 should print Invalid severity level.

C5-4. Pipeline Destination Check: Create a list called sentinel_destinations representing log types routed to Sentinel Analytics (hot tier). Loop through a list of incoming log types and use an if-else inside the loop to print either Routing to Sentinel Analytics: <type> or Routing to ADLS cold storage: <type>.

C5-5. Empty Source Check: Create an empty list called log_sources. Write an if statement that checks whether the list is empty. If it is, print No log sources configured — pipeline will not start. Then add sources to the list and confirm the check passes.

General Sysadmin / Linux

L5-1. Service State Check: Create a variable called service_state and assign it 'active', 'inactive', or 'failed'. Write an if-elif-else chain that prints an appropriate message for each state, such as Service is running normally., Service is stopped., or ALERT: Service has failed — investigate immediately.

L5-2. Allowed Users: Create a list called sudo_users containing usernames allowed to run privileged commands. Create a variable user with a username to check. Use in and not in to print whether the user has sudo access or is denied.

L5-3. Disk Usage Alert: Create a variable called usage_percent and assign it an integer. Write an if-elif-else chain that prints: OK if usage is below 70, WARNING if 70–89, CRITICAL if 90 or above. Test all three ranges.

L5-4. Package Manager Check: Create a list called installed_packages. Loop through a list of required packages and use an if-else inside the loop to print either OK: <package> is installed. or MISSING: <package> needs to be installed.

L5-5. Empty Job Queue: Create an empty list called cron_jobs. Write an if statement that checks whether the list is empty and prints No scheduled jobs found. if it is. Then populate the list and loop through it, printing each job name.

Spanish / DELE C2

E5-1. Dificultad del Capítulo: Create a variable called capitulo and assign it a chapter number from Don Quijote. Write an if-elif-else chain that prints a difficulty label: chapters 1–10 are introductorio, 11–20 are intermedio, 21–30 are avanzado, and anything above 30 is maestro. Test all four ranges.

E5-2. Vocabulary in Context: Create a list called palabras_conocidas with Spanish words you’ve already studied. Create a variable palabra_nueva with a word to check. Use in and not in to print either Ya conoces esta palabra. or Palabra nueva — añadir al vocabulario.

E5-3. DELE Level Check: Create a variable called nivel_actual and assign it a DELE level string (e.g., 'B2'). Write an if-elif-else chain that prints a study recommendation for each level: A1/A2 print Sigue practicando lo básico., B1/B2 print Buen progreso — enfócate en gramática avanzada., C1 prints Casi en la cima — practica redacción formal., C2 prints ¡Nivel maestro alcanzado!

E5-4. Study Session Check: Create a list called temas_completados representing DELE study topics you’ve finished. Loop through a list of all required topics and use an if-else inside the loop to print either ✓ Completado: <tema> or ✗ Pendiente: <tema>.

E5-5. Empty Reading List: Create an empty list called capitulos_pendientes. Write an if statement that checks whether the list is empty and prints ¡Has terminado todos los capítulos pendientes! if it is. Then populate the list and loop through it, printing each chapter number with Capítulo <N> — por leer.