Python “OR” operator will use in the if statement to test conditions and decide which execution path your programs will take.
A if statement with or operator in Python
Simple python example code using “OR” logical operator that evaluates as True if any of the operands is True.
An example of an OR operator
Use python or logical operator to form a compound logical expression. OR logical operator returns True if any of the operands provided to it evaluates to true.
Taking input value from the user in this example.
x = int(input('Enter your age: ')) if x < 21 or x > 100: print('You are too young or too old, go away!') else: print('Welcome, you are of the right age!')
Using multiple OR operator example
Example how to evaluate more than two expressions by using the OR operator.
a = 'Text' b = 33 c = 'Python' if (a == 'Text') or (b == 20) or (c == 'Python'): print("True") else: print("All are False")
Example ‘OR’ with ‘AND’ operator
See a Python example code of using the ‘and’ and ‘or’ operators in if statement.
a = 5 b = 10 c = 15 d = 20 if (a > 0 or b == 15) and (c == 16 or d == 20): print("True") else: print("False")
Here’s a summary of the Python
or operator’s behavior:
|Result of ||Result of ||Result of |
Do comment if you have any doubts and suggestions on this Python code.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.