Python has three Boolean operators: and, or, and not. You can use them to test situations and determine which execution path your programs will take. You’ll learn about Python or operators and how to use them, in this article.
Python’s Boolean Operators
Python contains three Boolean operators that are written in plain English:
- and
- or
- not
These operators join Boolean expressions (as well as objects) to form compound Boolean expressions.
Because the Python Boolean operators always take two Boolean expressions or two objects or a combination of the two, they are classified as binary operators.
In this article, you’ll learn about the Python or operator, which implements the logical OR operation in Python. You’ll discover how it works and how to apply it.
OR Operator in Python
The Python OR Operator accepts at least two boolean expressions and returns True if any of them is True. It returns False if all of the expressions are False.
Flowchart of Python OR Operator
Truth Table for Python OR Operator
Expression 1 | Expression 2 | Result |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Using the OR Operator in Python with a Boolean Expression
If any of the boolean expressions supplied is True, the Python OR operator returns True.
Example: Or Operator with Boolean Expression
bool1
=
2
>
3
bool2
=
2
<
3
print
(
'bool1:'
, bool1)
print
(
'bool2:'
, bool2)
# or operator
OR
=
bool1
or
bool2
print
(
"OR operator:"
, OR)
Output
bool1: False bool2: True OR operator: True
Using the OR Operator in Python in if
In the if statement, we can utilize the OR operator. It can be used when we wish to execute the if block if any of the conditions turns True.
Example: Or Operator with if statement
# or operator with if
def
fun(a):
if
a >
=
5
or
a <
=
15
:
print
(
'a lies between 5 and 15'
)
else
:
print
(
'a is either less than 5 or greater than 15'
)
# driver code
fun(
10
)
fun(
20
)
fun(
5
)
Output
a lies between 5 and 15 a lies between 5 and 15 a lies between 5 and 15
We can see from the output that the code for the if statement is always executed. Because one of the boolean expressions will always be True for any value of a the else block will never be executed.
Short Circuit Python OR Operator
The Python Or operator always evaluates the statement until it finds a True, and once that happens, the rest of the expression is ignored. Consider the following example to gain a better idea.
Example: Short Circuit in Python OR Operator
# short circuit in Python or operator def true(): print ( "Inside True" ) return True def false(): print ( "Inside False" ) return False case1 = true() or false() print ( "Case 1" ) print (case1) print () case2 = true() or true() print ( "Case 2" ) print (case2) print () case3 = false() or false() print ( "Case 3" ) print (case3) print () case4 = false() or true() print ( "Case 4" ) print (case4) |
Output
Inside True Case 1 True Inside True Case 2 True Inside False Inside False Case 3 False Inside False Inside True Case 4 True
Conclusion
We taught how to utilize Python or logical operator with boolean expression, python in if, and short circuit with well-explained examples in this Python Examples course.
Frequently Asked Questions
In Python, How Do You Check or Condition?
In Python, an if-else conditional statement is used when a scenario leads to two conditions, one of which must be true.
In Python, What Are or $ and?
And $ or (together with not) are logical operators in Python. Both require two operands that can be true or false. Only if both operands are True does the and operator return True.
In Python, What is the Difference Between () and []?
() represents a tuple: An unchanging set of values, typically (but not always) of diverse types. [] contains a list: A modifiable collection of values that are usually (but not always) of the same type.