[Tutor] How do I do square roots and exponents?

Kirby Urner urnerk@qwest.net
Wed, 27 Mar 2002 17:40:47 -0800


At 06:16 PM 3/27/2002 -0500, you wrote:
>While toying around in Python, I decided to make a program
>that calculates the hypotenuse. I ran into a problem when
>I realized that I had no idea what the command was for
>square roots and exponents. Does anyone know?

Actually, there's a built in function for this, in the
math library:

  >>> import math
  >>> math.hypot(3,4)
  5.0
  >>> math.hypot(5,6)
  7.810249675906654

Here's a challenge:  write a function that takes the
square root of the sum of the squares of a *list* of
numbers (a list of arbitrary length) e.g.

  >>> f([3,4,5])
  7.0710678118654755
  >>> math.sqrt(3**2 + 4**2 + 5**2)
  7.0710678118654755

The hypotenuse would fall out as a special case when
you have a list of only two numbers.  Otherwise, you
can think of this as a measure of the distance of
(a,b,c...) from (0,0,0...) in n-dimensional Euclidean
space (or something).

Kirby