Open In App

Python OR Operator

Last Updated : 19 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 1 Expression 2 Result
True True True
True False True
False True True
False False False

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

Python3




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

Python3




# 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

In the above output, we can see that the code for the if statement is executed always. This is because for every value of a, one of the boolean expressions will always be True and the else block will never get executed.

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

Python3




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



Previous Article
Next Article

Similar Reads

Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # &amp;quot;//&amp;quot; for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Difference between == and is operator in Python
When comparing objects in Python, the identity operator is frequently used in contexts where the equality operator == should be. In reality, it is almost never a good idea to use it when comparing data. What is == Operator?To compare objects based on their values, Python's equality operators (==) are employed. It calls the left object's __eq__() cl
3 min read
Operator Overloading in Python
Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because '+' operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows d
7 min read
Python | Operator.countOf
Operator.countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. Syntax : Operator.countOf(freq = a, value = b) Parameters : freq : It can be list or any other data type which store value value : It is value for which we have to count the number of occurrences Returns: Count of number of occ
2 min read
Python | List comprehension vs * operator
* operator and range() in python 3.x has many uses. One of them is to initialize the list. Code : Initializing 1D-list list in Python # Python code to initialize 1D-list # Initialize using star operator # list of size 5 will be initialized. # star is used outside the list. list1 = [0]*5 # Initialize using list comprehension # list of size 5 will be
2 min read
Check if a value exists in a DataFrame using in &amp; not in operator in Python-Pandas
In this article, Let’s discuss how to check if a given value exists in the dataframe or not.Method 1 : Use in operator to check if an element exists in dataframe. C/C++ Code # import pandas library import pandas as pd # dictionary with list object in values details = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'], 'Age
3 min read
Concatenate two strings using Operator Overloading in Python
Operator Overloading refers to using the same operator to perform different tasks by passing different types of data as arguments. To understand how '+' operator works in two different ways in python let us take the following example C/C++ Code # taking two numbers a = 2 b = 3 # using '+' operator add them c = a+b # printing the result print(&quot;
2 min read
Modulo operator (%) in Python
When we see a '%' the first thing that comes to our mind is the "percent" but in computer language, it means modulo operation(%) which returns the remainder of dividing the left-hand operand by right-hand operand or in layman's terms it finds the remainder or signed remainder after the division of one number by another. Given two positive numbers,
3 min read
What does the Double Star operator mean in Python?
Double Star or (**) is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python Language. It is also known as Power Operator. What is the Precedence of Arithmetic Operators? Arithmetic operators follow the same precedence rules as in mathematics, and they are: exponential is performed first, multiplication and division are performed ne
3 min read
Difference between != and is not operator in Python
In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If same, it returns False and if not same, it returns
3 min read
Article Tags :
Practice Tags :