MCS 260 Fall 2021
Emily Dumas
We've seen a few types so far:
260
260.0
"260"
There is a type in Python called a boolean or bool that has only two possible values:
True
False
These values are Python keywords.
(Boolean variables are also used in mathematics and CS, and most programming languages support them in some way.)
Boolean values in Python programs are usually produced by operators such as:
$\texttt{>}$ | is greater than |
$\texttt{<}$ | is less than |
$\texttt{==}$ | is equal to note two equal signs! |
$\texttt{!=}$ | is not equal to |
$\texttt{>=}$ | is greater than or equal to |
$\texttt{<=}$ | is less than or equal to |
Examples of boolean expressions:
2 > 5 # False
11 == 9 + 2 # True
score >= 85.0 # True or False, depending on score
You can assign variables to boolean expressions:
x = 4>5 # Make bool variable x storing False
account_lockout = failed_logins >= 3
# now account_lockout is True or False depending on the value
# of failed_logins
So far, the programs we've written are executed line by line, starting from the top and moving down.
Most programs need to also make decisions, e.g. to say that something should happen only if certain criteria are met.
This is what conditionals do. They say that a certain section of code should only run if a boolean expression evaluates to True.
if boolean expression:
indented line 1
indented line 2
...
non-indented line
The indented lines below the if
form a code block.
If the boolean is True
, the indented block runs.
If the boolean is False
, the indented block is skipped.
(Recommended to use four spaces to indent a block.)
Let's write a program that will tell the user whether a quadratic polynomial $$a x^2 + bx + c$$ is a perfect square or not.
An $\texttt{if}$ statement can be followed by $\texttt{else:}$ and a code block to be executed if the condition is False.
if x == 100:
print("x is equal to 100")
else:
print("x is NOT equal to 100")
This is useful for handling dichotomies.
An $\texttt{if}$ statement can also be followed by $\texttt{elif}$ (for "else if"), which begins a new conditional.
if height_cm >= 120:
print("you can ride the roller coaster")
elif height_cm >= 100:
print("you can only ride if accompanied by an adult")
else:
print("you are not allowed to ride the roller coaster")
A chain of $\texttt{if}/\texttt{elif}/\texttt{elif}/...$ is the typical way to compare a variable to multiple values or categories.
Let's modify our perfect square program to tell us how many real roots a quadratic polynomial has.
Expression | Condition to be True | In math |
---|---|---|
$x \texttt{ and } y$ | Both $x$ and $y$ are True | $x \land y$ |
$x \texttt{ or } y$ | At least one of $x,y$ is True | $x \lor y$ |
$\texttt{ not } x$ | $x$ is False | $\lnot x$ $!x$ $\bar{x}$ |
What happens if you use a non-boolean value in a conditional? e.g.
if "walrus":
print("Will this statement execute or not?")
Answer: The value will first be converted to boolean.
A few values convert to False (are "falsy"):
""
Anything else converts to True.
x
is a variable of type int, then:
if x != 0:
print("x is nonzero")
does exactly the same thing as
if x:
print("x is nonzero")