【Python】数字取反(相反数)的几种方法
方法一:绝对值
if __name__ == "__main__": """run""" print("负数取反-绝对值:{}".format(abs(-28))) print("正数数取反-绝对值:{}".format(abs(32) * -1))
结果:
方法二:numpy库
# coding:utf-8 import numpy as np np.set_printoptions(suppress=True) # 取消科学计数法 if __name__ == "__main__": """run""" print("numpy库取反", np.negative(-28)) print("numpy库取反", np.negative(32))
结果:
方法三:算法取法
if __name__ == "__main__": """run""" print("算法取反", 0-(-28)) print("算法取反", 0-32)
结果
-------------------------------------------------------------------------------------
如果万事开头难 那请结局一定圆满 @ Phoenixy
-------------------------------------------------------------------------------------