20. Logical Operators

  • Logical operators allow you to combine checks on variables
  • The two operators are OR and AND
    • OR will be true if either condition is met
    • AND will be true when both conditions are met
  • In Python, they are always written in lowercase
x = 4

if (x < 1) or (x > 10):
    print("x is not between 1 and 100")

if (x > 1) and (x <= 5):
    print("x is between 1 and 5")

Tasks

  1. Type in the code and test that it works
  2. Modify the program to check if a number entered is between 1-10, 11-20, 21-30, 31-40 or 41-50
  3. Save and label your code

Extension Task

Modify the program to accept user input, and handle unexpected input appropriately (i.e. the user does not enter a valid number)