Open In App

Python or Keyword

Improve
Improve
Like Article
Like
Save
Share
Report

Python OR is a logical operator keyword. The OR operator returns True if at least one of the operands becomes to be True

Note:

  • In Python or operator does not return True or False. 
  • The or operator in Python returns the first operand if it is True else the second operand.

Python OR Keyword Truth Table

Input 1 Input2 Output
True True True
True False True
False True True
False False False

In python or keyword works in two main situations of Boolean context :

If Statements 

 In the if statement python uses the or operator to connect the conditions in one expression.

Example :

Python3




# initializing variable
a = 55
b = 33
  
# defining the condition
if b > a:
    print("b is greater than a")
elif a == b:
    print("a and b are equal")
else:
    print("a is greater than b")


Output:

a is greater than b

While Loops 

 Another Statement of the Boolean context where you use the python or operator is while loop. By using or in the loop’s header, you can test several conditions and run the body until all the conditions evaluate to false.

Example :

Python3




# break the loop as soon it sees 'e'
# or 's'
i = 0
a = 'geeksforgeeks'
  
while i < len(a):
    if a[i] == 'e' or a[i] == 's':
        i += 1
        break
  
    print('Current Letter :', a[i])
    i += 1


Output:

Current Letter : g

Practical Applications 

Short-circuit evaluation is the semantics of some Boolean operators in programming in which the second argument is evaluated only if the first argument does not suffice to determine the output. When the first argument is true, the overall output becomes true.



Last Updated : 24 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads