Booleans
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
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..
![](/wp-content/plugins/sa-lms/includes/img/loading_spinner_badge.gif)
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Text | 8 min
Python Tuples
Text | 7 min
Python Sets
Text | 7 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 5 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete