Find cube root of a number in Python

In this tutorial, we are going to learn how to find the cube root of a number in Python.

How to find the cube root of a number in Python

Let us understand with an example.
Suppose a number stored in a variable.

a=125

We can find the cube root of 125 using a trick :

a=125
print(a**(1/3))

As we know that the cube root of 125 is 5. So it will return 5.
Note:- This trick will not work in case of negative integers

5.0

If we want to find the cube root of a negative integer. Then we have to make some changes in the above trick.

a=-125
print(-(-a)**(1/3))
-5.0

Function to find cube root using Python:

We can define a function for cube root. When a user inputs a number for cube root, it will automatically return the cube root of the number.

def cube_root(x):
        return x**(1/3)
print(cube_root(27))

As we can see, I have defined a function cube root. And I have called the same function with input 27. So it will return the cube root of 27.

3.0

You may also read:

One response to “Find cube root of a number in Python”

  1. Mr.Mohammad says:

    Hello
    Thanks for your good trick
    But this method does not work correctly on some numbers
    For example: number 185193

Leave a Reply

Your email address will not be published. Required fields are marked *