6

I am pretty new in programming, just learning python.

I'm using Komodo Edit 9.0 to write codes. So, when I write "from math import sqrt", I can use the "sqrt" function without any problem. But if I only write "import math", then "sqrt" function of that module doesn't work. What is the reason behind this? Can I fix it somehow?

Sheikh Ahmad Shah
  • 281
  • 1
  • 4
  • 12

5 Answers5

10

You have two options:

import math
math.sqrt()

will import the math module into its own namespace. This means that function names have to be prefixed with math. This is good practice because it avoids conflicts and won't overwrite a function that was already imported into the current namespace.

Alternatively:

from math import *
sqrt()

will import everything from the math module into the current namespace. That can be problematic.

Community
  • 1
  • 1
fenceop
  • 1,439
  • 3
  • 18
  • 29
  • 1
    However, it's generally not a good idea to do star imports in a normal script. See [Why is “import *” bad?](http://stackoverflow.com/q/2386714/4014959) – PM 2Ring Jun 04 '15 at 14:39
  • In place of using *, I can use "from module_name import func1, func2, func3, ... ," etc. That would be better I guess. – Sheikh Ahmad Shah Jun 04 '15 at 15:08
  • @SheikhAhmadShah Yes, but you could still overwrite an existing function by accident. – fenceop Jun 04 '15 at 15:10
  • 1
    you can also do `from math import sqrt` and `from math import sqrt as square_root_function` So I guess that makes **4** Options – rmoro Jul 07 '16 at 02:57
4

If you only import math to call sqrt function you need to do this:

In [1]: import math

In [2]: x = 2

In [3]: math.sqrt(x)
Out[3]: 1.4142135623730951

This is because from math import sqrt brings you the sqrt function, but import math only brings you the module.

metersk
  • 11,803
  • 21
  • 63
  • 100
  • @Maverick maybe you are thinking of 2^2? – metersk Oct 10 '16 at 15:36
  • @meepi I am actually confused, as I never noticed this result. Ideally square root should be 2**2 = 4 and that is what you expect in math.sqrt(2) would return. could you please explain? – Maverick Oct 10 '16 at 16:32
  • 2
    @Maverick you are thinking of squared, not square root. squared is an exponent like `2**2 = 4` and `2**3 = 8`. square root is `a number that produces a specified quantity when multiplied by itself.` `1.4142135623730951 * 1.4142135623730951 ≈ 2` – metersk Oct 10 '16 at 16:35
  • and what does 2^2 or 4^4 should result in? And I came to this post finding distance between two coordinates (lat, long) and in all those formulas they are writing sqrt, so not sure what to use for the right result. – Maverick Oct 10 '16 at 16:50
  • 1
    @Maverick 2^2 is just and 4^4 is just notation for an exponent. They are identical to `2**2` is just and `4**4`. Calculating the distance between 2 coordinates on our earth uses the Haversine formula, which does indeed use square roots – metersk Oct 10 '16 at 16:57
  • @Maverick yes look at this http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points – metersk Oct 10 '16 at 17:00
  • thanks for the info you gave, I really appreciate it! – Maverick Oct 11 '16 at 02:53
3

When you only use import math the sqrt function comes in under a different name: math.sqrt.

randomusername
  • 7,927
  • 23
  • 50
2

If you need a square root, you can also just exponentiate a number by 0.5.

144 ** 0.5

gives the result:

12.0
V M
  • 3
  • 2
Blake
  • 21
  • 1
0

If the command Import math is present more than once you will get the error: UnboundLocalError: local variable 'math' referenced before assignment

Al Martins
  • 431
  • 5
  • 13