Home »
Python
Python or Keyword
By IncludeHelp Last updated : December 07, 2024
Description and Usage
The or is a keyword (case-sensitive) in python, it is a logical operator actually, it is used to validate more than one conditions. It is similar to Logical OR (||) operator in C, C++ programming. It requires a minimum of two conditions and returns True – if one or more condition(s) is/are True.
Truth Table
Truth table for "or" keyword/operator
Condition1 Condition2 (Condition1 or Condition2)
True True True
True False True
False True True
False False False
Syntax
Syntax of or keyword/operator:
condition1 or condition2
Sample Input/Output
Input:
a = 10
b = 20
# conditions
print(a>=10 or b>=20)
print(a>10 or b>20)
Output:
True
False
Example 1
Take two numbers and test the conditions using or operator
# python code to demonstrate example of
# or keyword/operator
a = 10
b = 20
# printing return values
print(a>=10 or b>=20)
print(a>10 or b>20)
print(a==10 or b==20)
print(a==10 or b!=20)
Output
True
False
True
True
Example 2
Input a number and check whether it is divisible by 2 or 3
# Input a number and check whether
# it is divisible by 2 or 3
# input
num = int(input("Enter a number: "))
# checking num is divisible by 2 or 3
if num%2==0 or num%3==0:
print("Yes, ", num, " is divisble by 2 or 3")
else:
print("No, ", num, " is not divisble by 2 or 3")
Output
First run:
Enter a number: 66
Yes, 66 is divisble by 2 or 3
Second run:
Enter a number: 5
No, 5 is not divisble by 2 or 3
Example 3
Input a string and check whether it is "Hello" or "World"
# Input a string and check
# whether it is "Hello" or "World"
# input
string = input("Enter a string: ")
# checking the conditions
if string=="Hello" or string=="World":
print("Yes, \"", string, "\" is either \"Hello\" or \"World\"")
else:
print("No, \"", string, "\" is neither \"Hello\" nor \"World\"")
Output
First run:
Enter a string: Hello
Yes, " Hello " is either "Hello" or "World"
Second run:
Enter a string: World
Yes, " World " is either "Hello" or "World"
Third run:
Enter a string: IncludeHelp
No, " IncludeHelp " is neither "Hello" nor "World"