Cube root in Python
In this post, you will learn different ways to find the cube root of a number in the Python programming language.
The cube root of a number is the factor that we multiply by itself three times to get that number. In mathematics, the cube root of a number x is a number y, if y³ = x.
Python cube root using the exponent symbol **
The exponent operator (**) returns the result of raising the first operand to the power of the second operand. In Python, we can use this exponent symbol to calculate the cube root of a number. It is also called a power operator. However, we cannot calculate the cube root of the negative numbers by using this method correctly.
Here, we have defined a function cube_root() to calculate the cube root of a number using the exponent symbol.
def cube_root(x):
if x < 0:
x = abs(x)
cube_root = x**(1/3)*(-1)
else:
cube_root = x**(1/3)
return cube_root
print("Cube root of 181:",cube_root(181))
print("\nCube root of 21:",cube_root(21))
print("\nCube root of 81:",cube_root(81))
print("\nCube root of 27:",cube_root(27))
Output of the above code:
Cube root of 181 5.65665282582291
Cube root of 21 2.7589241763811203
Cube root of 81 4.3267487109222245
Cube root of 27 3.0
Python cube root using the pow() function
The pow() function from the Python math module is the easiest way to find the cube root of a number. The pow() function of Python returns the power of a number. This function accepts a number as the first argument and the exponent or power of the number as the second argument.
Here, we have defined a function to calculate the cube root.
def cube_root(x):
if x < 0:
x = abs(x)
cube_root = pow(x,1/3)*(-1)
else:
cube_root = pow(x,1/3)
return cube_root
print("Cube root of 198:",cube_root(198))
print("\nCube root of 8:",cube_root(8))
print("\nCube root of 85:",cube_root(85))
print("\nCube root of 39:",cube_root(39))
Output of the above code:
Cube root of 198: 5.828476683251456
Cube root of 8: 2.0
Cube root of 85: 4.396829672158179
Cube root of 39: 3.3912114430141664
Python cube root using the cbrt() function
The cbrt() function of numpy module is used to find the cube root of a number. This function accepts a number in the first argument and the exponent or power of the number in the second argument. To use this method, the numpy module needs to be installed.
Here, we have defined a function to calculate the cube root using the cbrt() function.
import numpy as np
print("Cube root of 198:",np.cbrt(198))
print("\nCube root of -8:",np.cbrt(-8))
print("\nCube root of 64:",np.cbrt(64))
print("\nCube root of 27:",np.cbrt(27))
Output of the above code:
Cube root of 198: 5.8284766832514565
Cube root of -8:-2.0
Cube root of 64:4.0
Cube root of 27:3.0000000000000004
Cube root of a negative number in Python
In the given example, we are finding the cube root of a negative number using the Python programming language. For this, we need to use the abs() function, and then we can use the simple math equation to calculate.
def findcuberoot(num):
if num < 0:
num = abs(num)
cuberoot = num**(1/3)*(-1)
else:
cuberoot = num**(1/3)
return cuberoot
print("Cube root of 729 : ",findcuberoot(729))
print("Cube root of -729 : ",round(findcuberoot(-729)))
Output of the above code:
Cube root of 729 : 8.999999999999998
Cube root of -729 : -9
Related Articles
Find the stop words in nltk PythonEye Detection Program in Python OpenCV
Python OpenCV Histogram of Grayscale Image
Install NLTK for Python on Windows 64 bit
Python program to map two lists into a dictionary
Python program to input week number and print week day
Python program to list even and odd numbers of a list
Python program to print odd numbers within a given range
Python program to multiply two numbers
Program to find area of triangle in Python
Find area of rectangle in Python
Swapping of two numbers in Python
Find average of n numbers in Python
Print multiplication table in Python
Python program to multiply two matrices
Python program to find area of circle
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Count consonants in a string Python
Convert array to list Python