In the next lesson, we'll learn about writing code that adapts to user input. But in order to get there, we need to learn about "boolean expressions". Before we go further, let's unpack the term "boolean expressions".
The word boolean comes from George Boole, the famous mathematician who amongst many things brought us boolean algebra, one of the foundations of the information age. We won't learn formal boolean algebra here, but we'll get a good feel for it regardless. Boolean algebra deals with the interaction of two values: true and false (also known as 1 and 0).
An expression is defined as "any legal combination of symbols that represents a value". It basically refers to a bit of code (in this case Python code).
So a boolean expression in this context is a bit of Python code that
represents a boolean value (either True
or False
).
Since these lessons are more practical in nature, let's see an example of true and false in Python:
If you look at the output of the above piece of code, you'll see:
5
True
False
There's a bunch of stuff that just happened here. So let's unpack it all.
In the example above, you've seen 2 boolean expressions: n == 5
and n == 6
.
The ==
operator checks whether its left-hand side (LHS) is equal to its
right-hand side (RHS). If the LHS and RHS are equal, then the expression
evaluates to True
; otherwise it evaluates to False
. Note that True
and
False
are Python reserved keywords. Also note that ==
is different from =
;
the former compares the LHS and the RHS and the later assigns the RHS to the LHS.
Confusing the two is a very common mistake, even for experienced programmers.
You can also write a boolean expression using !=
, which means "is not equal
to". So 5 != 6
should be read as "5 is not equal to 6", which evaluates to
True
because that is a correct statement.
Both ==
and !=
are examples of "comparison operators". An "operator" in
computer science is something that allows you to perform an operation on other
things (these "things" it performs the operation on are known as "operands").
Comparison operators allow you to compare things. Let's look at some other
useful comparison operators.
Comparison operator | Definition | Example |
---|---|---|
> |
Is greater than | 5 > 4 evaluates to True , while 4 > 5 evaluates to False |
< |
Is less than | 5 < 4 evaluates to False , while 4 < 5 evaluates to True |
>= |
Is greater than or equal to | Both 5 >= 4 and 5 >= 5 evaluate to True , while 5 >= 6 evaluates to False |
<= |
Is less than or equal to | Both 4 <= 5 and 5 <= 5 evaluate to True , while 6 <= 5 evaluates to False |
The last concept that'll help us write all sorts of boolean expressions is called "Logical operators". They basically allow us to use multiple comparison operators within the same boolean expression. Let's see an example:
The above example assigns the value 5
to n
and then checks if n
lies
between 0
and 10
. Since 5
does lie between 0
and 10
it prints True
.
Later on, when it assigns the value 11
to n
, it prints False
for the same
boolean expression, because 11
does not lie between 0
and 10
.
As you have now seen, the logical operator and
allows us to combine 2
boolean expressions into 1 by checking that both of them evaluate to True
.
There's another boolean expression called or
that allows us to check whether
either of its LHS or RHS evaluate to True
.
This is similar in concept to the previous example, but instead of checking if n
lies between 0
and 10
, this example checks if n
lies outside the range.
In the next lesson, we'll learn how to use boolean expressions to make our program adapt based on user input.
© 2024 Abhin Chhabra | Sitemap