Boolean variables
In order to control the flow of a program, we will need a new type of value and variable called a boolean. In python the name of the type is bool
. Boolean variables can only have two possible values: True
or False
.
Boolean variables are used much like other variables in python. For example, when creating an animated (moving) drawing in dudraw
, we may want a variable called is_moving
to keep track of whether one of our objects is continuing to move. Here is some code that would create and access that variable:
Code | Output |
---|---|
|
|
Relational operators
Relational operators can be used to compare two values to produce a boolean (True
or False
) result.
Relational operator | What it does | Example (x=4, y=8) |
---|---|---|
== | True if the two sides are equal, False otherwise | x==y →False |
!= | True if the two sides are not equal, False otherwise | x!=y →True |
> | True if the left side is greater than the right side, False otherwise | x>y →False |
< | True if the left side is less than the right side, False otherwise | x<y →True |
>= | True if the left side is greater than or equal to the right side, False otherwise | x>=y →False |
<= | True if the left side is less than or equal to the right side, False otherwise | x<=y →True |
Boolean Expressions
A Boolean expression is an expression that evaluates to either True
or False
.
Examples: Suppose the following code has executed:
x = 5
y = 3
z = 10
g = 2
Evaluate the following boolean expressions:
Boolean expression... | ...evaluates to: |
---|---|
x * g == z | True |
2 + y > z | False |
x - z >= g | False |
x * g <= z | True |
Logical Operators
Logical operators are used to construct more complicated boolean expressions from simpler boolean expressions.
The three logical operators we will use in python are not
, and
, and or
.
Logical operator | What it does |
Example ( |
|
Negate the value - meaning a |
|
|
"I completed homework and read a book", evaluates to |
|
|
"I completed homework or read a book" evaluates as |
|
Note that in the English language, sometimes people use the word "or" to mean one or the other but not both. That is an alternate meaning that in Computer Science and mathematics is called exclusive or. But in programming and in mathematics, or
always means one or the other or both.
Precedence of Logical Operators
The order of precedence for logical operators is
not
, thenand
, thenor
.
Just like in arithmetic, the order of precedence can be overridden with parentheses.
Sometimes people forget whether and
or or
has higher precedence. So you are urged to use parentheses in logical expressions even if they are not required, to avoid ambiguity and to improve the clarity of your code to others who read it.
Examples: Suppose the following code has executed:
x = 5
y = 3
z = 10
g = 2
Evaluate the following boolean expressions:
Boolean expression... | ...evaluates to: |
---|---|
x * g == z or not (x * x < y) | True |
(2 * y < z and z <= g) or x != 5 | False |
not (x == 7) | True |
Note: In the above examples, none of the parentheses are required, but they improve readability of the code.
Note: In the last example not (x == 7)
is equivalent to x != 7
.