Part 3 · Variables and types — chapter 5 of 6 · chapter 15 of 50 · 6 min read

Python Booleans Explained (True and False)

Boolean values are one of the most important parts of Python.

A Boolean is simply a value that is either True or False. You will use Booleans when checking conditions, comparing values, and controlling what your program does next.

If you are new to Python, this is the foundation you need before moving on to Python if statements explained and comparisons.

Below we cover how Booleans are created, the operators that produce them, and how Python decides what counts as true or false.

What a Boolean is #

A Boolean is a data type with only two possible values:

  • True
  • False

Booleans are used to represent conditions such as:

  • yes or no
  • on or off
  • true or false

In Python, you must write them with capital first letters:

print(True)
print(False)

Output:

True
False

Booleans are often used in conditions and comparisons. For example:

print(5 > 3)
print(10 == 2)

Output:

True
False

Here, Python checks each comparison and returns a Boolean result.

How to create Boolean values #

There are a few common ways to create Boolean values in Python.

Assign True or False directly #

You can store a Boolean in a variable:

is_logged_in = False
has_permission = True

print(is_logged_in)
print(has_permission)

Output:

False
True

This is useful when you want to keep track of a state or setting.

Use a comparison #

Comparisons often produce Boolean values:

print(5 > 3)
print(10 == 2)
print(7 != 4)

Output:

True
False
True

Return a Boolean from a function #

Functions can return True or False to answer a question:

def is_adult(age):
    return age >= 18

print(is_adult(20))
print(is_adult(15))

Output:

True
False

This pattern is very common in real Python code.

Where Booleans are used #

Booleans are mainly used to help your program make decisions.

In if statements #

An if statement runs code only when a condition is True:

age = 20

if age >= 18:
    print("You can vote.")

Output:

You can vote.

If you want to learn this next, see Python if statements explained.

In while loops #

A while loop continues running while its condition stays True:

count = 3

while count > 0:
    print(count)
    count -= 1

Output:

3
2
1

In functions #

Functions often return Booleans so your code can check a condition:

def has_long_name(name):
    return len(name) > 5

print(has_long_name("Sam"))
print(has_long_name("Sophia"))

Output:

False
True

As flags #

A flag is a variable that stores a simple status:

is_game_over = False
print(is_game_over)

Output:

False

Flags are often used to control program flow.

Comparison operators that return Booleans #

Python has several comparison operators that return True or False.

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
>greater than7 > 2True
<less than2 < 7True
>=greater than or equal to5 >= 5True
<=less than or equal to4 <= 3False

Example:

print(8 == 8)
print(8 != 5)
print(10 < 3)
print(6 >= 6)

Output:

True
True
False
True

These operators are used all the time in conditions. They are especially important when writing if, else, and elif statements in Python.

Boolean operations #

Python also lets you combine Boolean values with logical operators.

a and ba or ba=True, b=Truea=True, b=Falsea=False, b=Truea=False, b=False
Truth table for the and and or operators across all four combinations of a and b.

and #

and returns True only if both sides are True.

age = 20
has_id = True

print(age >= 18 and has_id)

Output:

True

or #

or returns True if at least one side is True.

is_weekend = False
is_holiday = True

print(is_weekend or is_holiday)

Output:

True

not #

not reverses a Boolean value.

is_logged_in = False

print(not is_logged_in)

Output:

True

These operators are useful when you need to combine conditions in one statement.

Truthiness in Python #

In Python, some values behave like False in conditions, even though they are not the Boolean value False.

Common falsy values include:

  • 0
  • 0.0
  • '' empty string
  • [] empty list
  • {} empty dictionary
  • set() empty set
  • None

Most other values behave like True.

Example:

print(bool(0))
print(bool(''))
print(bool('hello'))
print(bool([1, 2, 3]))

Output:

False
False
True
True

This behavior is called truthy and falsy.

For beginners, it is best to first understand real Boolean values like True and False. After that, the bool() function makes more sense.

Boolean examples for beginners #

Here are a few simple examples of how Booleans are used in real code.

Check an age rule #

age = 16
can_drive = age >= 16

print(can_drive)

Output:

True

Store a status flag #

is_logged_in = False

if not is_logged_in:
    print("Please log in.")

Output:

Please log in.

Return True or False from a function #

def is_even(number):
    return number % 2 == 0

print(is_even(4))
print(is_even(7))

Output:

True
False

Use Booleans to control program flow #

temperature = 30

if temperature > 25:
    print("It is hot today.")
else:
    print("It is not hot today.")

Output:

It is hot today.

Examples like these connect Booleans to other core Python ideas, especially conditions and basic Python data types.

Common beginner mistakes #

Here are some common Boolean mistakes and how to avoid them.

Writing true or false instead of True or False #

This causes an error because Python is case-sensitive.

Wrong:

is_ready = true

Correct:

is_ready = True

If you use lowercase names by mistake, Python may raise an error such as NameError. You can learn more about Python problems in Python errors and exceptions explained.

Using = instead of == in a comparison #

= assigns a value.
== compares two values.

Wrong:

age = 18

if age = 18:
    print("Adult")

Correct:

age = 18

if age == 18:
    print("Adult")

Comparing to True when a direct condition is enough #

This works, but it is usually unnecessary:

is_logged_in = True

if is_logged_in == True:
    print("Welcome")

A cleaner version is:

is_logged_in = True

if is_logged_in:
    print("Welcome")

Confusing the string 'True' with the Boolean True #

These are not the same thing.

print('True')
print(True)
print(type('True'))
print(type(True))

Output:

True
True
<class 'str'>
<class 'bool'>

The first value is a string. The second is a Boolean.

✍️ Try it yourself

Set age to 20 and has_ticket to True. Print whether a person is allowed in, where they are allowed in only if age is at least 18 and has_ticket is True.

Show answer
age = 20
has_ticket = True

print(age >= 18 and has_ticket)
# Output:
# True

FAQ #

What is a Boolean in Python? #

A Boolean is a value that is either True or False.

Why are Booleans useful in Python? #

They help programs make decisions using conditions in if statements and loops.

Is true the same as True in Python? #

No. Python is case-sensitive. You must write True and False with capital first letters.

What does bool() do in Python? #

It converts a value into a Boolean based on whether Python treats that value as true or false.

Can a comparison return a Boolean? #

Yes. Expressions like 4 < 10 or name == 'Sam' return True or False.

See also #

Press Esc to close