Learn by reading through in order

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.

The only values bool can take
bool typeTrue(truth)False(falsity)value 1value 2

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'>

Create a bool variable in the terminal.

① Assign True to is_student and display it with print().

② Show the type of is_student with type().

Python Editor

Run code to see output

Comparisons produce booleans

You don't only get booleans by typing True / Falsecomparison operators produce them too.

Python has six comparison operators, and each one always returns True or False (i.e. a bool).

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
>greater than5 > 3True
<less than5 < 3False
>=greater than or equal5 >= 5True
<=less than or equal5 <= 3False
How a comparison flows
age = 20age >= 18Trueage < 18Falseevaluateresultevaluateresult

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.

Starting from score = 75, confirm that comparisons produce booleans.

① Print the result of score == 100.

② Put the result of score >= 60 into a variable passed, then print it.

③ Show the type of passed with type().

Python Editor

Run code to see output

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.

if and bool
if True: print(...)run blockoutput appearsif False: print(...)skipno outputTrueprintFalseskip

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")

Practice branching on different booleans.

① Define is_open = True and print "OPEN" when it's True.

② Define temperature = 35 and print "Hot day" when temperature >= 30.

Python Editor

Run code to see output

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".

How and / or / not differ
and(both)True and TrueTrueor(either)True or FalseTruenot(negate)not TrueFalseexampleresultexampleresultexampleflip

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

ABA and BA or B
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse
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")

Using is_member = True and age = 15, try out the logical operators.

① Print the result of is_member and age >= 18.

② Print the result of is_member or age >= 18.

③ Print the result of not is_member.

Python Editor

Run code to see output

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.

QUIZ

Knowledge Check

Answer each question one by one.

Q1Which of the following is a valid bool value?

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)