In this article, let us see how to use the if statement with 2 conditions. This article assumes you have a basic understanding of the if statement beforehand. If you don’t, you can take 5 mins to read it here.
Assuming you are ready, let’s begin!
How to use 2 conditions with an “if” statement?
The if statement is used to execute a statement based on a conditional statement. For example
if (2 > 1): print("2 is greater than 1!")
Now as we can see from the above example, we used only 1 condition (2>1). But what if we needed to determine 2 conditions at the same time before we executed a line? Fortunately, Python allows us to use 2 conditional statements in a single if statement.
Let’s see a simple example:
x = 5 if (x > 1) and (x < 10): print("x is greater than 1 and less than 10!")
The above example checked 2 conditions, first, it was (x > 1), and second, it was ( x < 10). Only after it was determined that both conditions were true, the if block was executed.
The picture below explains the various elements of the syntax!
The <logical operator> here is used to combine boolean conditional statements to form a single compound expression.
In this article, we’ll take a look at some examples with the “and” operator and the “or” operator.
Using 2 conditions 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!
The following picture should give you more clarity
Let’s see an example:
username = input("Please enter your username: ") password = input("Please enter your password: ") valid_username = "admin" valid_password = "secret1234" if (username == valid_username) and (password == valid_password): print("Access granted!")
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!
The above example perfectly describes the use case of the “and” operator.
- It checks the first condition of the username, evaluates it to be True,
- then it checks the second condition of the password, which is also evaluated to be True.
Since both are qualified as True, the combined condition is also evaluated as True. Hence the if block is executed.
If at least one of those conditions isn’t met, then the combined condition would have been evaluated to False and the print statement inside the if-block would have been skipped!
Using 2 conditions 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!
The following picture should give you more clarity
Let us see an example with the usage of the ‘or‘ operator!
num = int(input("Please enter a number: ")) if num > 0 or num % 2 == 0: print("Your number is either positive or even or both!")
This example is pretty straightforward, when you enter a number, the if statement evaluates if the number is either positive or even.
- The condition (num>0) checks if the number is positive and
- the condition (num%2) checks if the number is divisible by 2.
The if block will be executed even if one of the conditions is evaluated to be True!
I leave it to you to try it out yourself!
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
The if statement with 2 conditions can be varied and in fact, can be brushed up to improve the readability of the code.
We can do this by evaluating the conditions before we execute the if statement itself. Let’s see an example.
This is a variation of a program we used earlier:
username = input("Please enter your username: ") password = input("Please enter your password: ") valid_username = "admin" valid_password = "secret1234" is_username_valid = (username == valid_username) is_password_valid = (password == valid_password) if is_username_valid and is_password_valid: print("Access granted!")
If you have been following along carefully, you would have spotted the difference! In this case, we evaluated the condition before the if statement.
The variables is_username_valid and is_password_valid have evaluated the condition and then they store the bool value of those conditions. These are in turn used in the if statement which directly evaluates it!
In this way, the if statement is made more simple and more readable overall.
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!