square root function on Python using Spyder

52 Views Asked by At

I'm just starting out using both Python under Spyder. I was about to do a simple square root calculation. I see that in Python, you need to do an "import math". I did that in Spyder and got an error: 'math' imported but unused. I tried using sqrt() but that threw an error. There's something fundamental I'm missing here. I did a bunch of searching and still have not been able to figure this issue out. Any help would be greatly appreciated.

thanks! clem

3

There are 3 best solutions below

0
SIGHUP On BEST ANSWER

I can think of 3 options.

1:

import math

n = 5

print(math.sqrt(n))

2:

from math import sqrt

n = 5

print(sqrt(n))

3:

n = 5

print(n ** 0.5)
0
inspectorG4dget On
import math

print(math.sqrt(25))

Or

from math import sqrt

print(sqrt(25))

And oh! for good measure:

import math

def sqrt(n):
    return "I am not the function you're looking for"

print(math.sqrt(25))
print(sqrt(25))  # see what happened here?

0
Clem On

Sorry all -- rookie move! I thought the variable containing a number inherited the math methods - and I thought I saw this in some of the stuff that I read! So I was trying z = 10 print( z.sqrt )

I got it now! I started in with MS basic, moved to Fortran, some java, some javascript, some others. I'm trying to do a little math -- writing a little thing that calculate the determinant of a 2x2 matrix formed by a couple of vectors (yeah, i know -- there's prob a Det method). I got the code work in another language but just tried it in Python as a learning task. Apologies for the dumb question but appreciate you all giving me a hand.