Discover the fundamentals of Python Booleans and learn to effectively use comparison operators. Understand how Boolean logic simplifies decision-making in programming contexts.
Key Insights
- Understand that Booleans in Python represent true or false states, making them ideal for handling binary decisions such as user actions like being online or logged in.
- Utilize the 'not' operator to effortlessly toggle Boolean values without explicitly setting them, streamlining dynamic changes within applications.
- Explore essential comparison operators (greater than, less than, equal to, not equal) in Python, which allow programmers to compare variables or variables with fixed values to return Boolean outcomes.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Boolean. A Boolean, as mentioned earlier, is a variable that can only be true or false, so it doesn't have an infinite number of options—just on/off, yes/no. Basic stuff that you're used to dealing with all the time, right? We're always thinking in Boolean terms.
I'm going, I'm not going, right? I'm taking the subway, I'm not taking the subway. There are a lot of yes/no decisions that we are constantly making. Sometimes it's based on the result of a condition, right? I'm either taking my umbrella or I'm not, but that decision is based on conditional logic.
Like, if it's raining, I'm taking the umbrella. So, in the next lesson, we'll get into the use of Booleans in this branching logic context. But for now, let's just look at Booleans—that is, variables with a value of true or false. So, we'll say isOnline
, and it's a convention to use some kind of identifier in the variable name to suggest that it's a Boolean, like is
. We'll say isOnline = True
, and then loggedIn = False
.
So, I'm online, but I'm not logged into my email or whatever. Let's print isOnline
, the type of isOnline
, and then print loggedIn
, and the type of loggedIn
. You get True
, False
, and they're both Booleans. Now, if you wanted to change the values, isOnline
becomes False
or loggedIn
becomes True
.
So, we could say loggedIn = True
, and then we could print loggedIn
. Then we could say loggedIn = False
again and print that, and you just get this true/false back and forth. It’s like a toggle switch, right? Because Booleans might change, and you wouldn't necessarily always know what to set it to explicitly. To set it to False
, you have to know that it’s True
, but what if it’s changing all the time or subject to dynamic changes in an application?
You just want a reliable way to flip or toggle a Boolean, and that is the not
operator. So, rather than set it equal to an explicit value, you just set it equal to not
itself, and that will flip it.
So, we’ll say loggedIn = not loggedIn
. It doesn’t matter what loggedIn
is—it will become the opposite. And if you do it again, it’ll flip it over one more time.
loggedIn
—we left it at False
, so it’s going to become True
again, and then back to False
as we just keep flipping it. You could do this as many times as you like. The not
operator is a very handy and powerful Python feature.
All right, moving on. Type all this. Don’t forget Boolean operators.
Boolean operators are used to compare two values and return True
or False
. You learned these in fourth grade—they just didn’t tell you they were Boolean operators when they taught you the greater than and less than signs:
>
, <
, <=
, >=
, ==
, !=
.
These all compare two values and return a Boolean. Most often, you're comparing either two variables or a variable with a hard-coded value like X > 5
, for example.
So let's say X = 5
.
X < 8
→ True
X > 8
→ False
X >= 5
→ True
X <= 5
→ True
X == 5
→ True
The double equal sign does not assign a value; it compares values. It’s called the comparison operator. It just compares the two values on either side and returns True
if they are equal, False
if they are not.
We could say X == 8
→ False
We could also say X!= 5
→ False
X!= 8
→ True
So those are your comparison operators:
<
, >
, >=
, <=
, ==
, !=
.
They’re very handy, but usually we use them in the context of what's called an if
statement, which we’ll cover in the next lesson. That means: if the condition is true, you do something; if it’s false, you do something else or nothing at all.
But we’re still covering variables. We’ll get into conditional logic in Lesson Two.