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: not

The third logical operator, not, is called negation. It turns a boolean into its opposite: not True is False and not False is True:

is_raining = False
if not is_raining:
    print("No umbrella needed")

This prints:

No umbrella needed

is_raining is False, so not is_raining is True and the block runs. Read the condition as “if it is not raining”. You could also write is_raining == False. It means the same, but not is shorter and reads better.

Negation comes in handy when you combine conditions. “The shop is open and it isn't a holiday” is is_open and not is_holiday. not applies only to the part right after it, up to the next and or or: here, just to is_holiday.

Instructions

  1. 1

    Write an if statement that prints I will go outside if the internet doesn't work. Use not.

  2. 2

    Going outside is only fun when it isn't raining. Extend the condition with and so that it's true only if the internet doesn't work and it isn't raining (not is_raining). Then add an else branch that prints I will read a book.

Back

13/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.