5.5. Exercises

  1. Use a for statement to print 10 random numbers.

    (ex_mod_1)

    7
     
    1
    import random
    2
    3
    howmany = 10
    4
    for counter in range(howmany):
    5
        arandom = random.random()
    6
        print(arandom)
    7

    (mod_q1_answer)

    Show Comments
  1. Repeat the above exercise but this time print 10 random numbers between 25 and 35, inclusive.

    (ex_mod_2)

  1. The Pythagorean Theorem tells us that the length of the hypotenuse of a right triangle is related to the lengths of the other two sides. Look through the math module and see if you can find a function that will compute this relationship for you. Once you find it, write a short program to try it out.

    (ex_mod_3)

    7
     
    1
    import math
    2
    3
    side1 = 3
    4
    side2 = 4
    5
    hypotenuse = math.hypot(side1,side2)
    6
    print(hypotenuse)
    7

    (mod_q3_answer)

    Show Comments
  1. Search on the internet for a way to calculate an approximation for pi. There are many that use simple arithmetic. Write a program to compute the approximation and then print that value as well as the value of math.pi from the math module.

    (ex_mod_4)

Next Section - 6. Functions