CSC108H1 Chap.5 Booleans, Comparisons and Logical Operators
Booleans, Comparisons and Logical Operators
A type with two values
Week 3 introduces bool, whose only values are True and False. Comparison operators produce bools, and arithmetic happens before comparison. Three logical operators, not, and and or, work on bools; not binds most tightly and or least.
Order can prevent a crash
Python evaluates and and or from left to right and stops as soon as the answer is known, so the right side may never run.
Placing a protective test first, such as checking that a count is not zero before dividing by it, is the guard pattern the course returns to with while loops.
Where marks slip
Three habits cost marks in Week 3 questions. Writing = where == is needed, forgetting that comparisons wait until arithmetic has finished, and assuming not reaches across a whole and or or expression, when it binds more tightly than both.
Strings bring two more: capital and lowercase letters are different characters, and the in operator needs its characters together and in order, so a scattered match does not count.
What this chapter covers
- 01
The bool type and its two values
- 02
Comparison operators and arithmetic order
- 03
Truth tables for not, and and or
- 04
Precedence among logical operators
- 05
Short-circuit evaluation and guards
- 06
Comparing strings and the in operator
Worked example · free
Use short-circuit evaluation as a guard
- 2In the first version count != 0 is False, so and stops and the result is False.
- 1In the reversed version total / count runs first and raises ZeroDivisionError.
Key terms
- Boolean
- A value of type bool, either True or False.
- Comparison Operator
- An operator such as == or != that compares two values and produces a bool.
- Short-Circuit Evaluation
- Python's practice of stopping an and or or expression once its result is known.
- Logical Operator
- One of not, and and or, which combine or invert bool values.
- Guard Condition
- A test placed first in an and expression to protect a riskier test after it.
Booleans, Comparisons and Logical Operators FAQ
What is the difference between = and ==?
One equals sign assigns: it stores an address in a variable and produces no value. Two equals signs ask whether two values are equal and produce True or False. Writing if x = 5: is a syntax error in Python.
Why is 'apple' != 'apple ' True?
The second string ends with a space, and a space is a character like any other. Two strings are equal only when every character matches in order, including case, so trailing spaces and capital letters both make strings differ.
Do I need an if statement to return True or False?
Usually not. A comparison already produces the bool the function needs, so a body can return it directly, for example return n % 2 == 1. The Week 3 slide introducing Boolean functions is titled No if required for this reason.
What does the in operator check for strings?
Whether one string appears inside another as a block of consecutive characters in the same order. 'ill' in 'still' is True, but the same letters scattered through a word would not be. The empty string counts as being inside every string.
What is chained comparison in Python?
Writing two comparisons back to back so that one value sits between two less-than signs. The chain means both comparisons hold, the left value is below the middle one and the middle one is below the right, and Python never compares the two outer values with each other directly.
Exam move
Build a table of twelve Boolean expressions mixing arithmetic, comparisons, not, and and or, then add parentheses to show precedence before evaluating each. Mark every case where short-circuit evaluation skips the right side. Finally rewrite three if-based Boolean functions from the worksheets so that each returns its comparison directly. Add three string comparisons whose answers depend on capital letters or spaces.
Close by explaining, in one sentence each, why = and == can never be swapped, and why the order of the two sides of and can decide whether code crashes.
Working through Booleans, Comparisons and Logical Operators in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 Booleans, Comparisons and Logical Operators question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.