Comparisons & Booleans, explained simply
A comparison is a question with only two possible answers: True or False. These two values are called Booleans, after the mathematician George Boole.
Python has six comparison signs: == (equal), != (not equal), > and < (bigger, smaller), and >= and <= (bigger or equal, smaller or equal).
You can join questions together. and is True only when both sides are True. or is True when at least one side is True. not flips True to False and False to True.
Example: A gate that needs two keys
has_gold_key = True
has_silver_key = False
print(has_gold_key and has_silver_key)
print(has_gold_key or has_silver_key)Output
False
TrueTry this
- Change has_silver_key to True and predict both lines before you run it.
- Print 7 > 3 and "cat" == "Cat". Was the second one what you expected?
- Write a check that is True only if a number is between 1 and 10.
