Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python or Keyword

  • Last Updated : 24 Oct, 2021

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

Note:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • 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 1Input2Output
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!