Skip to content

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.

Beginner 10 min read·Lesson 36 of 306 in Python Programming

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

Python
is_active = True
is_adult = age >= 18
can_enter = is_adult and has_ticket
  • True and False are literals.
  • Comparisons evaluate to Booleans.
  • and, or and not combine Booleans.

Code examples

Booleans from comparisons

Python
temperature = 38
is_hot = temperature > 35
print(is_hot, type(is_hot).__name__)
Output
True bool

Combining with and, or, not

Python
has_ticket = True
has_id = False
print(has_ticket and has_id)
print(has_ticket or has_id)
print(not has_id)
Output
False
True
True

More examples

Counting with Booleans

Python
marks = [35, 72, 48, 90, 22]
passed = [m >= 40 for m in marks]
print(passed)
print("Passed:", sum(passed), "of", len(marks))
print("Everyone passed:", all(passed))
print("Anyone passed:", any(passed))
Output
[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:

TextOutput
[False, True, True, True, False]
Passed: 3 of 5
Everyone passed: False
Anyone passed: True

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

is_ready = true

Prefer

is_ready = True

true is not defined; only True and False are Boolean literals.

Comparing a Boolean to True

Avoid

if is_ready == True:

Prefer

if is_ready:

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

  1. 1
    Store 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).

  2. 2
    Given 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?
Practise this topic