Boolean and None Objects

Note: Complete this lecture after finishing the "Comparison Operators" Section

Booleans are objects that evaluate to either True or False. In turn, they represent the 1 and 0 "on and off" concept. Whether you're building a web form or working with backend authentication, working with boolean objects is something that is inevitable. You use Booleans to check if things are either True or False, and use control flow to handle the situation accordingly.

A boolean object is either True, or False.


In [1]:
# Declaring both Boolean values
a = True
b = False

To stay practical, it is important to understand that you won't be assigning True and False values to variables as much as you will be receiving them. We talked in the "Comparison Operators" series about said operators, and what they return. How do we capture True and False from an expression?


In [2]:
# Capturing True from an expression
x = 2 < 3

In [3]:
# Capturing False from an expression
y = 5 > 9

The general format for if-else control flow in Python is the following:

if something_that_evals_to_True:
    foo()
elif something_else_:
    woo()
.
.
else:
    falsework()

None

Often, you will not always be sure as to what an object should hold yet. You may have a variable in mind to store an incoming object, but you need a placeholder. Or, perhaps, you want to check if the object you received is nothing. In this case, we use the "None" object.


In [6]:
# Example of assigning None, and changing it.
some_obj = None

if 2 < 3:
    some_obj = True