b1 = True
b2 = False
b3 = b1 and b2
b4 = b1 or b2
b5 = not b1
b6 = (b1 and b2) or (b4 and b3)
print(b1, b2, b3, b4, b5, b6)
We can also create booleans as the result of comparison operators.
x = 4
y = 10
z = 18
b1 = x < y
b2 = x > y
b3 = x >= y
b4 = x >= x
b5 = z <= z
b6 = z <= x
b7 = x == x
b8 = x == y
print(b1, b2, b3, b4, b5, b6, b7, b8)
A very common error is to mix up a single equals sign from multiple equals signs. A single equal sign is used for variable assignment. It means take the variable name on the left hand side of the equals sign and the expression on the right hand side and create a new variable. On the other hand, two equal signs in a row are the equality comparison which takes two things and returns a boolean depending on if they are equal or not. You should think of it as b8 = (x == y)
so x == y
is the expression and the result of that expression (which is a boolean) is assigned into the variable b8.
Comparisons work on strings as well as numbers, where the ordering is what we call the dictionary ordering.
x = "Hello"
y = "Goodbye"
z = "Goodman"
print(x == y)
print(x == x)
print(x < y)
print(x > y)
print(y >= z)
In python, the most important use of booleans is in an if statement. Section 4.1 that I linked to describes it pretty well. Any expression which produces a boolean can appear after the if
and the value of the boolean determines which block of code is executed. The else
and elif
parts are optional.
name = input("Enter a name: ")
# Lower case letters are sorted after upper case letters, so the
# dictionary ordering will place any string which starts with
# an upper case letter as smaller than "a".
if name >= "a":
print("You did not capitalize your name!")
print("What gives?")
if name == "John" or name == "Jon":
print("That's my name")
x = 20
elif name > "R":
print("Hello, ", name)
x = 55
else:
print("Hi, ", name)
x = 104
print(x)
Put the above code into a file and run it enough so that you get all the possible responses.
The if
must end with a colon and which blocks to execute are signaled by indentation. So depending on the boolean, either zero or one of the indented block will be executed. But no matter what, execution (the fetch-decode-execute cycle) will continue at the next statement which is not indented. That is why print x
is always executed no matter which block of the if statement was executed.
We visualize what happens with if
statements using Flow Charts (or the wikipedia page).
Problem 1: Write out the truth table for the following boolean expression
(x or y) and (not(x) or z) and (not(y) or z)
Problem 2: A year is a leap year if it is divisible by four unless it is a century that is not divisible by 400. Write a program which prompts for a year and prints a message depending on if the year is a leap year or not. Hint: you will need the remainder operator which is percent sign, try typing 14 % 3
into the python interpreter.
Problem 3: Draw a flow chart for your code from Problem 2. Since we submit electronically, you can use a camera to take a picture of your flow chart and submit the picture. If you want you can investigate flow-chart drawing software like graphviz but this is not required.