The Python or keyword is one of the logical operator. This keyword results in True, If either of the condition is True. It is a case-sensitive. It is must be used between two conditions.
The or keyword can be used in conditional statements, loops, functions to check whether the condition is True or False. It cannot be used directly between two numeric values because it will consider both the values as a True values.
Here, is the representation of Python or keyword −
condition1 or condition2
condition1 and condition2 are any numeric conditions.
Let us consider A and B be the conditions, If any one of the condition, either A or B is True, it will result in True. When A and B are False, it will result in False.
Here, is the truth-table of or keyword −
A B A or B True True True True False True False True True False False False ExampleHere, is an basic example of Python or keyword −
condition1=True condition2=True result_1=condition1 or condition2 print("The Result Of ",condition1,"or",condition2,":",result_1) condition3=1 condition4=0 result_2=condition3 or condition4 print("The Result Of ",condition3,"or",condition4,":",result_2)Output
Following is the output of the above code −
The Result Of True or False : True The Result Of False or False : Falseor keyword in if-else statements
The or keyword can be used in if-else block to check whether the given condition result in True or not. If one of the given conditions result in True than the if block is executed, otherwise else block is executed −
ExampleLets try to understand the usage of or keyword in if-else with following example −
var1=89 var2=39 var3=9 if var1 > var2 or var1 < var3: print("One Of the Condition is True") else: print("Both The Conditions Are False")Output
Following is the output of the above code −
One Of the Condition is Trueor keyword in loops
The or keyword is also used in the loops to check the given conditions. If the given condition result in True then the loop will be executed.
ExampleIn the following example, we are using the or keyword in a while loop −
tuple1=[] var1=1 while var1<30 or var1<0: if var1%2!=0: tuple1.append(var1) var1=var1+1 print("We have appended the tuple1 with the odd numbers below 30 :", tuple1)Output
Following is the output of the above code −
We have appended the tuple1 with the odd numbers below 30 : [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]Python and Keyword in Functions
The or keyword is also used in function. If the given condition is satisfied then it returns True else returns False −
ExampleHere, is an example of usage of or keyword in function −
def num(x,y): if x>y or x%y==0: return True else: return False var1=204 var2=2 result_1=num(var1,var2) print(var1,"is divisible by",var2,"True/False :",result_1) var3=9 var4=600 result_2=num(var3,var4) print(var3,"is divisible by",var4,"True/False :",result_2)Output
Following is the output of the above code −
204 is divisible by 2 True/False : True 9 is divisible by 600 True/False : Falseor Keyword in Numeric Value
In Python, Between two numeric values or keyword cannot be used. It will consider both the numeric values as True and result in first numeric value. If the first numeric value is 0 than results in second numeric value. If both the numeric values are zero than it will result in zero.
ExampleHere, is an example for usage of or keyword between numeric values −
var1=34 var2=67 result_1= var1 or var2 print("The Result of Two numeric values",var1,"or",var2,":",result_1) var3=0 var4=18 result_2= var3 or var4 print("The Result of Two numeric values",var3,"or",var4,":",result_2) var5=0 var6=0 result_3= var5 or var6 print("The Result of Two numeric values",var5,"or",var6,":",result_3)Output
Following is the output of the above code −
The Result of Two numeric values 34 or 67 : 34 The Result of Two numeric values 0 or 18 : 18 The Result of Two numeric values 0 or 0 : 0
python_keywords.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4