Section 4.1 Boolean expressions
Making Decisions.
Up to this point, our programs do the same sequence of statements every time we run them. Sometimes, though, we need to make decisions:
Is the number the user entered greater than zero?
Is the amount we want to withdraw from a bank account greater than the account balance?
Did the total of two dice come out to seven?
All of these are "yes or no" questions, and we may want to take different actions depending ono the answer to the question. For example, if the answer to the second question is "yes," we want to tell the customer they can't take out that much money; if the answer is "no," we go ahead and let them have their money.
In this chapter, we'll show you how to ask these kinds of questions in Python in order to control what your programs do.
We ask a yes/no question in Python by writing a boolean expression (named after George Boole, a 19th century mathematician). A boolean expression asks a question whose answer is either true (yes) or false (no). is an expression that is either true or false. The following examples use the operator ==
, which compares two operands and produces True
if they are equal and False
otherwise:
True
and False
are special values that belong to the class bool
; they are not strings:
The ==
operator is one of the comparison operators; others are:
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols for the same operations. A common error is to use a single equal sign (=
) instead of a double equal sign (==
). Remember that =
is an assignment operator and ==
is a comparison operator. There is no such thing as =<
or =>
.
Checkpoint 4.1.1.
Q-3: Which of the following is a Boolean expression? Select all that apply.
True
True and False are both Boolean literals.
3 == 4
The comparison between two numbers via == results in either True or False (in this case False), both Boolean values.
3 + 4
3 + 4 evaluates to 7, which is a number, not a Boolean value.
3 + 4 == 7
3 + 4 evaluates to 7. 7 == 7 then evaluates to True, which is a Boolean value.
"False"
With the double quotes surrounding it, False is interpreted as a string, not a Boolean value. If the quotes had not been included, False alone would in fact be a Boolean value.
Checkpoint 4.1.2.
Q-4: Which of the following comparison operators is used to check if x and y have the same value?
x = y
Try again. This reassigns the value of y to x.
x != y
Try again. This means that x is not equal to y.
x == y
Try again. This means that x and y have the same value, but it does not mean they are the same object.
Checkpoint 4.1.3.
Q-5: What operator makes 783 ___ 206
true? Select all that apply.
>
783 > 206 is True.
<=
Try again. 783 <= 206 is False.
True
Try again.
!=
Correct! 783 != 206 is True.
Now, translate these questions or phrases into Python.
Checkpoint 4.1.4.
Checkpoint 4.1.5.
Checkpoint 4.1.6.
There are two more comparison operators in Python:
x is y # x is the same object as y
x is not y # x is not the same object as y
While these might seem to be just another way of saying x == y
and x != y
, they are different in a very important way. Explaining that difference would take us into a fairly advanced topic, so we'll leave is
and is not
for later in the course. For now, use ==
to ask if two values are equal, and use !=
to ask if they are not equal.