Q1Which of the following is a valid bool value?
Booleans and Building Conditions
Get to know Python booleans by running every example live. Learn how True / False work and how to combine conditions with confidence.
What is a boolean (bool)?
A boolean (bool) is a type that only ever holds two values: True and False. It's how Python represents yes/no or holds/doesn't hold style states — basically anything with two choices.
Just as int handles numbers and str handles strings, bool is the specialist type for truth values.
Unlike other types, bool is limited to exactly two values — True and False. That's what makes it play so well with assignment, comparison, and conditional branches.
True and False must start with a capital letter. Write true or TRUE and you'll get a NameError.
They aren't strings either, so don't wrap them in quotes like "True" (that turns them into a str).
# Assigning bool values
is_animal = True
is_human = False
print(is_animal) # True
print(is_human) # False
print(type(is_animal)) # <class 'bool'>
# Writing "True" makes it a str (a different thing)
looks_bool = "True"
print(type(looks_bool)) # <class 'str'>
Comparisons produce booleans
You don't only get booleans by typing True / False — comparison operators produce them too.
Python has six comparison operators, and each one always returns True or False (i.e. a bool).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | equal to | 5 == 5 | True |
| != | not equal to | 5 != 3 | True |
| > | greater than | 5 > 3 | True |
| < | less than | 5 < 3 | False |
| >= | greater than or equal | 5 >= 5 | True |
| <= | less than or equal | 5 <= 3 | False |
When an expression is evaluated, it returns either True or False. You can also store that result in a variable and reuse it.
age = 20
print(age == 20) # True (equal)
print(age != 18) # True (not equal)
print(age >= 18) # True (>=)
print(age < 18) # False (<)
# You can store the result in a variable
is_adult = age >= 18
print(is_adult) # True
print(type(is_adult)) # <class 'bool'>
# Strings can be compared too (==, != are the useful ones)
print("apple" == "apple") # True
print("apple" == "Apple") # False (case-sensitive)
= and == are different
= (a single equals) is assignment, and == (a double equals) is comparison. age = 20 means "put 20 into age", while age == 20 asks "is age equal to 20?". Mixing these up is an easy way to introduce a bug you won't spot right away, so keep them straight.
Using booleans in if statements
The place you'll meet booleans most often is the if statement (a conditional branch). The block inside runs only when the expression after if is True.
We'll cover if in more depth later — for now, just get a feel for how a bool switches behavior.
End the if line with a colon :, and write the body with a 4-space indent. That indent is what tells Python which lines belong to the if.
is_animal = True
# Runs because is_animal is True
if is_animal:
print("It's an animal")
# Skipped because is_human is False — nothing prints
is_human = False
if is_human:
print("It's a human")
# You can put a comparison directly in the if
age = 20
if age >= 18:
print("Adult")
Logical operators: and / or / not
When you want to combine multiple conditions, reach for the logical operators and / or / not.
They take booleans as input and return booleans — they correspond to plain-English "and", "or", and "not".
and is True only when both sides are True.
or is True when either side is True.
not flips True and False.
Truth table
The result of and and or is fully determined by the left and right booleans. It helps to think of them as intersection (and) and union (or).
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
is_man = True
is_adult = True
# and: True only when both are True
if is_man and is_adult:
print("Adult man")
# or: True when at least one is True
if is_man or is_adult:
print("Man or adult")
# not: flip the value
is_child = not is_adult
print(is_child) # False
# Combine with comparisons
age = 25
if age >= 18 and age < 65:
print("Working-age adult")
Don't write &&, ||, !
Some other languages use &&, ||, and !, but Python always uses and, or, and not (the English words). Writing && gives you a SyntaxError.
In this article you learned about the boolean (bool) type and how to use it. You saw that bool only holds two values, that comparisons produce booleans, that if statements let you branch on them, and that and / or / not let you combine conditions — the groundwork for every conditional you'll write.
Next up, we'll take a deeper look at numbers with the int and float types.
Knowledge Check
Answer each question one by one.
Q2What does this code print?x = 10
print(x == 10 and x < 5)
Q3What does this code print?is_open = False
print(not is_open)