If with multiple “and” conditions in Python: Explained With Examples

In this article, let us see some examples of how to write if statements with multiple and conditions in Python.

Let us start with the simple case of just 2 conditions!

Example for “condition1 and condition2

Before we look at the example, one point worth noting is that

The ‘and’ operator returns False even if one condition is found to be False, no matter how many conditions there are!

Here’s a program that checks if the username and password are a correct match by checking 2 conditions via the ‘and’ operator.

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!

Python’s “==” Explained Using Examples!

In this program, the ‘and’ operator checks the first condition of username and then it checks the second condition of password. if both are qualified as True, the combined condition is evaluated as True, and then the if block is executed.

If even one of those conditions wasn’t met, then the combined condition would be evaluated as False and the if block will not be executed.

Also, notice how natural Python language feels when writing if statements with multiple conditions!

Next, let’s kick things up a notch and see another example this time with 3 conditions!

Example for “condition1 and condition2 and condition3”

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

Please enter a number: 12
Your number is a positive even multiple of 3!

This code simply checks the following 3 conditions

  1. if a given number is a positive integer,
  2. if it is even, and
  3. if it is a multiple of 3.

The ‘and’ operator evaluates all the 3 conditions. If any condition falls short, the whole condition returns False to the if statement

Remember, the ‘and’ operator returns False even if one condition is found to be False!

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“!

Python “if” Statement with Multiple Conditions: Explained with Examples!

Variations of writing the if statement

The if statement can be also written in different ways than the one we saw above. For example, instead of evaluating the conditions in the if statement line itself, we can evaluate the condition before the if statement.

Let’s try this for both the login.py and num_check.py programs we saw 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!")

The variables is_username_valid and is_password_valid evaluate the conditions and store the results (i.e. the boolean values). 

These are in turn used in the if statement which directly evaluates it further. Pretty neat right?

Let’s do the same for the num_check.py program:

Challenge time

Theory only gets you so far! It’s time to practice! 

The challenge for you is to refer back to the original num_check.py and rewrite it just like we did for the login.py program. 

You will need to check the conditions and store them in variables. All the best!

For those of you who didn’t get it, no worries! Here’s the answer:

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 into one 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!

Related Articles

If with multiple “or” conditions in Python: Examples

Python “if” Statement with Multiple Conditions: Explained with Examples!

Python: “if not true” How to write? Explained with Examples!

If-Else in Python: Explained with Examples!

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Editor
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn