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?

asked Jun 4 '15 at 14:29

5

  • you are not using math.sqrt() to access the above mentioned method.

    Jun 4 '15 at 14:30

  • You need to use math.sqr

    Jun 4 '15 at 14:31

  • I can accept answer after 15 minutes may be. Ok, I will do that. Thanks. @randomusername

    Jun 4 '15 at 14:39

  • FWIW, if you just want to do square roots, don't bother importing sqrt. Instead of sqrt(x) just do x ** 0.5 - the built-in exponentiation operator is efficient & saves a function call.

    Jun 4 '15 at 14:41

  • No, I was just trying to understand how module works. Anyway, thanks for your kind suggestion. @PM2Ring

    Jun 4 '15 at 14:45

4 Answers 4

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.

answered Jun 4 '15 at 14:31

4

  • However, it's generally not a good idea to do star imports in a normal script. See Why is "import *" bad?

    Jun 4 '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.

    Jun 4 '15 at 15:08

  • @SheikhAhmadShah Yes, but you could still overwrite an existing function by accident.

    Jun 4 '15 at 15:10

  • you can also do from math import sqrt and from math import sqrt as square_root_function So I guess that makes 4 Options

    Jul 7 '16 at 2:57

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.

answered Jun 4 '15 at 14:33

7

  • @Maverick maybe you are thinking of 2^2?

    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?

    Oct 10 '16 at 16:32

  • @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

    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.

    Oct 10 '16 at 16:50

  • @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

    Oct 10 '16 at 16:57

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

answered Jun 4 '15 at 14:31

0

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

                144 ** 0.5                              

gives the result:

                12.0                              

answered Feb 3 '18 at 14:16

Not the answer you're looking for? Browse other questions tagged python module komodo or ask your own question.