TExcelTAD
Home › Python › Python Booleans

Python Booleans

A boolean (bool) has only two possible values: True and False (with capital letters). Booleans are the answer to yes/no questions, and every if statement and while loop is driven by them.

Comparisons produce booleans

python · try it yourself
print(10 > 3)
print(10 == 3)
print("apple" < "banana")    # alphabetical comparison
print(type(10 > 3))
True
False
True
<class 'bool'>

You usually don't print booleans; you use them to make decisions:

python · try it yourself
stock = 0
if stock > 0:
    print("In stock")
else:
    print("Sold out")
Sold out

The bool() function and truthiness

Any value can be tested for truth. bool(x) tells you how Python sees it. Most values are truthy; a small, well-defined set is falsy:

Falsy values
False the boolean itself
None "no value"
0, 0.0, 0j numeric zero
"" empty string
[], (), {}, set(), range(0) empty collections

Everything else — including "0", "False", [0] and -1 — is truthy.

python · try it yourself
tests = [0, 1, -5, "", "hello", "0", [], [0], None, {}, 0.0]
for value in tests:
    print(repr(value), "->", bool(value))
0 -> False
1 -> True
-5 -> True
'' -> False
'hello' -> True
'0' -> True
[] -> False
[0] -> True
None -> False
{} -> False
0.0 -> False

This lets you write natural-looking checks:

python · try it yourself
shopping = []
if not shopping:
    print("Your list is empty")

username = "khoa"
if username:
    print("Welcome,", username)
Your list is empty
Welcome, khoa

Tip: if items: is the idiomatic way to ask "is this collection non-empty?" — no need for if len(items) > 0:.

Combining booleans

and, or and not combine conditions:

Expression True when
a and b both are true
a or b at least one is true
not a a is false
python · try it yourself
age = 20
has_ticket = True
print(age >= 18 and has_ticket)
print(age < 12 or age > 65)
print(not has_ticket)
True
False
False

Short-circuit evaluation

and stops as soon as it finds a falsy value, or stops at the first truthy one. They also return the value that decided the result, not necessarily True/False. A common idiom uses or to supply a default:

python · try it yourself
nickname = ""
display = nickname or "Guest"
print(display)

print(0 and 10)       # 0 is falsy, so `and` returns it
print(3 and 10)       # both truthy, returns the last
print(None or "x")
Guest
0
10
x

Booleans are numbers

bool is a subclass of int: True equals 1 and False equals 0. This makes counting matches easy:

python · try it yourself
answers = [True, False, True, True]
print(sum(answers), "correct")
print(True + True)
print(isinstance(True, int))
3 correct
2
True

Functions that return booleans

Many built-ins answer yes/no questions: isinstance(), callable(), any(), all(), and string methods like isdigit().

python · try it yourself
scores = [72, 88, 95]
print(all(s >= 50 for s in scores))   # did everyone pass?
print(any(s > 90 for s in scores))    # did anyone get above 90?
True
True

Test yourself

Which value is truthy?

  1. 0
  2. ""
  3. []
  4. "0"

What does "" or "default" evaluate to?

  1. True
  2. ""
  3. "default"
  4. False

What is True + True?

  1. True
  2. 2
  3. TrueTrue
  4. TypeError