Module 3 · Variables and Data Types — topic 8 of 15
Booleans
The bool type and its two values, True and False: where Booleans come from, how comparisons and logic produce them, and how Booleans behave like the numbers 1 and 0.
Learning objectives
- Create Booleans directly and from comparisons
- Combine Booleans with and, or and not
- Use Booleans in conditions and counts
Definition
The bool type and its two values, True and False: where Booleans come from, how comparisons and logic produce them, and how Booleans behave like the numbers 1 and 0.
Introduction
A Boolean is a value that is either True or False. It answers yes-or-no questions: Is the user logged in? Is the score above 40? Has the file been saved? The type is called bool, after the mathematician George Boole.
Why it matters
Every decision in a program, every if and every while, depends on a Boolean. Storing the answer to a question in a well-named Boolean variable, such as is_eligible, makes conditions easy to read.
Detailed explanation
The two Boolean literals are True and False, always capitalised.
Comparisons produce Booleans: 5 > 3 is True, "a" == "b" is False.
Logical operators combine them: and is True only if both sides are True; or is True if at least one side is True; not flips a Boolean.
bool is a subclass of int: True behaves like 1 and False like 0 in arithmetic. This makes sum() of a list of Booleans count the True values.
bool(value) converts any value to a Boolean using truthiness rules covered in Module 5.
Name Boolean variables as questions or states: is_open, has_paid, can_vote.
and and or stop as soon as the answer is known. In has_ticket and check_id(), check_id() is never called if has_ticket is False, because the result must be False anyway. This short-circuit behaviour lets you put a cheap or safety check first, for example items and items[0] > 10 avoids looking inside an empty list.
Storing a condition in a named Boolean before using it often makes an if statement read like a sentence: if is_weekend and not is_raining: is clearer than repeating the raw comparisons inside the if.
Syntax
- True and False are literals.
- Comparisons evaluate to Booleans.
- and, or and not combine Booleans.
Code examples
Booleans from comparisons
True bool
Combining with and, or, not
False True True
More examples
Counting with Booleans
[False, True, True, True, False] Passed: 3 of 5 Everyone passed: False Anyone passed: True
Step-by-step explanation
- The list comprehension (Module 10) turns each mark into True or False.
- sum(passed) adds the Booleans; each True counts as 1.
- all() is True only if every value is True.
- any() is True if at least one value is True.
Expected output
Running “Counting with Booleans” prints exactly:
Practical applications
- Feature flags such as dark_mode_enabled are Booleans stored in settings.
- Form validation produces a Boolean for each field and checks all(fields).
- Analytics count how many users met a condition by summing Booleans.
Best practices
- Name Booleans with is_, has_ or can_.
- Use the Boolean directly in conditions.
- Use any() and all() for collections of conditions.
Common mistakes
Lower-case true
Avoid
Prefer
true is not defined; only True and False are Boolean literals.
Comparing a Boolean to True
Avoid
Prefer
The variable is already a Boolean, so the comparison adds nothing.
Key takeaways
- bool has two values: True and False.
- Comparisons produce Booleans; and, or, not combine them.
- True counts as 1 and False as 0 in arithmetic.
Try it yourself
- 1Store whether a year is a leap year in a Boolean variable for 2028 and print it.
Show hint
Divisible by 4 and (not by 100 or by 400).
- 2Given a list of five ages, print how many are 18 or older using sum().
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?