# Python or Operator

### What is or operator in Python?

• The ‘or’ in Python is a logical operator that evaluates as True if any of the operands is True.
• This is unlike the ‘and’ operator where all operands have to be True in order to be evaluated as True.
• For example, if we check x == 10 and y == 20 in the if condition. If either of the expressions is True, the code inside the if statement will execute.
• This is demonstrated in the example below.

### An example of OR operator

In the following example, the value of variable int_x = 10 and int_y = 20. The values of these variables are checked in the if statement with OR operator.

Python code:

```#A demo of using OR operator

int_x = 10

int_y = 20

if (int_x == 10) or (int_y < 20):

print("One or Both expressions are True")

else:

print("Both are False!")```

Output:

Even if the value of int_y is not less than 20 and it is False, the if statement is evaluated as True so the statement inside the if statement is executed.

### User input example with OR operator

For this example, the values for x and y variables are taken by the user input.

If the value of x >= 10 or y <= 20, the if statement should be evaluated as True. I have entered three different sets of values and see the output:

```#A demo of using OR operator

x = int(input("Enter value of x = "))

y = int(input("Enter value of y = "))

if (x >= 10) or (y <= 20):

print("x >= 10 or y <=20 so if statement is True")

else:

print("Value of x < 10 and y > 20 so if statement is False!")```

### So, how OR operator work?

To understand the concept of the OR operator, have a look at the following example.

• Suppose we have (x or y).
• The OR operator will only evaluate the y if x is False.
• If x is True, it will not check y.
• In the case of AND operator, if x is True, it will evaluate y. If x is False then y will not be evaluated.

See this demonstration in the code below:

```#How OR works - Demo

x = False

y = 30

a = True

b = 30

# x is False so y should evaluvate

print(x or y)

# a is True - No need to evaluvate b

print(a or b)```

Output:

30
True

Hope it clears up how the Python OR operator works.

### Using multiple OR operator example

You may evaluate more than two expressions by using the OR operator. To demonstrate this, I have declared and assigned values to three variables.

The OR operator is used twice in the if statement to evaluate three expressions. If either of the three expressions is True, the print function inside the if statement should display the message.

If all are False, the statement in the else block should execute:

```#Multiple OR operator example

var_a = 'Cool'

var_b = 33

var_c = 'Python'

if (var_a== 'cool') or (var_b == 20) or (var_c == 'Python'):

print("If statement is True")

else:

print("All are False")

```

Result:

If statement is True

You can see, only one expression out of three is True, so the if statement is True.

### A Demo of ‘or’ and ‘and’ operator

Can we use the ‘or’ and ‘and’ operators in a single if statement? Yes, we may use it depending on the scenario.

See an example of using the ‘and’ and ‘or’ operators in a single if statement.

```#Example of ‘and’ and ‘or’ operators

a = 5

b = 10

c = 15

d = 20

if (a == 5 or b == 15) and (c == 16 or d == 20):

print("If statement is True")

else:

print("False")```

Output:

If statement is True

In that case, both OR operators must be true to make if statement True. If one is True and the other is False, the ‘AND’ operator evaluates as False.

So if variable values are a=5, b= 10, c = 15, d= 20 then:

• (a == 5 or b == 15) and (c == 16 or d == 20) = True
• (a == 5 or b == 15) and (c == 16 or d == 21) = False
• (a == 10 or b == 15) and (c == 16 or d == 20) = True
• (a == 8 or b == 13) and (c == 15 or d == 20) = False
##### Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!