Python If OR

Python If OR

You can combine multiple conditions into a single expression in an If statement, If-Else statement, or Elif statement using logical operator OR.

Python OR logical operator returns True if at least one of the two operands provided is True. Refer Python OR logical operator tutorial for syntax and truth table.

Examples

In the following examples, we will see how we can use Python OR logical operator to form a compound condition in conditional statement.

1. Python If with OR operator in condition

In the following example, we will learn how to use Python or operator to join two simple boolean conditions to form a compound boolean condition in a Python If statement.

Python Program

today = 'Saturday'

if today=='Sunday' or today=='Saturday':
	print('Today is off. Rest at home.')
Run Code Copy

Output

Today is off. Rest at home.

Here, today=='Sunday' and today=='Saturday' are two simple conditions. We have used and operator to join these two simple conditions and create a compound condition: today=='Sunday' or today=='Saturday' .

2. Python If-Else with OR operator in condition

In the following example, we will use or operator to combine two basic conditional expressions in boolean expression in a Python If Else statement.

Python Program

today = 'Wednesday'

if today=='Sunday' or today=='Saturday':
	print('Today is off. Rest at home.')
else:
	print('Go to work.')
Run Code Copy

Output

Go to work.

3. Python Elif with OR operator in condition

In the following example, we will use or operator to combine two basic conditional expressions in boolean expression of a Python elif statement.

Python Program

today = 'Sunday'

if today=='Monday':
	print('Your weekend is over. Go to work.')
elif today=='Sunday' or today=='Saturday':
	print('Today is off.')
else:
	print('Go to work.')
Run Code Copy

Output

Today is off.

Summary

In this tutorial, we learned how to use Python OR logical operator in If, If-Else and Elif conditional statements, with well detailed examples.

Frequently Asked Questions

1. Can I use if or in Python?

Answer:

Yes, you can use the or keyword in Python within an if statement to check if at least one of multiple conditions is true.

x = 5
if x == 5 or x == 10:
    print('x is either 5 or 10')

In this example, the or operator is used to combine two conditions: x == 5 and x == 10. The print statement will be executed because the condition x == 5 is true (x is indeed 5), even though the condition x == 10 is false.

2. What does an if-statement with or do in Python?

Answer:

In Python, an if-statement with the or operator evaluates to True if at least one of the conditions it combines evaluates to True. If none of the conditions evaluate to True, then the entire if-statement evaluates to False.

if condition1 or condition2:
    # code to execute if either condition1 or condition2 is True
3. When do you use or operator with an if-statement?

Answer:

You use the or operator with an if-statement in Python when you want to execute a block of code if at least one of multiple conditions is true. This is useful when you have multiple conditions and you want your code to proceed if any of those conditions are met.

Related Tutorials

Code copied to clipboard successfully 👍