Today we’re going to continue to our series on Python. We’ll learn about Booleans and how to compare values.
Goal for this Tutorial:
- Learn about Booleans and comparison operators.
- Learn how to compare 2 different values.
- Learn how to use logic operators
We’re going to start today’s tutorial where we left off with last weeks. Go to the folder you are going to be using to follow along with the tutorial and run:
$ docker run --rm -it python:3.5
True, False, and Comparisons
Often times when writing programs we need to see if 2 items or equal or we need to take a different approach if something is larger than a specific value. To do this we need to be able to compare things. Let’s take a look at the most common tools that we’ll use when making comparisons in our code.
comparing number
>>> 1 == 1 # Equal to
True
>>> 1 == 2
False
>>> 1 != 1 # Not Equal to
False
>>> 1 != 2
True
>>> 1 < 2 # Less than
True
>>> 2 < 1
False
>>> 1 > 2 # Greater than
False
>>> 2 > 1
True
>>> 1 >= 1 # Greater than or equal to
True
>>> 0 >= 1
False
>>> 1 <= 1 # Less than or equal to
True
>>> 2 <= 1
False
You’ll notice that all of these comparisons give us either True
or False
, these are the boolean values in Python. “Boolean” values represent true and false from logic. Some languages use true and false all lowercase, but python uses the capitalized variation so you need to make sure that you always capitalize those words.
We can compare more than just numbers, for instance "a" == "a"
would work just fine, but the two types that we’re comparing need to be able to compare with one another or be coerced from one type to another. For instance this is what we see if we try to compare a string to a number using greater than or less than:
can’t compare
>>> "a" > 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
Logical Operators
Now that we have True and False we can also look at what are called the logical operators. These are keywords in python of and
, or
, and not
. Let’s look at them one at a time.
>>> True and False
False
>>> False and True
False
>>> 0 and 1
0
>>> 1 and 0
0
You’ll notice that 1 and 0
returns 0
and that’s because 0
is a falsy value. and
will return the first falsy object that is comes across because it’s impossible for them both to be true if one is false. If both are truthy then it returns the item on the right.
If we never need to know whether a value is true or false we can pass it into the bool
function like so:
>>> bool(0)
False
Let’s continue looking at the logical operators using the or
keyword. I’m going to go back to using True
and False
to make these operations easier to
read.
>>> False or True
True
>>> True or False
True
>>> 0 or False
False
>>> False or 0
0
>>> True or 1
True
>>> 1 or True
1
The or
operator return the first truthy value that it comes across going from left to right. If neither of the items is truthy then it returns the item on the right side.
Only one more logical operator to go in the form of not
.
>>> not 1
False
>>> not 0
True
>>> not True
False
>>> not False
True
In the same way the bool
function returns the truthiness of a value, not
returns the opposite.
Recap
Today we took a look what it looks like to compare values in Python using comparison and logical operators, learning along the way how the truth works in python. Comparisons will become very important as we start structuring our code to do things based on inputs that will vary. The logical operators might not seem immediately useful right now, but trust me, we’ll use them a lot.