Python Boolean Values and Expressions

Use True and False, build conditions with comparisons, understand truthiness and see how and, or and not really evaluate.

Lesson 13 of 107Beginner12 min read

A boolean is a value that is either True or False. Python's bool type is what every condition ultimately produces: age >= 14, "a" in word and x == y all evaluate to True or False, and if and while statements decide what to do based on that result.

Python
age = 16
induction_done = True
can_join = age >= 14 and induction_done
print(can_join)     # True

This lesson covers the two boolean literals, comparison operators, the "truthiness" rules that let any value act as a condition, and how and and or short-circuit. The example is a climbing club deciding whether someone can join a beginner session.

Literals

Python has exactly two boolean values, written True and False with capital first letters. true, TRUE and false are not keywords; Python treats them as ordinary names and raises NameError because nothing called true exists.

Python
induction_done = True
print(type(induction_done))   # <class 'bool'>
print("True" == True)         # False: a string is not a boolean

Quoted "True" is a four-letter string. This difference matters when values arrive from files, forms or JSON: JSON's true becomes Python's True when parsed with json.loads(), but a CSV cell containing True stays a string until you convert it.

bool is a subclass of int, so True behaves like 1 and False like 0 in arithmetic. True + True is 2, and sum(results) counts the number of True values in a list. That's handy for counting, but don't rely on it for clarity elsewhere.

Comparisons

Comparison operators produce booleans:

OperatorMeaningExample (age = 16)
==equal toage == 16 → True
!=not equal toage != 16 → False
<, <=less than, at mostage <= 13 → False
>, >=greater than, at leastage >= 14 → True

= assigns and == compares; using = inside a condition is a SyntaxError. Comparisons can be chained: 14 <= age < 18 means 14 <= age and age < 18, with age evaluated only once.

Strings compare alphabetically by character code, and different types usually can't be ordered: "16" < 18 raises TypeError, although "16" == 16 is simply False. A comparison result can be stored in a variable with a descriptive name, such as is_teenager = 13 <= age <= 19, which makes later if statements read like sentences.

Truthiness

Any value can be used where Python expects a condition. Python calls bool() on it behind the scenes:

  • Falsy: False, None, 0, 0.0, "", [], {}, (), set(), range(0).
  • Truthy: everything else, including "0", "False", [0] and -1.
Python
equipment = []
if equipment:
    print("First item:", equipment[0])
else:
    print("No equipment listed")

Testing the list itself avoids an IndexError when it is empty, and reads more naturally than if len(equipment) > 0:. Be careful when zero is a meaningful value: a climber with 0 previous sessions makes if sessions: false. Write if sessions is not None: when you mean "has a value at all".

Short circuiting

and and or evaluate from left to right and stop as soon as the answer is certain:

  • a and b: if a is falsy, the result is a and b is never evaluated. Otherwise the result is b.
  • a or b: if a is truthy, the result is a. Otherwise the result is b.
Python
name = ""
if name and name[0].isupper():   # safe: name[0] is never read when name is empty
    print("Capitalised")

display = name or "Guest"
print(display)          # Guest
print(0 or [] or "x")   # x

Notice that these operators return one of their operands, not necessarily True or False. That's what makes name or "Guest" useful as a default, but it can surprise you if you print the result of and/or expecting a boolean. not is different: it always returns True or False. Precedence is not, then and, then or; add brackets when you combine them.

Independent practice and a worked check

Task: test an eligibility rule at the minimum age, below it and with an incomplete induction. The rule is: at least 14 years old and induction completed.

Python
def eligible(age, induction_done):
    return age >= 14 and induction_done

cases = [(14, True), (13, True), (15, False)]
for age, done in cases:
    print(age, done, "->", eligible(age, done))
Text
14 True -> True
13 True -> False
15 False -> False

Each case changes one input, so a failure points straight at the faulty part of the rule. For the worked check, bool("0") is True, because any non-empty string is truthy regardless of what it contains.

Reference and next practice

The Python documentation explains truth value testing and boolean operations precisely. Continue with operators and precedence and use booleans in if statements. Practise with the Python exercises and Python quiz.

Boolean Values and Expressions Summary

  • Literals: True and False are boolean objects whose spellings begin with capitals.
  • Comparisons: Comparisons express equality or order and return truth values.
  • Truthiness: Zero, empty collections and None are false in conditions.
  • Short circuiting: and and or evaluate only the operands they need and can return an operand.

Boolean Values and Expressions Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Boolean Values and Expressions in a small program

Python
age = 16
induction_done = True
can_join = age >= 14 and induction_done
print(can_join)
print(bool([]))
Output
True
False
Line-by-line explanation
  1. Line 3Both sides of and are true, so can_join is True.
  2. Line 4Printing a boolean shows True or False.
  3. Line 5An empty list is falsy, so bool([]) is False.
Example 2

Checking eligibility at the boundaries

Python
def can_join(age, induction_done, waiver=None):
    old_enough = age >= 14
    has_waiver = waiver or age >= 18
    return old_enough and induction_done and bool(has_waiver)

print(can_join(14, True, waiver="signed"))
print(can_join(13, True, waiver="signed"))
print(can_join(16, False, waiver="signed"))
print(can_join(16, True))
print(can_join(20, True))
Output
True
False
False
False
True
Line-by-line explanation
  1. Line 3waiver or age >= 18 returns the waiver text if there is one, otherwise the result of the age test.
  2. Line 4bool() turns that operand into a real boolean so the function always returns True or False.
  3. Lines 7 to 8Testing exactly at 14 and just below it checks the boundary of >=.

Common Boolean Values and Expressions Mistakes

Writing x == a or b

Don't
level = "expert"
if level == "beginner" or "improver":
    print("Join the beginner session")   # always runs
Do
level = "expert"
if level in ("beginner", "improver"):
    print("Join the beginner session")

or has lower precedence than ==, so the wrong version means (level == "beginner") or "improver". The non-empty string "improver" is truthy, so the condition is always true.

Treating the string "0" as false

Don't
spaces = "0"          # read from a form
if spaces:
    print("Book now")   # runs: "0" is a non-empty string
Do
spaces = int("0")
if spaces > 0:
    print("Book now")

Convert text to the type you mean before testing it.

Boolean Values and Expressions in Real Projects

Booleans in real code

Booleans appear as feature flags (dark_mode = True), validation results (is_valid_email(address)), filters ([m for m in members if m.active]) and permission checks. Functions that answer a yes-or-no question are conventionally named with is_, has_ or can_, so if can_join(member): reads like the rule it implements.

Short-circuiting is used every day to guard against errors and supply defaults: config.get("timeout") or 30, user and user.is_admin. When 0 or an empty string is a legitimate value, though, developers switch to explicit checks such as if value is None, because or would replace a valid zero with the default.

Boolean Values and Expressions FAQ

Why must True and False be capitalised in Python?

They are keywords spelled with a capital letter, like None. Lower-case true and false are treated as ordinary variable names, so Python raises NameError unless you have defined them. JSON and JavaScript use lower case, which is a common source of this mistake.

Is 1 == True in Python?

Yes. bool is a subclass of int, so True == 1 and False == 0 are both True. This also means True and 1 count as the same dictionary key. True is 1 is False, though, because they are different objects.

What does "truthy" mean?

A truthy value is one that counts as true in a condition even though it isn't the boolean True, such as "hello", 42 or [1]. Falsy values are those that count as false: None, zero, and empty strings and containers.

What is the difference between and/or and &/| in Python?

and, or and not are logical operators that short-circuit and work with any values. & and | are bitwise operators for integers and set operators for sets; they always evaluate both sides. NumPy and pandas use & and | for element-wise conditions, so you'll see them there with brackets around each comparison.

How do I check that all or any items in a list are true?

Use all(items) to check every item is truthy and any(items) to check at least one is. Both accept generator expressions, such as all(age >= 14 for age in ages), and stop early as soon as the answer is known.

Boolean Values and Expressions Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choiceeasy

What is bool("0")?

Multiple choicemedium

Which review concern is valid for boolean values and expressions?

Boolean Values and Expressions Quiz

2 questions to check what you learned.

  1. What is bool("0")?

    • False
    • True
    • Python ignores the distinction
  2. Which statement correctly describes truthiness?

    • Zero, empty collections and None are false in conditions.
    • Only the example output matters, not the rule
    • Every operation has the same behavior