Python math module

余生颓废 提交于 2019-12-04 22:32:54

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)

You can also import as

from math import *

Then you can use any mathematical function without prefixing math. e.g.

sqrt(4)

add:

import math

at beginning. and then use:

math.sqrt(num)  # or any other function you seem neccessary

You need to say math.sqrt when you use it. Or, do from math import sqrt.

Hmm, I just read your question more thoroughly.... How are you importing math? I just tried import math and then math.sqrt which worked perfectly. Are you doing something like import math as m? If so, then you have to prefix the function with m (or whatever name you used after as).

pow is working because there are two versions: an always available version in __builtin__, and another version in math.

import math #imports math module

import math as m
print(m.sqrt(25))

from math import sqrt #imports a method from math module
print(sqrt(25))

from math import sqrt as s
print(s(25))

from math import *
print(sqrt(25))

In

from math import sqrt

Using sqrt(4) works perfectly well. You need to only use math.sqrt(4) when you just use "import math".

import math as m a=int(input("Enter the no")) print(m.sqrt(a))

from math import sqrt print(sqrt(25))

from math import sqrt as s print(s(25))

from math import * print(sqrt(25))

All works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!