Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Logical Operators: or

The second logical operator is or. “If it's Saturday or Sunday, it's the weekend”:

day = "Sunday"
if day == "Saturday" or day == "Sunday":
    print("It's the weekend")

This prints:

It's the weekend

A condition joined with or is true if at least one of its parts is true. It's false only if all the parts are false:

  • True or True is True
  • True or False is True
  • False or True is True
  • False or False is False

A typical mistake is to shorten the condition the way we speak: day == "Saturday" or "Sunday". Python takes the right part on its own: it's just the text "Sunday", not a comparison. Text that isn't empty counts as true, so the whole condition is always true, even on Monday. Each part needs its own comparison: day == "Saturday" or day == "Sunday".

You can use and and or in one condition. Just as multiplication goes before addition, and goes before or. Use parentheses to group what belongs together, as in math:

day = "Saturday"
temperature = 15
if (day == "Saturday" or day == "Sunday") and temperature > 20:
    print("Let's go on a trip")
else:
    print("Let's stay at home")

This prints:

Let's stay at home

The part in parentheses is evaluated first: it's Saturday, so it's true. But temperature > 20 is false, and True and False is false. Without the parentheses, Python would first join day == "Sunday" and temperature > 20, and the condition would be true on every Saturday, whatever the weather.

Instructions

  1. 1

    A movie theater gives a discount to students and to people aged 65 or more. Write an if statement that prints Discount if is_student is true or age is at least 65. Use or.

  2. 2

    New rule: discounts only on Wednesdays. Add and day == "Wednesday" to the end of the condition, put parentheses around the or part, and add an else branch that prints Full price. Today is Monday, so the program prints Full price.

Back

12/15

  1. 1. The if Statement
  2. 2. Blocks of Code
  3. 3. The else Branch
  4. 4. Quiz: if and else
  5. 5. Comparison Operators
  6. 6. Quiz: Arranging a Condition
  7. 7. Boolean Values
  8. 8. More Options: elif
  9. 9. Quiz: elif
  10. 10. Logical Operators: and
  11. 11. Quiz: and
  12. 12. Logical Operators: or
  13. 13. Logical Operators: not
  14. 14. Nested Conditions
  15. 15. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.