要计算Python中的平方、平方根或指数,可以使用数学库;但是,你也可以用numpy库
import math
import numpy as np
print('\nExample of taking the square')
print(math.pow(2,2))
print(np.square(2))
print('\nExample of taking the cube')
print(math.pow(2,3))
print(np.power(2,3))
print('\nExample of taking the square root')
print(math.sqrt(4))
print(np.sqrt(4))
print('\nExample of taking the exponent')
print(math.exp(3))
print(np.exp(3))
numpy库允许你在列表的元素上运行数学表达式,但数学库不能做到这一点
print('\nExample of squaring elements in a list')
print(np.square([1, 2, 3, 4, 5]))
print('\nExample of taking the square root of a list')
print(np.sqrt([1, 4, 9, 16, 25]))
print('\nExamples of taking the cube of a list')
print(np.power([1, 2, 3, 4, 5], 3))