Skip to content
Related Articles
Open in App
Not now

Related Articles

Python or Keyword

Improve Article
Save Article
  • Last Updated : 24 Oct, 2021
Improve Article
Save Article

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


  • 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 :


# 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")
    print("a is greater than b")


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 :


# 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
    print('Current Letter :', a[i])
    i += 1


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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!