Learn
02.1 | COMPARISONS AND BOOLEAN LOGIC
01 | TURN A QUESTION INTO A VALUE
A program can store a score, but how does it decide whether the score reaches a target? A comparison asks a precise question and produces a Boolean result: True or False.
Suppose points is 12. The expression points >= 10 asks whether points is at least ten. It evaluates to True. Python can print that result or store it under another name for later use.
This lesson builds the conditions themselves. The next lesson uses them to choose which instructions run. Keep the question separate from the action you might take after answering it.
points = 12 reached_target = points >= 10 print(reached_target)
EXPECTED OUTPUT
True
02 | ASSIGNMENT AND EQUALITY ARE DIFFERENT
A single = assigns a value. A double == compares two values for equality. In points = 10, the name gets ten. In points == 10, Python asks whether its current value equals ten.
Comparing does not change points. Writing = where a comparison is expected is a common syntax mistake. Conversely, writing points == 10 on its own does not set points to ten.
Use != for “not equal”. Python does not use the Cambridge pseudocode symbol <> for this comparison.
points = 10 print(points == 10) print(points != 10) print(points)
EXPECTED OUTPUT
True False 10
03 | KNOW THE SIX BASIC COMPARISONS
== means equal; != means not equal; < means less than; > means greater than; <= includes the lower comparison’s equality; >= includes the upper comparison’s equality.
The difference between points > 10 and points >= 10 matters at exactly ten. “More than” excludes the threshold, while “at least” includes it. Write the boundary into your test plan rather than guessing from one ordinary example.
points = 10 print(points > 10) print(points >= 10) print(points < 10) print(points <= 10)
EXPECTED OUTPUT
False True False True
04 | COMPARE VALUES OF THE INTENDED TYPE
input() returns text. Convert suitable numeric responses before comparing amounts. The integer 10 and the string "10" are different values, so 10 == "10" is False.
Text ordering follows character ordering, not a numeric quantity rule. For example, "10" < "2" is True because the first different characters are 1 and 2. That is usually wrong for a score comparison.
Ordering an int against a str, such as 10 < "2", raises TypeError. Equality can return False instead, so successful execution alone does not prove the comparison was meaningful.
score = int(input("Score (whole number): "))
print("At least ten:", score >= 10)05 | AND REQUIRES BOTH CONDITIONS
Use lowercase and when both conditions must hold. A valid score from zero to one hundred must meet a lower limit and an upper limit.
For a score of 50, both tests are True. For -1, the lower test is False. For 101, the upper test is False. Either failure makes the combined validity False.
These examples combine Boolean comparisons. More generally, Python and and or can return an operand rather than a literal bool; start with Boolean conditions so the intended rule remains clear.
score = 50 valid = (score >= 0) and (score <= 100) print(valid)
EXPECTED OUTPUT
True
06 | OR ACCEPTS EITHER OR BOTH
Use lowercase or when either condition is enough. A score is outside the allowed range if it is below zero or above one hundred.
OR is inclusive: with Boolean operands it is True when either or both are True. It is not the exactly-one rule of XOR.
Do not use or for the inside-range test. score >= 0 or score <= 100 accepts every ordinary number: a value below zero still meets the upper test, and a value above one hundred still meets the lower test.
score = 101 outside_range = (score < 0) or (score > 100) print(outside_range)
EXPECTED OUTPUT
True
07 | NOT REVERSES TRUTH
Use not to reverse a condition’s truth value. If finished is False, not finished is True. Evaluating not finished does not assign a new value to finished.
You can also invert a whole grouped condition. not ((score < 0) or (score > 100)) means the score is not outside the range, which is the same validity rule for these ordinary numeric scores.
Keep the parentheses when they clarify what is being negated. not score < 0 is parsed as not (score < 0), but the explicit grouped form is easier to read while learning.
finished = False print(not finished) print(finished)
EXPECTED OUTPUT
True False
08 | GROUP THE RULE YOU ACTUALLY MEAN
Python’s Boolean grouping gives not higher precedence than and, and and higher precedence than or. Parentheses make the intended rule visible without requiring a reader to remember that order.
Suppose entry requires either a paid ticket or free-entry eligibility, and space must be available in both cases. Use (paid or free_entry) and space_available.
Without that grouping, paid or free_entry and space_available allows a paid ticket to pass even when there is no space. The expression runs, but describes a different rule.
paid = True free_entry = False space_available = False print((paid or free_entry) and space_available) print(paid or (free_entry and space_available))
EXPECTED OUTPUT
False True
09 | SHORT-CIRCUIT CHECKING
Python does not always evaluate both sides of and or or. With and, a false first condition is already enough to know the combined result is false. With or, a true first condition is already enough.
This is called short-circuit evaluation. In the example, count != 0 protects the division. When count is zero, the second comparison is not evaluated, avoiding division by zero.
The order matters: putting the division first would attempt it before the guard. Use this carefully and keep the rule readable.
total = 20 count = 0 large_average = (count != 0) and (total / count > 5) print(large_average)
EXPECTED OUTPUT
False
10 | TEST THE RANGE AND ITS BOUNDARIES
Use the checker to compare the lower and upper tests for an INTEGER score. Predict the combined result before checking. It models the fixed rule zero to one hundred inclusive; it does not run arbitrary Python.
Test -1, 0, 100 and 101. The two endpoints should pass; the values immediately outside should fail.
11 | AVOID THE INCOMPLETE OR TRAP
The expression choice == "yes" or "y" does not compare choice with both strings. Its second operand is the non-empty string "y", which is truthy, so the overall condition can be treated as true even for "no".
Write both comparisons: (choice == "yes") or (choice == "y"). The same principle applies to numbers. score == 1 or 2 is not a test for either one or two.
A truthy value is treated as true in a condition. Empty text, zero and False are falsy; non-empty text such as "False" is truthy. Do not confuse text contents with Boolean meaning.
12 | YOUR CONDITION DESIGN CHALLENGE
Build conditions for a non-negative item count, a temperature between 15 and 25 inclusive, and a menu response equal to either a or b. Print the individual comparison results before combining them.
For each rule, write its meaning in ordinary language and test both valid and invalid values. Include the exact boundary where appropriate. Explain why and or or fits the rule.
Extension: store the validity Boolean, then change the source value. Explain why the stored result does not automatically recalculate unless you evaluate the expression again.
PUT YOUR SKILLS TO WORK
Use the Section 2 challenges to practise these ideas independently. Work in your Python editor, predict results and keep a short test record.
Section 2 challenges Python course navigation