How to Use OR Operator in Python If Statement?

Logical operators are used in Python to combine multiple expressions into single-line expressions. In Python, logical operators such as OR, AND, and NOT return the Boolean value “True or False”. These operators help us to combine multiple decisions-driven statements. Logical operators are used along with conditions like “if”, “if-else”, and “elif” to reduce multiple lines of code into a single line.

This Python guide will provide a detailed overview of the “OR” operator in Python if statement. The following aspects are discussed in this Python guide:

So let’s get started!

What is Logical OR Operator in Python?

The logical “OR” operator in Python returns the boolean value “True” when any operand’s value is “True”.

We can specify multiple conditions using the “OR” operator, which returns either “True or False”. When either of the operand values is “True”, the “OR” operator returns the final value as “True”. The truth table of the “OR” operator is shown below:

1st Operand Value2nd Operand ValueOutput
False (0)False (0)False (0)
False (0)True (1)True (1)
True (1)False (0)True (1)
True (1)True (1)True (1)

The outcomes generated from the above table is:

  • If any of the operand value returns “True” boolean value then the final output of the “OR” operator is “True”.
  • If both operand values return a “False” value, only then the final output of the “OR” operator will be “False”.

The “OR” operator, along with the “if statement” can enhance the functionality of code. The “OR” operator along with the “if” statement is elaborated in detail in the examples shown below:

Example 1: Using OR Operator With Python if Statement

In the following example, the “OR” operator is used along with the “if-statement” to compare two conditions. Multiple conditions are applied to the given data with the help of the “OR” operator. It will return “True” if either of the conditions is satisfied:

Code:

Number = 45

if Number < 21 or Number > 100:
    print('Linux Guide')
else:
    print('Python Guide')

In the above code:

  • The integer value “45” is initialized.
  • The “if” statement is utilized with the combination of the “OR” operator to compare the compound condition. The “OR” operator returns a true Boolean value if any operand conditions become true.
  • Similarly, if both conditions are false, then the “else” statement/block will be executed in the program.

Output:

The output shows that the given number doesn’t satisfy the criteria of the specified condition, so the else block gets executed.

Example 2: Using Multiple OR Operator With Python if Statement

In the following example, multiple “OR” operators are used with Python if statement to return the Boolean value “True” if either of the conditions is satisfied.

Code:

string_value = 'Python'
intger_value = 12
value1 = 'Linux'

if (string_value == 'Python') or (intger_value == 20) or (value1 == 'Java'):
    print("One of the value is same")
else:
    print("All values are different")

In the above code:

  • Two string values and an integer value are initialized in the program.
  • Multiple “OR” operators are used with the combination of “if statement” to compare three different conditions.
  • Whenever one of the conditions in the “if statement” is met, the ” OR ” operator returns a ” True ” value.
  • If all the conditions of the “OR” operator are not satisfied, then the code associated with the “else”  block will execute.

Output:

The output shows that one of the initialized values is the same.

Example 3: Using OR Operator With Python elif Statement

In the following example, the “OR” operator is used with multiple “elif” statements. It returns the boolean value “True” if either of the conditions is satisfied:

Code:

Number = 72

if Number<0:
    print('Number is less than zero.')
elif Number<0 or Number>80:
    print('Number is in range from (0,80)')
elif Number>70 or Number<100:
    print('Number is in range from (70,100)')

In the above code:

  • An integer number is initialized in the program.
  • The “if” statement and multiple “elif” statements are used in the above code.
  • The “OR” operator is utilized along with the “elif” statement. if either one or both of the conditions meets the given criteria, then the statement written inside the “elif” block will execute and show the result. 

Output:

The above output shows the value that the number lies in the range of (70-100).

That’s all from this Python Tutorial!

Conclusion

The “OR” operator in Python “if statement” is used to combine multiple conditions and filter out the information based on the specified conditions. The “OR” operator shows a “True” boolean value when any of its conditions become “True”. If all the operands/conditions retrieve a ” False” value, then the “OR” operator retrieves ” False“. The “OR” operator is also used with multiple “elif” statements, and within the “elif” statement, you can specify more than one condition using the “OR” operator. This article presented a detailed overview of using OR operator in Python if statement with multiple examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×