<condition> in two patterns so far: the conditional (
statements) and the while loop. Until now, we’ve just said that it should be
an expression that evaluates to
False without going into much
detail. It turns out that there is a specific data type that is used for
these values (
False) and for the expressions used in conditions:
the Boolean data type.
False are the only two values that this
bool data type can have.
Note that they are not strings; they aren’t written in quotation marks.
3.4.1. Comparison Operators¶
So if every condition for an
while statement needs to evaluate to a
Boolean value, how do we write expressions that produce the correct type?
We’ve already seen some examples:
x < y n > 0 choice == 'a'
These are Boolean expressions, meaning they each evaluate to a Boolean
value. Each of those expressions is an operator with two operands. The
== operator compares two operands and produces
True if their values are
>>> 5 == 5 True >>> 5 == 6 False >>> x = 'hello' >>> y = 'hello' >>> x == y True
The complete list of comparison operators:
x is equal to y
x is not equal to y
x is greater than y
x is less than y
x is greater than or equal to 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 (
= is an assignment operator and
== is a comparison operator.
= is setting something equal to a value, while
asking a question about whether two things are equal.
Also, there is no such thing in Python as
=>. They’re always
written like they’re most commonly said aloud: e.g.,
<= is “less than or
3.4.2. Logical Operators¶
Sometimes we need a condition to depend on more than one comparion. For example, what if we wanted a while loop to continue as long as one variable was greater than 5 and another was less than 5? In Python, we can often (though certainly not always!) write a condition just as we would say it in English:
There are three logical operators:
semantics (meaning) of these operators is similar to their meaning in English.
x > 0 and x < 10
is true only if
x is greater than 0 and less than 10.
n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that
is, if the number is divisible by 2 or 3.
not operator negates a boolean expression, so
not (x > y)
is true if
x > y is false; that is, if
x is less than or equal to
One common mistake for beginning programmers is to write conditions like this:
x > 0 and < 10
or like this:
age == 18 or 19 or 20
You might want to read the first example as “x is greater than 0 and less
than 10,” and the second looks like “age is 18, 19, or 20.” But in
Python, these are both invalid syntax! Remember that
are operators that take two independent Boolean operands, one on each
side. That is, each side of an
and or an
or must be something that
False by itself.
On the left side of the
x > 0 is a valid Boolean expression
that will evaluate to
False, but on the right side,
is not a valid expression!
Check your understanding
- year > 1900 < 2000
- This is invalid syntax. The '<' operator has to be given two numbers, but its left hand side in this case is a Boolean value.
- year > 1900 and < 2000
- This is invalid syntax. The 'and' operator has to be given two Boolean values, but its right hand side in this case is an invalid expression, because the '<' needs a number on its left hand side.
- year > 1900 or < 2000
- This is invalid syntax. The 'or' operator has to be given two Boolean values, but its right hand side in this case is an invalid expression, because the '<' needs a number on its left hand side.
- year > 1900 and year < 2000
- year > 1900 or year < 2000
- This expression will evaluate to 'True' if 'year' is holding any number at all. The 'or' evaluates to 'True' if either side is 'True', and in thise case, at least one of the two sides will always be 'True'.
Q-1: Which of the following expressions is a correct, valid Python expression
for checking whether the
year variable’s value is between 1900 and
Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional:
Check your understanding
if year == 1969: if month == 'July': if day == 20: print("Moon landing!")
In the ActiveCode below, rewrite the above conditional using a single if statement with one conditional expression that is equivalent.
You can test your code and practice writing
input() statements by adding
code that asks you to input a year, month, and day before the if statement.