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
Update: Some of our viewers reported the code was not working properly so I have added a new updated code here in the first place:
x = 8 cube_root = x ** (1/3) print(cube_root)
Output:
2.0
If you want to remove that decimal point after the digit you do this:
x = 8 cube_root = x ** (1/3) cube_root_int = int(cube_root) print(cube_root_int)
Output:
2
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 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:
Hello
Thanks for your good trick
But this method does not work correctly on some numbers
For example: number 185193
If I try this with 64, then I get 3 and not 4. Any idea why?
That cant be.
I get 4 when trying it with 64, maybe just check ur code
Cube Root
number = float(input( “Enter a number please? “)
cube_root = number**(1/3)
print(cube_root)
for some digits like 636.it doesn’t give the exact answer instead it give in terms of .99999999
This function has now been removed from Python Math library.
We have updated the post with a new code. Check it. It’s working properly.