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:
- How to check if a number is a perfect square in Python
- Music Recommendation System Project using Python
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?
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