Booleans

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!")
else:
    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)
else:
    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
    else:
        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.

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

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

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete