The Wayback Machine - https://web.archive.org/web/20250108130124/https://www.geeksforgeeks.org/python-or-operator/
Open In App

Python OR Operator

Last Updated : 21 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python OR Operator takes at least two boolean expressions and returns True if any one of the expressions is True. If all the expressions are False then it returns False.

Flowchart of Python OR Operator

Python-logical-or-operator

Truth Table for Python OR Operator

Expression 1Expression 2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Using Python OR Operator with Boolean Expression

Python OR operator returns True in any one of the boolean expressions passed is True.

Example: Or Operator with Boolean Expression

Python
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 Python OR Operator in if

We can use the OR operator in the if statement. We can use it in the case where we want to execute the if block if any one of the conditions becomes if True.

Example: Or Operator with if statement

Python
# or operator with if
def fun(a):
    if a % 5 == 0 or a % 3 == 0:
        print('a either a multiple of 3 or 5')
    else:
        print('a is not a multple of 3 or 5')


# driver code
fun(10)
fun(22)
fun(5)

Output
a either a multiple of 3 or 5
a is not a multple of 3 or 5
a either a multiple of 3 or 5


Python OR Operator – Short Circuit

The Python Or operator always evaluates the expression until it finds a True and as soon it Found a True then the rest of the expression is not checked. Consider the below example for better understanding.

Example: Short Circuit in Python OR Operator

Python
# 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

From the above example, we can see that the short circuit or lazy evaluation is followed. In case1 and case2, the second expression is not evaluated because the first expression returns True, whereas, in case3 and case4 the second expression is evaluated as the first expression does not returns True.

Python OR Operator – FAQs

What are the 7 Operators in Python?

Python supports several types of operators, here categorized into seven primary types:

  1. Arithmetic Operators: +, -, *, /, % (modulus), ** (exponentiation), and // (floor division).
  2. Comparison (Relational) Operators: ==, !=, >, <, >=, <=.
  3. Assignment Operators: =, +=, -=, *=, /=, %=, **=, //=.
  4. Logical Operators: and, or, not.
  5. Bitwise Operators: &, |, ^, ~, <<, >>.
  6. Membership Operators: in, not in.
  7. Identity Operators: is, is not.

These operators allow for complex expressions and control structures in programming by facilitating arithmetic operations, data comparisons, logical operations, and more.

What is %= Operator in Python?

The %= operator in Python is an assignment operator that takes the modulus of the variable on the left with the value on the right and then assigns the result back to the variable on the left.

Example:

x = 10
x %= 3 # x = x % 3
print(x) # Output will be 1

What is |= in Python?

The |= operator in Python is a bitwise OR assignment operator. It performs a bitwise OR operation between the original variable and the value on the right, and then assigns the result back to the variable.

Example:

x = 1  # Binary: 01
x |= 2 # Binary: 10
print(x) # Output will be 3, Binary: 11

What are the 6 Relational Operators in Python?

The six relational operators in Python used for comparing values are:

  1. == (equal to)
  2. != (not equal to)
  3. > (greater than)
  4. < (less than)
  5. >= (greater than or equal to)
  6. <= (less than or equal to)

These operators are used to compare two values and return a Boolean value based on whether the comparison is true or false.

Can We Use *= in Python?

Yes, the *= operator in Python is an assignment operator that multiplies the variable on the left by the value on the right and then assigns the result back to the variable.

Example:

x = 5
x *= 3 # x = x * 3
print(x) # Output will be 15

This operator is commonly used for scaling values or repeatedly applying a multiplicative factor to a variable, streamlining the code by avoiding more verbose expressions



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg