To find the square root and cube root of a number in Python, you can use the sqrt()
function from the math
module and the pow()
function from the math
module or the **
operator.
Here’s an example of how to find the square root and cube root of a number in Python:
import math
def square_root(x):
return math.sqrt(x)
def cube_root(x):
return x ** (1/3)
print(square_root(4)) # Output: 2.0
print(square_root(9)) # Output: 3.0
print(square_root(16)) # Output: 4.0
print(cube_root(8)) # Output: 2.0
print(cube_root(27)) # Output: 3.0
print(cube_root(64)) # Output: 4.0
cube root in python numpy
To find the cube root of a number in Python using NumPy, you can use the cbrt()
function from the numpy
module. Here’s an example of how to use the cbrt()
function from the numpy
module to find the cube root of a number in Python:
import numpy as np
def cube_root(x):
return np.cbrt(x)
print(cube_root(8)) # Output: 2.0
print(cube_root(27)) # Output: 3.0
print(cube_root(64)) # Output: 4.0