Python Programming: An Introduction to Computer Science

(Nora) #1
36 CHAPTER3. COMPUTINGWITHNUMBERS


  1. Show thelistofnumbersthatwouldbegeneratedbyeachofthefollowingrangeexpressions.


(a)range(5)
(b)range(3, 10)
(c)range(4, 13, 3)
(d)range(15, 5, -2)
(e)range(5, 3)


  1. 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


  1. Writea programtocalculatethevolumeandsurfaceareaofa spherefromitsradius,givenasinput.
    Herearesomeformulasthatmightbeuseful:V 4



3 πr^3 A 4 πr^2


  1. Writea programthatcalculatesthecostpersquareinchofa circularpizza,givenitsdiameterandprice.
    A πr^2

  2. Writea programthatdeterminesthemolecularweightofa hydrocarbonbasedonthenumberofhy-
    drogen,carbon,andoxygenatoms.Youshouldusethefollowingweights:
    Atom Weight
    (grams/ mole)
    H 1.0079
    C 12.011
    O 15.9994

  3. Writea programthatdeterminesthedistancetoa lightingstrike basedonthetimeelapsedbetweenthe
    flashandthesoundofthunder. Thespeedofsoundis approximately 1100 ft/secand1 mileis 5280ft.

  4. TheKonditoreicoffeeshopsellscoffeeat $10.50a poundplusthecostofshipping.Eachorderships
    for$0.86perpound+ $1.50fixedcostforoverhead.Writea programthatcalculatesthecostofan
    order.

  5. Two pointsina planearespecifiedusingthecoordinates(x1,y1)and(x2,y2).Writea programthat
    calculatestheslopeofa linethroughtwo (non-vertical)pointsenteredbytheuser.m yx^22  yx 11

  6. Writea programthatacceptstwo points(seepreviousproblem)anddeterminesthedistancebetween
    them.d 



x 2  x 1 ^2 


y 2  y 1 ^2
Free download pdf