Whenever I try to use any of the built-in functions of Python's exponentiation and logarithms module, I get an error like this:
NameError: name 'sqrt' is not defined
I have tried using math.sqrt(4)
,sqrt(4)
and sqrt(4.0)
, but none of them work. The exception is pow
, which works as it's supposed to. This is really strange and I'm not sure what's wrong.
What is math module in Python? The math module is a standard module in Python and is always available. To use mathematical functions under this module, you have to import the module using import math . It gives access to the underlying C library functions.
As I mentioned earlier, the math module comes packaged with the standard Python installation. So, it is a built-in Python module, and to use it you just need to import it. Printing the type() of math will tell you that it is a module. dir() will give you all available attributes and methods available in math .
These modules are not written in Python but in C. You can find them (at least on linux) in a subfolder of the lib-folder called lib-dynload. The math module is then in a file math.cpython-33m.so (on windows probably with .
Python has a built-in module that you can use for mathematical tasks. The math module has a set of methods and constants.
pow
is built into the language(not part of the math library). The problem is that you haven't imported math.
Try this:
import math math.sqrt(4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With