Wednesday, October 4, 2023
HomeProgrammingPythonOr Operator in Python

Or Operator in Python

This post was last Updated on October 24, 2022 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.

In this tutorial, you will learn about the or operator in Python. The “or” is one of the logical operators used in Boolean Algebra developed by George Boole. There are a total of three logical operators present in Python. They are:

  1. and
  2. or
  3. not

These logical operators connect individual boolean expressions to create compound boolean expressions.

Also ReadNatural Language Processing in Python: Techniques

Or Operator in Python

The or operator combines individual boolean expressions to form a single compound boolean expression, and the resultant compound boolean expression is True if at least one subexpression is True.

Syntax of or operator

Expression1 or Expression2 or Expression3…..

Let’s look into various cases/contexts where or operator is used.

Also ReadPython Reserved Words List With Definitions and Examples

Using or with Boolean Expressions

Boolean Expressions are expressions that result in a boolean value, i.e., either True or False. As discussed above, if at least one sub-expression in the compound expression is True, then the resultant value of the compound expression is also True. If none of the expressions is True, then the result of the compound expression formed using or is False.

Below is the working of or operator with Boolean Expressions

Expression 1 Expression 2 Expression 1 or Expression 2
 True True True
 True  False  True
 False  True  True
 False  False False

Below is an example program where or operator is used with boolean expressions.

Code

print((1>2) or (1<2))

print(1==2 or 1==0)

Output

True

False

Also ReadHow to Fix Invalid Syntax Error in Python

Using or with common objects

When or operator is used between objects, the or operator returns a true/false object, not the boolean value. It returns the first object evaluated to True or the last object. Below are a few false objects –

  • Empty sequences/collections – [], (), {}, set(), ‘’
  • None, False
  • Zero – 0, 0.0, 0/1

Let’s look at a few examples to get more clearance between True and False objects.

Code

# case-1
print(1 or 2)

# case-2
print(0 or 1.2)

# case-3
print([] or 0)

# case-4
print(0.0 or 5 or [1,2])

Output

1
1.2
0
5

Also ReadHow to Fix Unexpected EOF While Parsing Error In Python

Explanation:

Case 1: Here, the 1 and 2 both values are True values, so it returns the first true value, i.e., 1

Case 2: In this case, the first object value 0 is False. The second object, 1.2, is true, and the 1.2 value is returned.

Case 3: Here, both values [] (‘Empty list’), 0 are False values. So the result of or operator between [], 0 results last object, i.e., 0

Case 4: The or operation between multiple objects gives the first object, which is evaluated to be true, or the last object. In this case, 5 is the first true object among 0, 5, [1,2]. So it returns 5 as a result.

Below is the working of or operator with 2 objects.

 Left Object  Right Object  Left Object or Right Object
 obj1  obj2  obj1 if it is True else obj2

 

Also ReadHow To Copy List in Python

Using or between Boolean Expressions and Objects

The Boolean expression and Objects can be combined into a compound expression using an operator and returns the first True or last operand. The returned value can be True, False, or the Object.

Below is the working of or operator between Boolean Expressions and Objects.

Result of Boolean Expression
Result of Object
Boolean Expression or Object
True
 True
 True
True 
False 
False 
True 
True 
Object 
False 
False 
Object 

Also Read10 Best Programming Apps To Learn Python

Let’s look at a few examples to understand the topic better.

Code:

# case-1
print(10>1 or 1)

# case-2
print(1 or 1<12)

# case-3
print({} or 100>1000)

# case-4
print(1==2 or 0.0)

# case-5
print(0.0 or 1==1 or [1,2])

Output:

True
1
False
0.0
True

Also ReadPython Program To Reverse A Number

Explanation:

Case 1: Here, the first expression 10>1 results in True, and the compound expression returns True.

Case 2: In this case, the first expression is 1 (object), which is a true value, so irrespective of the second expression, it returns the first true value – 1 as a result.

Case 3: Here, the first expression {} is an empty object with a False value, and the second expression 100>1000 results in True. So it returns True as a result.

Case 4: In this case, both expressions are False values. So returns the last expression value, i.e., object 0.0

Case 5: Here, the first true expression is 1==1, resulting in a True value. So it returns a True value.

These are the various cases/contexts where or operator is used.

Note:- When Python Interpreter works with the expressions combined with or logical operator, the interpreter stops evaluating the individual expressions after finding the first true operand/expression. This is called a short-circuit/lazy evaluation.

Also ReadPython Program to Find Sum of Digits [5 Methods]

Error Handling

The or operator can also be used to avoid some errors while coding. One such case is ZeroDivisionError.

The code that throws an error when trying to divide a number with zero is given below.

# divide function
def divide(numerator,denominator):
return numerator/denominator

print(divide(24/0))

Output:

ZeroDivisionError Traceback (most recent call last)
<ipython-input-1-07ea2d9e78b7> in <module>
2 return numerator/denominator
3
----> 4 print(divide(24/0))

ZeroDivisionError: division by zero

Also ReadPython Program To Check If A Number Is a Perfect Square

This error can be fixed using the or operator. Below is the error-fixed code.

def divide(numerator,denominator):
return denominator==0 or numerator/denominator

print(divide(24/0))

Output

True

Explanation: The or operator is used between two expressions in the return statement. The first expression, denominator==0, is valid as per the input. So the second statement numerator/denominator will not get executed, and ZeroDivisionError will be avoided.

Also ReadIncrement and Decrement Operators in Python

Multiple expressions in the Lambda function

The lambda keyword defines a simple anonymous function with a single expression. But using or operator, it’s possible to define multiple expressions in a single lambda function by the below code.

# lambda function
website_Name = lambda string1, string2 : print(string1) or print(string2)

website_Name('CodeITBro','Programming Tutorials site')

Output

CodeITBro
Programming Tutorials site

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. Founder Cool SaaS Hunter. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES