University of Toronto · FACULTY OF COMPUTER SCIENCE

CSC108H1 Chap.5 Booleans, Comparisons and Logical Operators

- one subject, every graph, every model, every mark
6 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 5 of 10 · CSC108H1

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.

In this chapter

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

Q [3 marks]. AskSia-authored practice. With count = 0 and total = 120, compare count != 0 and total / count > 50 with the same two tests in the opposite order. The marks shown are an AskSia study allocation, not an official University marking scheme.
Figure 5.1.Figure 5.1. With count equal to 0 and total equal to 120, the left test count != 0 is False, so the red path ends the and expression at once. The division on the right, which would raise ZeroDivisionError, is never reached.count != 0 (count is 0)False: stop herewhole result is FalseTrue: evaluate righttotal / count > 50path taken: left only
Figure 5.1. With count equal to 0 and total equal to 120, the left test count != 0 is False, so the red path ends the and expression at once. The division on the right, which would raise ZeroDivisionError, is never reached.
  • 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.
The guarded version evaluates only its left side, finds False and returns False without dividing. The reversed version divides 120 by 0 before any check runs and crashes. The two expressions are equivalent in logic but not in execution, because and evaluates from left to right.
Sia tip — Read an and expression aloud from the left and stop at the first False; anything after that point never runs, so put the risky test there.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 21 of your University of Toronto subjects - and 1,000+ Bibles across every Australian university.
Sia - your CSC108H1 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works