[Tutor] How do I make Python calculate square roots?
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Jul 4 01:02:56 CEST 2005
On Sat, 2 Jul 2005, Reed L. O'Brien wrote:
> > Does anyone know how to make Python calculate square roots?
> >
> Raise to the 1/2 power.
>
> >>> 4**.5
> 2.0
> >>> 16**.5
> 4.0
> >>> 81**.5
> 9.0
By the way, if you need to take the square roots of negative numbers, then
the 'sqrt' function in the cmath module should do the trick:
######
>>> (-1)**0.5
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: negative number cannot be raised to a fractional power
>>>
>>> import cmath
>>> cmath.sqrt(-1)
1j
######
Best of wishes!
More information about the Tutor
mailing list