How To Check For Multiple Conditions In An If Statement In Python

The if statement only checks one condition and returns either True or False. The question is, how to check for multiple conditions in an if statement in Python? There are many ways to do this job. In this article, we will find out the top ways to check multiple conditions using an if statement.

How to check for multiple conditions in an if statement in Python

Python comes up with two built-in logical operators and and or. Below  is how to check for multiple conditions in an if statement in Python using these two operators. In the samples, we check the discrete variables in the conditions using the == operator. But you can also use other operators, such as !=, >, <, <=, >=, and so on!

Using the logical operator ‘and’

The and logical operators check the conditions and return True, when all conditions are True. The and operator returns the False value whenever there are one or more conditions in the if statement that has the False value.

The syntax for using and logical operator in an if statement is:

if [condition1] and [condition2] and … [conditionN]:
   [code]
else:
   [code]

Here is a code sample:

# Assign the value True to the variable condition1
condition1 = True

# Assign the value True to the variable condition2
condition2 = True

# Assign the value True to the variable condition3
condition3 = True

# Check multiple conditions using the logical operator 'and'
if condition1 == True and condition2 == True and condition3 == True:
    print('The result is True')
else:
    print('The result is False')

The output will be:

The result is True

The result will be a False in case one or more conditions in the if statement is False.

Here is the code sample:

# Assign the value False to the variable condition1
condition1 = False

# Assign the value True to the variable condition2
condition2 = True

# Assign the value True to the variable condition3
condition3 = True

# Check multiple conditions using the logical operator 'and'
if condition1 == True and condition2 == True and condition3 == True:
    print('The result is True')
else:
    print('The result is False')

The output will be:

The result is False

Using the logical operator ‘or’

The or logical operator returns True, when there are one or more conditions in the if statement that has the True value. It does not care about how many False conditions there are. As long as there is a True condition, the operator will return True. 

The syntax for using or logical operator in an if statement is:

if [condition1] or [condition2] or … [conditionN]:
   [code]
else:
   [code]

Here is a code sample:

# Assign the value False to the variable condition1
condition1 = True

# Assign the value True to the variable condition2
condition2 = False

# Assign the value True to the variable condition3
condition3 = False

# Check multiple conditions using the logical operator 'and'
if condition1 == True or condition2 == True or condition3 == True:
    print('The result is True')
else:
    print('The result is False')

The output will be:

The result is True

The logical operator only returns False when all the conditions are False in an if statement.

Here is the code sample:

# Assign the value False to the variable condition1
condition1 = False

# Assign the value False to the variable condition2
condition2 = False

# Assign the value False to the variable condition3
condition3 = False

# Check multiple conditions using the logical operator 'and'
if condition1 == True or condition2 == True or condition3 == True:
    print('The result is True')
else:
    print('The result is False')

The output will be:

The result is False

Summary

With the help of the two built-in logical operators and and or, developers can now easily check for multiple conditions in an if statement in Python. You should learn these operators, as they will make your Python program more efficient.

Leave a Reply

Your email address will not be published. Required fields are marked *