Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Or Operator

check

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:

  1. Create a variable named lowest_price and assign it a value of 12.99.

  2. Create a variable named highest_price and assign it a value of 22.99.

  3. Create a variable named test_price and assign it a value greater than high_price.

  4. Create a variable named test_1 and assign it the result of an or expression checking if the test price is greater than the low_price value and less than the high_price value.

  5. Print the value of the test_1 variable.

  6. Update the value of the test_price variable to a value below the low_price value.

  7. Create a variable named test_2 and assign it the result of an or expression checking if the test price is greater than the low_price value and less than the high_price value.

  8. Print the value of the test_2 variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > True
    > True
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    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!