36 CHAPTER3. COMPUTINGWITHNUMBERS
- Show thelistofnumbersthatwouldbegeneratedbyeachofthefollowingrangeexpressions.
(a)range(5)
(b)range(3, 10)
(c)range(4, 13, 3)
(d)range(15, 5, -2)
(e)range(5, 3)
- Show theoutputthatwouldbegeneratedbyeachofthefollowingprogramfragments.
(a)for i in range(1,11):
print i*i
(b)for i in [1,3,5,7,9]:
print i, ":",i**3
print i
(c)x = 2
y = 10
for j in range(0,y, x):
print j,
print x + y
print "done"
(d)ans = 0
for i in range(1,11):
ans = ans + i*i
print i
print ans
- Writea programtocalculatethevolumeandsurfaceareaofa spherefromitsradius,givenasinput.
Herearesomeformulasthatmightbeuseful:V 4
3 πr^3 A 4 πr^2
- Writea programthatcalculatesthecostpersquareinchofa circularpizza,givenitsdiameterandprice.
A πr^2
- Writea programthatdeterminesthemolecularweightofa hydrocarbonbasedonthenumberofhy-
drogen,carbon,andoxygenatoms.Youshouldusethefollowingweights:
Atom Weight
(grams/ mole)
H 1.0079
C 12.011
O 15.9994
- Writea programthatdeterminesthedistancetoa lightingstrike basedonthetimeelapsedbetweenthe
flashandthesoundofthunder. Thespeedofsoundis approximately 1100 ft/secand1 mileis 5280ft.
- TheKonditoreicoffeeshopsellscoffeeat $10.50a poundplusthecostofshipping.Eachorderships
for$0.86perpound+ $1.50fixedcostforoverhead.Writea programthatcalculatesthecostofan
order.
- Two pointsina planearespecifiedusingthecoordinates(x1,y1)and(x2,y2).Writea programthat
calculatestheslopeofa linethroughtwo (non-vertical)pointsenteredbytheuser.m yx^22 yx 11
- Writea programthatacceptstwo points(seepreviousproblem)anddeterminesthedistancebetween
them.d
x 2 x 1 ^2
y 2 y 1 ^2