Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

math - Is there a short-hand for nth root of x, in Python?

Simple syntax question.

In maths if I have two number 3 and 2 and I wish to calculate 3 to the power of 2 then no symbol is required but I write the two small. In Python this operation seems to be represented by the ** syntax.

>>> 3**2
9

If I want to go the other direction and calculate the 2nd root of 9 then in maths I need to use a symbol:

nth root of x

Is there a short-hand symbol in Python, similar to ** that achieves this i.e.2<symbol>9. Or do I need to use the math module ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

nth root of x is x^(1/n), so you can do 9**(1/2.0) to find the 2nd root of 9, for example. In general, you can compute the nth root of x as:

x**(1/float(n))

You can also do 1.0/n instead of 1/float(n). It is required so that the result is a float rather than an int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...