Python OR Operator27 May 2025 | 5 min read The 'or' operator is a logical operator in Python used to combine two or more conditions. This operation returns a Boolean Value - True if at least one condition is true, and False in case of all conditions are false. We commonly use Python 'or' operator in if statements, assert statements, while loops, and in expressions where the first truthy value has to be selected from multiple variables. Flowchart of OR OperationWe will now look at the following flowchart that visually represents the working of the 'or' operator in Python. ![]() Step-by-Step Explanation of the Flowchart:Step 1: Evaluate the expression X or Y (overall logical condition) Step 2: Check the value of X (first operand)
Step 3: Check the value of Y (second operand)
Truth Table of Python OR Operator
Using Python OR Operator with Boolean ExpressionsPython 'or' operator is generally utilized in order to combine Boolean expressions (conditions that evaluates to either True or False). When we use the 'or' operator, the entire expression returns True if at least one of the conditions is True. It returns False if all conditions are False. Let us take a simple example showing the use of Python 'or' operator with Boolean expressions. ExampleExecute NowOutput: Boolean Expression 1: False Boolean Expression 2: True Result: True Explanation: In the above example, we are given two Boolean expressions as bool_exp_1 and bool_exp_2. We then used the 'or' operator to combine these two expressions. Since one of the expressions is True, the resultant of this operation is also True. Using Python OR Operator in ifWe can use the 'or' operator in the if statement to check multiple conditions. In case, any of the conditions is True, the entire expression evaluates to True. Let us see an example. ExampleExecute NowOutput: The number is either 10 or 20. The number is not 10 or 20. The number is either 10 or 20. Explanation: In this example, we have defined a function as check_number() that takes an argument as num. Inside this function, we have used the 'or' operator in the if statement to check if the passed argument is either 10 or 20. We have then called the check_number() to check different numbers such as 10, 14, and 20. As a result, if any of the conditions specified in the if statement is True, the print() function inside the if block is execute; otherwise the else block is executed. Python OR Operator - Short CircuitingIn Python, Short-circuiting refers to the stoppage of the execution of Boolean operation if the truth value of the expression has already been determined. In case of the 'or' operator, if the first operand or Boolean expression is True, Python will not check of the second one, as the entire expression is already True. We will now take a look at the following example: ExampleExecute NowOutput: Inside the 'True' function Case 1: True Inside the 'True' function Case 2: True Inside the 'False' function Inside the 'True' function Case 3: True Inside the 'False' function Inside the 'False' function Case 4: False Explanation: In this example, we defined two functions as true() and false(). Inside these functions, we have returned the Boolean values - True and False. We have then used the 'or' operator to combine these functions during calls. As a result, we can see how Python short-circuiting when the first operand in True. ConclusionIn this tutorial, we learned about Python 'or' operator with various examples. We discussed how to use the 'or' operator with Boolean expressions and in the if statements. At last, we discussed, how short circuiting affects to flow of execution in Python 'or' operator. Python OR Operator FAQs1. What does the 'or' operator do in Python? It returns True in case any of the provided operands is true. In short, when there are multiple conditions and any single one is needed to execute the block. 2. What is short-circuiting in or operator? It allows halting of further checks in case when any True statement is found. Further computation no longer becomes necessary thereby improving efficiency. 3. Can or be used with values that are not Booleans? Sure! In fact, treated as False are values such as 0, " ", None, and []. While anything else is regarded as True. In case all 3 flag values were false the last is returned. Otherwise the first truthful value is selected. 4. What's the difference between or and |? The 'or' operator is a logical operator used with Boolean values, while the '|' operator is a bitwise operator used with integers. It compares each bit of its operands and returns a result with bits set where either operand has a 1. ExampleExecute NowOutput: True 7 (binary: 101 | 011 = 111) 5. Can we use or in if statements? Yes! It's commonly used to check if any one of multiple conditions is True. Next TopicPython Bitwise XOR Operator |
Introduction: In this tutorial, we learn about the Hierholzer's algorithm in Python. The basic step of the Hierholzer's algorithm is to combine different circles to form a Euler circle. It starts from a random node. Then, it randomly moves towards neighbours along unvisited edges. Repeat these steps...
9 min read
Linear regression is a statistical technique to describe relationships between dependent variables with a number of independent variables. This tutorial will discuss the basic concepts of linear regression as well as its application within Python. In order to give an understanding of the basics of the concept...
7 min read
You have two major options for building a GUI for your PyQt windows and dialogues: either utilize Qt Designer or manually design the GUI in plain Python code. While the second method gives you complete control over your application's code, the first path can significantly increase...
18 min read
Python is an extremely versatile programming language employed by many large companies. It is a simple and easy-to-understand syntax, which makes it perfect for those trying to master computer programming for the first time. It is a high-level programming language. Its fundamental design principle is about...
5 min read
Using binary search, we can find an element in the given sorted array or list in O(log(n)) time complexity, where n is the number of the numbers in the list. Now let us rotate this sorted array or list at any pivot. However, we don't know...
7 min read
A string is a succession of characters. A person is just an image. For instance, the English language has 26 characters. PCs don't manage characters; they manage numbers (twofold). Despite the fact that you might see characters on your screen, inside, it is put away and...
4 min read
Introduction In Python, a private method is a method that is not intended to be used outside of the class in which it is defined. These methods are denoted by a double underscore prefix (__) before their name, and they can only be accessed within the class...
3 min read
In the following tutorial, we will understand the Web2py framework in the Python programming language. Understanding the Web2py Framework Web2py is an easy-to-use framework that does not require any installation and configuration. This framework is portable and can also be executed on a USD drive. It is...
3 min read
Matplotlib is a Python library that is utilized frequently with Jupyter Notebook. The module in matplotlib that is utilized is called pyplot. In this instructional exercise, we'll become familiar with a smidgen about matplotlib and how to involve it in Jupyter Note pad. Matplotlib.pyplot gives a...
3 min read
In this tutorial, we will discuss the difference between the doctest and unittest and see some cases. Testing is an important phase of software development, and it helps to identify an error, agile code, and code reusability. A codebase is tested with several test cases to...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India