Skip to content

Python If-Else Statements with Multiple Conditions

Python If-Else Statements with Multiple Conditions Cover Image

Conditional statements, or if-else statements, allow you to control the flow of your code. Understanding the power of if-else statements allows you to become a stronger Python programmer. This is because they allow you to execute only certain parts of your code if a condition or multiple conditions are met. In this tutorial, you’ll learn how to use Python if-else statements with multiple conditions.

By the end of this tutorial, you’ll have learned:

  • How to use Python if-else statements with multiple conditions
  • How to check if all conditions are met in Python if-else statements
  • How to check if only some conditions are met in Python if-else statemens
  • How to write complex if-else statements with multiple conditions in Python

Understanding Python if-else Statements

One of the benefits of Python is how clear the syntax is. This is also true for if-else statements, which follow simple English syntax in order to control flow of your program. Python if-else statements, allow you to run code if a condition is met. They follow the syntax shown below:

# Understanding Python if-else Statements
if condition:
   # run code
else:
   # run other code

To better understand how Python handles multiple conditions, it’s important to understand how logical operators work. Let’s take a look at this next.

Understanding Logical Operators in Python

While Python provides many more comparison operators, in this tutorial we’ll focus on two: and and or. These comparison operators allow us to chain multiple conditions using logical statements. To break this down:

  • and ensures that both conditions are met, otherwise returning False
  • or ensures that at least one condition must be true, otherwise returning False

Now that we’ve covered some of the basics, let’s dive into how to use multiple conditions in Python if-else statements.

Using Multiple Conditions in Python if-else Statements

In Python if-else statements, we can use multiple conditions which can be used with logical and and or operators. Let’s take a look at how we can write multiple conditions into a Python if-else statement:

# Using Multiple Conditons in Python if-else
val1 = 2
val2 = 10

if val1 % 2 == 0 and val2 % 5 == 0:
    print("Divisible by 2 and 5.")
else:
    print("Not divisible by both 2 and 5.")

# Returns: Divisible by 2 and 5.

We can see that the first condition uses the logical and operator to check if both conditions are true. If this isn’t met, the else block is executed. Let’s dive into how these operators work in action.

Checking For Multiple Conditions to be True in Python if-else Statements

We can use Python if-else statements to check that all conditions are true by using one or more and statements. This allows us to check that every condition is true. If a single condition is not true, then the flow statement moves onto the next condition.

Let’s see how we can check if multiple conditions are true in Python:

# Checking For Multiple Conditions to be True in Python if-else Statements
age = 32

if age >= 0 and age < 20:
    group = "0-19"
elif age >= 20 and age < 30:
    group = "20-29"
else:
    group = "30+"

print(f'Person is in group: {group}')

# Returns: Person is in group: 30+

Let’s break down what the code block above is doing:

  1. A variable, age, is defined with the value 32
  2. An if-else statement first checks if the age is 0 or older and less than 20. If either of these arent true, the next block is checked.
  3. The elif statement checks if the age is between 20 and 30. If this isn’t true, the else block is executed

With and conditions, Python will check each item sequentially. If one condition is not true, Python will not need to check the others and move onto the next block.

This is actually the same as running the all() function in a block, which will check if all items that are passed into the function are True. We could rewrite the if-else statement above using the all() function, as shown below:

# Checking For Multiple Conditions to be True in Python if-else Statements
age = 32

if all([age >= 0,  age < 20]):
    group = "0-19"
elif all([age >= 20, age < 30]):
    group = "20-29"
else:
    group = "30+"

print(f'Person is in group: {group}')

# Returns: Person is in group: 30+

Checking For Some Conditions to be True in Python if-else Statements

The Python or operator can be used to check if only one condition is true. This can allow you to write programs which can, for example, check if a weekday is a weekend day or not. This can allow us to check if a value passed meets either condition. Let’s see what this looks like:

# Checking For Some Conditions to be True in Python if-else Statements
weekday = 'Friday'

if weekday == 'Saturday' or weekday == 'Sunday':
    print("Yay! It's the weekend!")
else:
    print("Time to go to work.")

# Returns: Time to go to work.

In the code block above, we wrote an if-else block that checks whether the weekday we passed in is either Saturday or Sunday. If neither of these conditions evaluate to be true, the else block is executed.

This is actually the same as using the Python any() function, which will check if any of the items passed in are true. Let’s see how we can re-write our if-else block using the any() function:

# Checking For Some Conditions to be True in Python if-else Statements
weekday = 'Friday'

if any([weekday == 'Saturday', weekday == 'Sunday']):
    print("Yay! It's the weekend!")
else:
    print("Time to go to work.")

# Returns: Time to go to work.

In the next section, you’ll learn how to write complex if-else statements that combine and and or in creative ways.

Writing Complex if-else Statements with Multiple Conditions

We can write complex if-else statements with multiple conditions that combine using and and or operators. This can be very helpful when you’re writing complex conditional code that allows for some conditions to be true, while enforcing others.

Let’s take a look at an example of how this works:

# Writing Complex if-else Statements with Multiple Conditions
age = 67
active = True

if (age <= 18 or age >= 65) and active:
    print("Discount applies.")
else:
    print("No discount.")

# Returns: Discount applies.

In the code block above, we wrap the or statement in brackets. This means that this or condition only applies to the code in the brackets. Following that, Python will check if the user is active.

Let’s take a look at what this conditional flow looks like:

  1. Since age is 67, the code in the brackets will evaluate to True, since it meets the age >= 65 condition.
  2. Python then checks if the user is active, which evaluates to True as well

By doing this, Python allows you write complex conditions. By combining and and or operators, you can write if-else statements without nesting your conditions.

Conclusion

In this tutorial, you learned how to write complex if-else statements with multiple conditions. Python if-else statements allow you to control the flow of your code. By using multiple conditions, you can write more sophisticated code.

You first learned how to check if all conditions were true, using the and operator. Then, you learned how to check if any conditions were true, using the or operator. Finally, you learned how to write complex if-else statements by combining and and or.

Additional Resources

To learn more about related topics, check out the tutorials below:

Tags:

Leave a Reply

Your email address will not be published.