2016 September 14,

CS 111: Booleans

We've already seen how if and while can alter the behavior of a program based on whether a certain condition is true. In this brief section, we focus on the condition itself.

A Boolean value is a value that is either True or False. Enter the following commands into the interactive Python interpreter, to explore Booleans. (The != operator means "does not equal".)

3.2 < 5

type(3.2 < 5)

type(False)

"Obama" == "President"

"Obama"[1:4] == "b" + "am"

3.0 != 8 / 2

4.0 != 8 / 2

You can combine Booleans using the operators and, or, and not. For example, the following code tests whether a given person is allowed to vote:

name = "Big Bird"
age = 42
isFelon = True
if age >= 18 and not isFelon:
    print name, "may vote."
else:
    print name, "may not vote."

Question 12A: Rewrite that code, so that it uses neither and nor not, but instead uses or, and accomplishes exactly the same task.