72

In maths, if I wish to calculate 3 to the power of 2 then no symbol is required, but I write the 2 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: 2√9 = 3

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • 13
    Might be helpful to know that `x` to the `1/n` power is the same as the `nth` root of `x`. – nhgrif Oct 08 '13 at 18:10
  • 8
    Make sure you use `** 1.0/n` rather than `** 1/n` in Python 2 because of integer division. – Wooble Oct 08 '13 at 18:11
  • 1
    Once you see how simple the answer is, you realize why there's no dedicated syntax for it. – Mark Ransom Oct 08 '13 at 18:16
  • @MarkRansom - I know Mark: although this is one of those questions that I nearly deleted - then left for a minute or two - and turns out the questions simplicity (silliness) has lead to some interesting answers. – whytheq Oct 08 '13 at 18:35
  • 1
    You certainly won't be the last person to forget how to do an "nth root", and now when somebody Googles for it they'll end up here. Deleting the question would be pure selfishness. – Mark Ransom Oct 08 '13 at 18:53
  • 1
    @MarkRansom in my defence though Mark - in maths we don't write 9^(1/2) when we want the square root of 9 - I thought there might be a syntactic equivalent to the mathematical norm. – whytheq Oct 08 '13 at 19:00
  • 1
    We may not write `9^(1/2)`, but in my experience, writing something like `9^(3/2)` is far more common then writing `sqrt(9^3)`, etc. And if you're dealing with the type of math where you've got to do things like `9^(3/2)` anyway, you'll probably just write `9^(1/2)` for consistency. I always did. – nhgrif Oct 08 '13 at 19:59
  • @nhgrif -not too sure if anyone would right `sqrt(9^3)`, it just looks really confusing and is not what I'm suggesting - I'm talking about the root symbol (see above the 9 in the OP) - used pretty extensively in maths [at all levels] - certainly when I was at school - [nth root on wikipedia](http://en.wikipedia.org/wiki/Nth_root) – whytheq Oct 09 '13 at 06:24
  • I don't know how to make the `sqrt` symbol. The point is, I never see anyone writing the `sqrt` symbol when you're talking about fractional powers. I've never seen it. I've always seen it as `x^n`, whether `n` is a whole integer, a fraction, or a decimal, I've always seen it this way. The only way I've seen any back and forth is `1/x^n` vs `x^(-n)`. – nhgrif Oct 09 '13 at 11:58
  • @nhgrif - no problem we might be getting our wires crossed - here it is **√** - I can see it 9 times in the first paragraph of the [wikipedia definition of nth root](http://en.wikipedia.org/wiki/Nth_root) so my point is that _people do use this symbol_. This has been an interesting question/answer and I now know that no such shorthand exists in Python but I can use `**`. Thanks again. – whytheq Oct 10 '13 at 06:38

9 Answers9

126

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

x**(1/n)

Note: In Python 2, you had to do 1/float(n) or 1.0/n so that the result would be a float rather than an int. For more details, see Why does Python give the "wrong" answer for square root?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
  • This doesn't work for negative values of x. For example, the expression `(-8)**(1/3)` gives a complex number instead of `-2`. – idontknow Mar 13 '23 at 02:17
13

You may also use some logarithms:

Nth root of x:

exp(log(x)/n)

For example:

>>> from math import exp, log
>>> x = 8
>>> n = 3
>>> exp(log(x)/n)
2.0
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Idvar
  • 131
  • 1
  • 2
6

Also: x**(n**-1), which is the same but shorter than x**(1/float(n))

Nacib Neme
  • 859
  • 1
  • 17
  • 28
  • 1
    However, it is not shorter than `x**(1./n)` and probably slightly less accurate (but then again, this whole way of computing nth roots is less than optimal). –  Oct 08 '13 at 18:33
5

If you prefer to apply this operation functionally rather than with an infix operator (the ** symbol), you can pass the base and exponent as arguments to the pow function:

In [23]: (9**(0.5)) == pow(9, 0.5)
Out[23]: True

I am also fond of finding new uses for this Infix hack in Python although it's more of a fun aside than a heavy-duty solution. But you could effectively make your own personal symbol for this by doing the following:

class Infix:
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __or__(self, other):
        return self.function(other)
    def __rlshift__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __rshift__(self, other):
        return self.function(other)
    def __call__(self, value1, value2):
        return self.function(value1, value2)


root_of = Infix(lambda x,y: y**(1.0/x))

print 2 |root_of| 9
3.0
ely
  • 74,674
  • 34
  • 147
  • 228
  • `2 <> 9` also works, via the shift-operator special methods. Thanks for the tip on the Infix hack. Freaky and adorable. Also `root_of(2,9)` thanks to the call special method. – Bob Stein Jul 24 '22 at 18:37
3

There is. It's just ** =)

Any nth root is an exponentiation by 1/n, so to get the square root of 9, you use 9**(1/2) (or 9**0.5) to get the cube root, you use 9 ** (1/3) (which we can't write with a simpler fraction), and to get the nth root, 9 ** (1/n).

Also note that as of Python 3, adding periods to integers to make them a float is no longer necessary. Saying 1/3 works the way you would actually expect it to, giving 0.333... as result, rather than zero. For legacy versions of Python, you'll have to remember to use that period (but also critically wonder why you're using a legacy version of a programming language)

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
2

Basically sqrt(9) is equivalent to 9^.5

>>>9**.5
3.0
Ishaan
  • 886
  • 6
  • 12
2

You should do

16**(0.5) #If you print it, you get 4, So you can use this formula.
PersianGulf
  • 2,845
  • 6
  • 47
  • 67
2
def nthrootofm(a,n):
    return pow(a,(1/n))
a=81
n=4
q=nthrootofm(a,n)
print(q)

pow() function takes two parameters .

ravi tanwar
  • 598
  • 5
  • 16
1

You can take the nth root of a number in Python by using nroot included in the libnum library:

from libnum import nroot

nth_root = nroot(a, n)

This would be equivalent to \sqrt[n]{a}

Although note, that it will return the truncated nth root of a.

Chryzl
  • 101
  • 1
  • 3