Compare two values
A comparison asks a question and answers True or False.
- Compare numbers and read True or False
- Compare text, including capitals
- Tell = (store) from == (compare), and < from <=
Programs ask questions all the time: is this customer old enough, is the cart over the free-shipping line, is the status delivered. == asks whether two values are the same. != asks whether they are different. <, >, <=, and >= compare order. The answer is True or False, the values from the previous module.
= stores a value. == compares. They are different.
Run each example to see what it prints. Running examples doesn't count toward progress.
print(3 < 10) print(3 == 10) print(3 != 10) print(10 >= 10)
status = "delivered" print(status == "delivered") print(status == "Delivered")
The boundary matters. "Free shipping from 50" means a total of exactly 50 qualifies, so the test is total >= 50, not total > 50. Decide which side of the line the boundary value is on, then pick < or <=. A comparison is a value like any other: you can store it, adult = age >= 18, and print it later.
- ==, !=, <, >, <=, and >= answer True or False.
- = stores a value. == compares two values. Decide whether the boundary value counts, then choose < or <=.
- Text comparison cares about capitals. Next: combining two answers with and, or, and not.