Booleans and comparisons
Evaluate a condition built from comparisons and and, or, not, and write one for a range or for the leap-year rule.
It opens on
The year is 1900 on the board. Of the three comparisons on it, will one, two or all three give True?
Why it matters
Every decision a program makes, whether to log a user in, stop a loop or charge a fee, rests on a condition, and a condition with the wrong operator or the wrong grouping gives the wrong answer for just the unusual inputs that tests tend to miss.
Good to know first
- Values, types and expressions: % gives a remainder and comparisons come after arithmetic in the order of operations
- Variables and assignment: year = 1900 binds the name year to a value that later lines read
Step by step
Comparisons give True or False
Evaluate a comparison with ==, !=, <, <=, > or >= and say the bool it gives, for numbers and for text.
For 1900, year % 4 is 0 and 0 == 0 is True, then year % 100 is 0 so that is True too, but year % 400 is 300, and 300 == 0 is False. So two of the three are True.
the three results
A common mix-up
Year = 1900 and year == 1900 do the same thing.
In fact: The single sign binds the name and gives no bool, the double sign compares and gives True or False.
and, or and not
Evaluate an expression with and, or and not, using their order, and combine the three comparisons into the leap-year condition.
The word and gives True only when both sides are True, so True and False is False; the word or gives True when at least one side is True, so True or False is True.
A common mix-up
And and or are worked out strictly left to right, so div4 or not div100 and div400 is the same condition.
In fact: And goes before or, so this groups as div4 or (not div100 and div400), which is True for 1900.
Short-circuiting
Say which parts of an and or an or Python works out, and what value the whole gives, including when it is not a bool.
For 2023, year % 4 == 0 is False, so the and is already False and the bracket is never looked at, which is one comparison.
A common mix-up
Both sides of an and or an or are always worked out, and the result is always True or False.
In fact: Python stops at the first side that decides it, and gives back that operand, as 0 or 5 giving 5 shows.
Writing a condition
Write a condition for a range and for a rule given in English, and test it on the edge cases.
The leap rule in English is divisible by 4, and either not divisible by 100 or divisible by 400, which is the leap line as written. For 1600 it gives True, and for 1900 in range and not leap, so the whole is False.
the first two test years
A common mix-up
Between is written with or, so year >= 1582 or year <= 9999 is a range test.
In fact: Or is True when either side is, which every year satisfies; a range needs and, or the chain.
Try it
For year = 1600, what do year % 4 == 0, year % 100 == 0 and year % 400 == 0 give?Answer
True, True and True
Hint: 1600 is 4 times 400
For year = 2023, div4 is False, so what does div4 and (not div100 or div400) give?Answer
False
Hint: And is False when one side is False
With x = 3, what does x > 5 or x * 2 give?Answer
6
Hint: x > 5 is False, so or gives the second side
Write a condition that is True when a score s is from 0 to 100, ends included, and give its value for s = 101.Answer
0 <= s <= 100, which is False for 101
Hint: a chain with two ends
Suppose year is 2024 instead. What is year % 100, and what does year % 100 == 0 give?Answer
24, which is not 0, so the comparison gives False.
