Chapter 5


8.

Geometry

import math def sqcube():      s = float(raw_input('enter length of one side: '))      print 'the area is:', s ** 2., '(units squared)'      print 'the volume is:', s ** 3., '(cubic units)' def  cirsph():      r = float(raw_input('enter length of radius: '))      print 'the area is:', math.pi * (r ** 2.),       '(units squared)'      print 'the volume is:', (4. / 3.) * math.pi * (r ** 3.),  '(cubic units)' sqcube() cirsph()


11.

Modulus

  1. for i in range(0, 22, 2):    # range(0, 21, 2) okay too     print i

    or

    for i in range(22):           # range(21) okay too     if i % 2 == 0:        print i

  2. for i in range(1, 20, 2):     # range(1, 21, 2) okay too     print i

    or

    for i in  range(20):         # range(21) okay too     if  i % 2 != 0:          print i

  3. When i % 2 == 0, it's even (divisible by 2), otherwise it's odd.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net