Harold Nelson
1/27/2018
The two possible boolean values are True and False. These values are created by expressions that naturally evaluate to one of these values. Such expressions are statements about the relationship between other values.
Examples.
print ("3 < 4")
print(3 < 4)
print(' ')
print("3 > 4")
print(3 > 4)
print(' ')
print("3 == 4")
print(" note '==' rather than '='")
print(3 == 4)
print(' ')## 3 < 4
## True
##
## 3 > 4
## False
##
## 3 == 4
## note '==' rather than '='
## False
##
Note that we can assign the value of a boolean expression to a variable. The type of this variable is boolean, not numerical.
a = 3 < 4
print(type(a))
print(a)## <class 'bool'>
## True
We use booleans to make our programs do different things in different circumstances. The simplest case uses a boolean to decide whether a set of statements should or should not be executed. The set of statements may be arbitrarily large and its scope is indicated by indentation.
# Do some setup
x = 3
y = 4
cond1 = x < y
# See what we have
print(cond1)
if cond1:
print("cond1 is True")
# Many statements could be here.
# Only execute if cond1 is True
# Statements here are executed regardless.
print("On we go")## True
## cond1 is True
## On we go
Here’s an example to test if a number is evenly divisible by 2.
def divby2(x):
if x%2 == 0:
print(x, "is divisible by 2")
print("divby2 has run. Did it say anything")
divby2(5)
divby2(4) ## divby2 has run. Did it say anything
## 4 is divisible by 2
## divby2 has run. Did it say anything
Create a function pay(hours,rate). It computes a worker’s weekly pay. Up to 40 hours, pay is simply hours * rate. But if a worker has more than 40 hours, the hours above 40 are paid at time-and-a half. Use an if statement
def pay(hours,rate):
amt = hours * rate
if hours > 40:
amt = amt + (hours - 40) * rate * .5
return amt
print(pay(35,10))
print(pay(45,10))## 350
## 475.0
It’s a bit unsatisfactory that when x is not divisible by 2, the function does not say anything specific. We can fix this with an else.
def divby2_e(x):
if x%2 == 0:
print(x, "is divisible by 2")
else:
print(x, "is not divisible by 2")
divby2_e(4)
divby2_e(5)## 4 is divisible by 2
## 5 is not divisible by 2
def example(x): # Note that x is boolean
if x:
print("Here only if x is true")
else:
print("Here only if x is false")
print("Here no matter what x is")
example(True)
print(" ")
example(False)
# Note that example can be called with a boolean expression
print("")
example(3<4)
print("")
example(4<3)## Here only if x is true
## Here no matter what x is
##
## Here only if x is false
## Here no matter what x is
##
## Here only if x is true
## Here no matter what x is
##
## Here only if x is false
## Here no matter what x is
Review Publishers slides from 2.9
import random
import math
def montePi(numDarts):
inCircle = 0
for i in range(numDarts):
x = random.random()
y = random.random()
d = math.sqrt(x**2 + y**2)
if d <= 1:
inCircle = inCircle + 1
pi = inCircle/numDarts * 4
return pi
for i in range(20):
print(montePi(100000))## 3.14372
## 3.1474
## 3.14496
## 3.13984
## 3.1352
## 3.13844
## 3.14664
## 3.13644
## 3.14012
## 3.13752
## 3.14732
## 3.1424
## 3.14528
## 3.13816
## 3.13624
## 3.14808
## 3.14488
## 3.1438
## 3.13832
## 3.14508
Do you see the unnecessary computation?
We can check a series of conditions and act on the first one that’s true. After 1 is found true, the remainder are not examined. The final else is optional
cond1 = False
cond2 = True
cond3 = True
if cond1:
print("cond1 is True." )
elif cond2:
print("cond1 is False and cond2 is True")
elif cond3:
print("cond1 and 2 are false, but cond3 is True")
else:
print("cond1, 2 and 3 are all False")
print("We get here no matter what")
## cond1 is False and cond2 is True
## We get here no matter what
Write a python function grader(pct). The variable pct is a number between 0 and 100. If pct is 90 or more, the grade is 4. If pct is between 80 and 90, the grade is 3. If pct is between 70 and 80, the grade is 2. If oct is between 60 and 70, the grade is 1. If pct is below 60, the grade is o.
def grader(pct):
if pct >= 90:
grade = 4
elif pct >= 80:
grade = 3
elif pct >= 70:
grade = 2
elif pct >= 60:
grade = 1.
else:
grade = 0
return grade
print(grader(90))
print(grader(79))
print(grader(100))
print(grader(59))## 4
## 2
## 4
## 0