In this article, let’s look at constructing the ‘if statement’ with 3 conditions.
Alright, let’s begin!
How to write an if statement with 3 conditions?
Here’s the basic syntax of writing an if statement with 3 conditions:
Let’s see how to use this syntax with an example:
a = 1 b = 2 c = 3 if (a == 1) and (b == 2) and (c == 4): print("This line was printed because all 3 conditions are satisfied")
This line was printed because all 3 conditions are satisfied
If you are wondering what this double equal sign ( “==” ) is doing here, it is checking the equality of the objects on either side of it. If this is the first time you are coming across this “==” sign then I strongly suggest reading the article we made on that topic to get up to speed!
This was a simple example that checked 3 conditions before it executed the if block. Read on to understand how this exactly works!
If statement with 3 conditions combined with the ‘and’ operator
If you are just starting out in your programming journey, then I suggest you learn the following rule.
The ‘and’ operator returns False even if one condition is found to be False, no matter how many conditions there are!
Here’s a breakdown of how the ‘and’ operator works;
Let’s say we have 3 conditions to evaluate. We’ll be using 2 ‘and’ operators:
<condition1> and <condition2> and <condition3>
- <condition1> is True
- <condition2> is True and
- <condition3> is False.
This leads us to: True and True and False
Firstly, the Python interpreter will only evaluate the first 2 conditions (from left to right):
Now that we have only 2 conditions left, the second ‘and’ operator is used to evaluate these 2 and get the final result:
Here’s another example but slightly different:
True and False and True
In this statement, when the Python interpreter does the evaluation for the first 2 statements and returns False:
It doesn’t even bother evaluating the next 2 conditions:
This is because it knows that the ‘and’ condition returns False even if one single condition is False. The interpreter functions in this way to be more efficient.
The same process occurs for the following statements:
False and True and True
False and False and True
False and False and False
In all of those cases, only the first 2 conditions will be evaluated!
Let’s have one last example of using 3 conditions using and operator.
age = 20 is_citizen = True is_registered = True if (age >= 18) and is_citizen and is_registered: print("You are eligible to vote!")
You are eligible to vote!
This is a simple example that verifies if the user is eligible to vote based on 3 mandatory criteria. If any condition falls short, the whole condition will be evaluated to False.
By now I hope you have a good grasp of how the ‘and’ operator functions, next let’s have a look at the ‘or’ operator!
If statement with 3 conditions combined with the ‘or’ operator
What if want the combined condition to be evaluated as True even if just one of the conditions is evaluated as True? i.e if either condition1 or condition2 is True, we want the whole combined condition to be True. That’s where we use the or operator.
The rule for or operator is
The ‘or’ operator returns True even if one condition is found to be True, no matter how many conditions there are!
Here’s the syntax:
<condition1> or <condition2> or <condtion3>
Just like the ‘and’ operator, the ‘or’ operator handles multiple conditions by tackling them pair by pair.
So for a statement like this:
False or False or True
The first 2 conditions are evaluated (from left to right):
And then the last 2 statements are evaluated:
Here’s a second example:
True and False and True
In this combined evaluation, the first 2 conditions are evaluated and we already get the result for the whole combination without the need for the evaluation of the 3rd condition.
As usual, we start by evaluating the first 2 conditions:
Now instead of evaluating the last 2 conditions like this:
The ‘or’ statement directly returns True. That’s because the whole condition will be evaluated as True even if one condition is evaluated as True for the ‘or’ statement.
This is the same thing that happens in all the cases below:
True or False or False
False or True or True
False or True or False
True or True or True
True or True or False
Notice how there’s at least one True statement in the first 2 conditions, this is enough for the ‘or’ operator to determine the whole condition as True!
Let’s look at the another example:
experience_years = 3 completed_internship = True has_relevant_degree = False if experience_years >= 2 or completed_internship or has_relevant_degree : print("You are eligible to apply to MNCs!")
You are eligible to apply to MNCs!
The above if statement checks if the user is eligible to apply if they satisfy any of the given criteria and executes the if block subsequently.
Using both ‘and’ and ‘or’ operators in an if statement
Python allows us to use both the ‘and’ operator and the ‘or’ operator in a single complex if statement.
Let’s see how to make one:
shop_employee = True shop_customer = False shop_open = True if shop_open and (shop_customer or shop_employee): print("You can enter the shop!")
You can enter the shop!
In this example, the if statement checks if the shop is open and whether the visitor entering is either a customer or an employee.
Notice the usage of parantheses “(” and “)”.
The condition inside the parantheses are evaluated first , in this case the condition “shop_customer or shop_employee“.
Then the result of the evaluation is then combined with the condition “shop open” using the ‘and’ operator.
What will happen if we remove the brackets like this?
if shop_open and shop_customer or shop_employee:
Here, we can see how the aim of the if statement is not conveyed clearly to the interpreter.
With the brackets, the aim is that the shop should be open and the visitor must either be a customer or an employee.
Without the brackets, the interpreter will evaluate the condition in the following order:
- Evaluate the condition “shop_open and shop_customer“
- Then the result of the above evaluation is ‘or‘ed with the condition shop_employee
In our case, the condition shop_employee is True, hence it will always result in the output
You can enter the shop!
even if the shop_open condition is False.
Hence if you are mixing up and operator and or operator, don’t forget the use of parantheses!
The Fundamentals Of Writing If with Multiple Conditions
Learning from examples can only take you so far. If you wish to get your fundamentals strong and learn what happens behind the scenes, then I strongly suggest watching the video we made on that topic shown below.
You can only cover so much in a video, here is its “article-counterpart“!
Variations of writing the statement
This if statement can be also written in different expressions. For example, instead of evaluating the conditions in the if statement line itself, we can evaluate the condition before the if statement.
Let’s have a look at an example
num = int(input("Please enter a number: ")) if (num > 0) and (num % 2 == 0) and (num % 3 == 0): print("Your number is a positive even multiple of 3!")
This code simply checks if a given number is a positive integer, and if it is even, and if it is a multiple of 3 all in one line of the if statement.
Here’s a more neat way of writing the same program:
num = int(input("Please enter a number: ")) #All the below variables store the boolean value True is_positive = (num > 0) is_even = (num % 2 == 0) is_multiple_of_3 = (num % 3 == 0) #if True and True and True if is_positive and is_even and is_multiple_of_3: print("Your number is a positive even multiple of 3!")
By writing the boolean values of the condition beforehand and not dumping it all in the if statement line, we improve the readability of the code and avoid making the if statement look like a complex formula!
And with that, I will end this article.
Congratulations on making it to the end of the article, not many have the perseverance to do so!
I hope you enjoyed reading this article and found it useful!
Feel free to share it with your friends and colleagues!
If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!
Thanks to Namazi Jamal for his contributions in writing this article!