Reading: Sections 6.1 and 6.2 of Practical Programming. We’ll return to the rest of this chapter later in the semester.
Yet another type
Values are True and False
We’ll see a large series of operations that either produce or use boolean values, including relational operators such as <, <=, etc. and logical operations such as and and or.
We can assign them to variables, as in,
x = True
but we will not explore this until Lecture 9.
Comparisons between values, perhaps values associated with variables, to produce a boolean outcome.
For numerical values, <, <=, >, >= are straightforward:
>>> x = 17
>>> y = 15.1
>>> x < y
False
>>> x <= y
False
>>> x <= 17
True
>>> y < x
True
The comparison operators <, <=, >, >= may also be used for strings but the results are sometimes a bit surprising:
>>> s1 = 'art'
>>> s2 = 'Art'
>>> s3 = 'Music'
>>> s4 = 'music'
>>> s1 < s2
False
>>> s2 < s3
True
>>> s2 < s4
True
>>> s1 < s3
False
With strings, the ordering is what’s called lexicographic rather than purely alphabetical order:
What are the values of the following boolean expressions?
a = 1.6
b = -1.7
c = 15
s = 'hi'
t = "good"
u = "Bye"
v = "GOOD"
w = "Bye"
a < b # A
a < abs(b) # B
a >= c # C
s < t # D
t == v # E
u == w # F
b < y # G
What happens with the following?
u == w
u = v
u == w
General form of what we saw in the example we explored at the start of lecture:
if condition:
block1
else:
block2
where
All statements in the block1 and block2 must be indented the same number of spaces
The block continues until the indentation stops, and returns to the same level of indentation as the statement starting with if
The else: and block2 are optional, as the following example shows.
Suppose we want to input the heights of two siblings and output the name and height of the taller of them.
We could write two consecutive if statements:
name1 = "Dale"
print "Enter the height of %s in cm ==> " %name1,
height1 = int(raw_input())
name2 = "Erin"
print "Enter the height of %s in cm ==> " %name2,
height2 = int(raw_input())
if height1 < height2:
print "%s is taller" %name2
max_height = height2
if height1 >= height2:
print "%s is taller" %name1
max_height = height1
print "The max height is %d" %max_height
Since the two if statements are (seemingly) exclusive, we can use the if-else structure to simplify this:
name1 = "Dale"
height1 = int(raw_input("Enter the height of %s in cm ==> " %name1))
name2 = "Erin"
height2 = int(raw_input("Enter the height of %s in cm ==> " %name2,))
max_height = 0;
if height1 < height2:
print "%s is taller" %name2
max_height = height2
else:
print "%s is taller" %name1
max_height = height1
print "The max height is %d" %max_height
Note that none of the blank lines are required for these programs to have correct syntax.
Note also that neither program handles the case of Dale and Erin being the same height. For this we need the next Python construct.
Recall the kids guessing game where someone thinks of a number and you have to guess it. The only information you are given is that the person who knows the number tells you if your guess is too high, too low, or if you got it correct.
When we have three or more alternatives to consider we use the if-elif-else structure:
if condition1:
block1
elif condition2:
block2
else:
block3
We’ll write the solution and discuss the flow of Python execution in class.
Notes:
Consider the following piece of Python code:
if cel > 0 and cel < 100:
print "At %dC water is liquid" %cel
Consider the following:
if cel < 0 or cel > 100:
print "At %dC water is not a liquid" %cel
The boolean expression is True if ANY of the following occurs
This is called the inclusive or and it is somewhat different from common use of the word or in English.
For examples, in the sentence
You may order the pancakes or the omlet.
usually means you may choose pancakes, or you may choose an omelet, but you may not choose both (unless you pay extra).
We can also “logically negate” a boolean expression using not.
a = 15
b = 20
if not a<b:
print "a is not less than b"
else:
print "a is less than b"
What are the results of the following Python boolean expressions:
>>> x = 15
>>> y = -15
>>> z = 32
>>> x == y and y < z #A
>>> x == y or y < z #B
>>> x == abs(y) and y < z #C
>>> x == abs(y) or y < z #D
>>> not x == abs(y) #E
>>> not x != abs(y) #F
Suppose the bounds of a rectangle are defined by
x0 = 10
x1 = 16
y0 = 32
y1 = 45
Note that a point at location is inside the rectangle if