Skip to main content

A Boolean can hold just two values: True or False.


Boolean Values

Programs often need to find out whether an expression is True or False.

Python can evaluate any expression and will always answer with one of those two values.

Comparing two values is a common case, and the result is a Boolean:

Example

Prompt
Write a Python program that prints the result of three comparisons: 10 > 9, 10 == 9, and 10 < 9. Each comparison should be printed on its own line using the print() function.
Output: complete Python program

Example

Choose what to print depending on whether the condition is True or False:

Prompt
Write a Python program that assigns a = 200 and b = 33, then uses an if/else statement to print whether b is greater than a or not.
Output: complete Python program

Evaluate Values and Variables

Pass any value to the bool() function and it answers with True or False.

Example

Check a string and a number:

Prompt
Write a Python program that uses the bool() function to evaluate the string "Hello" and the number 15, and prints both results.
Output: complete Python program

Example

Check two variables:

Prompt
Write a Python program that assigns x = "Hello" and y = 15, then prints bool(x) and bool(y).
Output: complete Python program

Most Values are True

If a value has some kind of content, it will nearly always count as True.

Strings are True unless they are empty.

Numbers are True unless they equal 0.

Lists, tuples, sets, and dictionaries are True unless they are empty.

Example

Each of these calls returns True:

Prompt
Write a Python program that prints the bool() result of "abc", 123, and ["apple", "cherry", "banana"].
Output: complete Python program

Some Values are False

Only a small group of values count as False: empty values such as (), [], {}, and "", the number 0, the value None, and naturally False itself.

Example

Each of these calls returns False:

Prompt
Write a Python program that prints the bool() result of False, None, 0, "", (), [], and {}. Each on its own line.
Output: complete Python program

Example

An object can also count as False. This happens when its class defines a __len__ function that returns 0 or False:

Prompt
Write a Python program that defines a class myclass with a len method returning 0, creates an instance, and prints bool() of that instance.
Output: complete Python program

Functions can Return a Boolean

Your own functions can return a Boolean value too:

Example

Print what the function returns:

Prompt
Write a Python program that defines a function myFunction() which returns True, then prints the result of calling that function.
Output: complete Python program

Example

Print "YES!" when the function returns True, and "NO!" when it doesn't:

Prompt
Write a Python program that defines myFunction() returning True, then uses an if/else to print "YES!" if it returns True, otherwise "NO!".
Output: complete Python program

Example

Many built-in Python functions return a Boolean as well. One of them is isinstance(), which tells you whether an object has a given data type:

Find out whether an object is an integer:

Prompt
Write a Python program that assigns x = 200 and prints isinstance(x, int) to check if the object is an integer.
Output: complete Python program