Section 4.1 Making Decisions
Boolean Values and Boolean Expressions.
Up to this point, our programs do the same sequence of statements every time we run them. The user input may be different, but the program does the same steps. Sometimes, though, we need to make decisions:
Is the number the user entered for the number of seconds greater than zero?
Is the amount we want to withdraw from a bank account less than or equal to 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 on the answer to the question. For example, if the answer to the second question is “yes”, we let the customer have their money; if the answer is “no”, we tell the customer they can’t take out that much 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).
The following example uses the operator <
to ask the question “is the operand on the left less than the operand on the right?” and produces a True
or False
result:
True
and False
are the only two boolean values that belong to the class bool
. Capitalization is important, since Python is case sensitive. True
and False
are not strings, as we can see if we print out their data types:
The <
operator is one of the comparison operators. Here is a table of comparison operators:
x < y # x is less than y
x <= y # x is less than or equal to y
x > y # x is greater than y
x >= y # x is greater than or equal to y
x == y # x is equal to y
x != y # x is not equal to y
Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols for the same operations.
Pay special attention to the ==
operator. This operator, like all the comparison operators, gives the answer to a question: “Does the operand on the left have the same value as the operand on the right?”
A common error is to use a single equal sign (=
) instead of a double equal sign (==
). Remember that =
is the assignment operator and ==
is a comparison operator.
The assignment operator =
performs an action. age = 25
assigns the value 25 to the variable age
.
The comparison operator ==
answers a question. age == 25
answers the question: “True or False—is the value of age
equal to 25?”
Here’s another difference between ==
and =
: an equality test is symmetric, but assignment is not. For example, the result of a == 7
is always the same as the result of 7 == a
. But in Python, the statement a = 7
is legal and 7 = a
is not. (Can you explain why?)
Checkpoint 4.1.1.
Checkpoint 4.1.2.
Q-5: Which operator makes 783 ___ 206
true? Select all that apply.
>
783 > 206
is True
.
<=
Try again. 783 <= 206
is False
.
True
Try again with one of the comparison operators.
!=
Correct! 783 != 206
is True
.
Now, translate these questions or phrases into Python.
Checkpoint 4.1.3.
Checkpoint 4.1.4.
Checkpoint 4.1.5.