Sign up for free!

Sign up for free and get instant access to this course!.

Sign up to access this lesson

Click here to sign up and get access to this lesson!

Saving Progress...

Booleans are commonly used as a True or False statement. The simplest example would be comparing two numbers, is 1 greater than 2?

print( 1 > 2 )

Python will happily tell you that this is a false statement. In Python (and in computer programming in general), you will need to use boolean statements frequently in various scenarios that we will explore here. Let’s take a look at several examples:

Are these numbers equal?

print( 1 == 2 )
print( 2 < 1 )

These will all return False. Note that False is one of the few capitalized keywords in Python. A few examples that will return True are:

print( 2 == 2 )
print( 5 > 1 )
print( 1 < 3 )

We can use booleans to drive our logic in our code like so:

myVar = True

if myVar:
    print("It is true!")
    print("Its false!")

Try changing the value of myVar to False and run the code. Note that we will dive deeper into if..else statements in future lessons.

A very handy use of booleans is to see if a variable is set. You will use this quite frequently when you are querying APIs - sometimes you will attempt to do something like create a user account and you will need to see if the API returned an error. This would be a perfect True or False scenario. Let’s take a look at how we can get a True or False result based on if a variable has a value or not:

myVar = "I am a string and I will return True!"
anotherVar = ""

print( bool(myVar) )
print( bool(anotherVar) )

Here we are assigning to string variables. The first has text inside the string and the second is empty. If we cast these variables to a boolean, the first returns True and the second returns False. Let’s say for example we have a variable named “apiError”, and we want to display an error message if something goes wrong in our code. We can do that like this:

apiError = ""

if apiError:
    print("There was an error with the API! Error message: " + apiError)
    print("No error was found")

Try editing the apiError variable and write some error code.

Empty values generally return False:

print( bool ( "" ) )
print( bool ( 0 ) )

Even lists, tuples, sets and dictionaries:

print( bool ( [] ) ) # List
print( bool ( () ) ) # Tuple
print( bool ( {} ) ) # Set or dictionary

If you place a value in any of these objects then they will then return true:

print( bool ( [ "item" ] ) ) # List
print( bool ( ( "item" ) ) ) # Tuple
print( bool ( { "item" } ) ) # Set

Functions can return a boolean as well:

def my_function(x):

def my_function(x):
    if x > 5:
        return True
        return False
print( my_function(1) )
print( my_function(3) )
print( my_function(6) )
print( my_function(7) )

We can also get a boolean result if we check to see if a variable is of a certain type. Let’s say we want to see if a variable is of the type int:

myVar = 5
print( isinstance(myVar, int) )

As you can see Booleans are a critical concept for you to understand. Take some time and practice what you’ve learned in this lesson to make sure you fully understand before moving on.

Sign up to access the rest of this lesson

You must either log in or sign up to access this lesson.

Saving Progress...

0 0 votes
Lesson Rating
Notify of
Inline Feedbacks
View all comments