- Checking if a number is equal is useful, but we need to be able to do more!
- Relational operators allow us to perform a range of checks on variables
- Remember when we are comparing, use two equals rather than one!
Symbol | Example | Operation |
---|---|---|
== | if x == 10: | equal to |
< | if x < 10: | less than |
<= | if x <= 10: | less than or equal to |
> | if x > 10: | greater than |
>= | if x >= 10: | greater than or equal to |
!= | if x != 10: | not equal to |
#Read in number as integer
x = int(input("Enter a number: "))
#Check x
if x < 0:
print("x is negative")
elif x > 0:
print("x is positive")
elif x == 0:
print("x is 0")
else:
print("x is not a number")
Tasks
- Type in the code above and make sure it works
- Modify the program to print a message if the number is over 50
- Modify the program to print a message when the number is over 100
- Save and label your code
Extension Task
Investigate what happens if you type in letters instead of numbers. How might you prevent these issues?