Lesson 06 of 55
8 mins readPython Booleans & Comparison Operators
In Plain English
Explore boolean logic with Bool (true, false), relational comparisons, chained comparisons like 1 < x < 10, and short-circuit evaluation operators && and ||.
Deep Dive: How It Works
Boolean Type: Bool has two values: true and false. In arithmetic contexts, true is treated as 1 and false as 0.
Chained Comparisons: In Julia, 1 < x < 10 is valid syntax and checks if x is strictly between 1 and 10 without needing 1 < x && x < 10.
Short-Circuit Operators: && and || evaluate expressions from left to right and stop as soon as the result is determined.
Core Rules to Remember

Chained Comparisons: Write mathematical range checks naturally: 0 <= x <= 100.

Booleans as Integers: count(isodd, [1, 2, 3, 4, 5]) works by summing the boolean results (1 + 0 + 1 + 0 + 1 = 3).
Live Interactive Example
Hit Run Code to see it liveChained Comparisons and Short-Circuiting
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: == tests value equality; === tests strict object and type identity.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Verify Temperature Range
Set `temp = 24.5`.
Check if `temp` is between `18.0` and `28.0` inclusive using chained comparison `is_comfortable = 18.0 <= temp <= 28.0`.
Print `"Comfortable: "` followed by `is_comfortable`.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.