
Syntax: Or Operator
Checking if at least one thing is true with the logical "or" operator.
The Boolean "OR" Operator in Python
While the and operator is used to check if multiple expressions are all true, the or operator is used when at least one expression must be true.
Notice the example use of or below, when both expressions are false:
packages = 4
print(packages < 1 or packages > 6)
> False
If both expressions are false, then the result will be false.
Notice in the following example when one of the expressions is true:
has_no_bathroom = False
has_no_bedroom = True
print(has_no_bathroom or has_no_bedroom)
> True
Notice that has_no_bathroom is a false value, but true is still printed because has_no_bedroom is a true value. Since one of the two expressions are True, the expression will evaluate to True.
Practice: OR Operations
Based on the information above. Complete the following steps:
-
Create a variable named lowest_price and assign it a value of 12.99.
-
Create a variable named highest_price and assign it a value of 22.99.
-
Create a variable named test_price and assign it a value greater than high_price.
-
Create a variable named test_1 and assign it the result of an
orexpression checking if the test price is greater than the low_price value and less than the high_price value. -
Print the value of the test_1 variable.
-
Update the value of the test_price variable to a value below the low_price value.
-
Create a variable named test_2 and assign it the result of an
orexpression checking if the test price is greater than the low_price value and less than the high_price value. -
Print the value of the test_2 variable.
HINT*: Click here for a hint
> True
> True
SOLUTION*: Click here for an example solution
low_price = 12.99 high_price = 22.99 test_price = 24.99 test_1 = test_price > low_price and test_price < high_price print(test_1) test_price = 11.50 test_2 = test_price > low_price and test_price < high_price print(test_2)
When the above is completed and you are getting the proper output, you are ready to move onto the next exercise!