Home > Article > Backend Development > How to use absolute values in python
How to use absolute values in python? Here are three methods for finding absolute values:
import math def abs_value1(): a = float(input('1.请输入一个数字:')) if a >= 0: a = a else: a = -a print('绝对值为:%f' % a) def abs_value2(): a = float(input('2.请输入一个数字:')) a = abs(a) print('绝对值为:%f' % a) def abs_value3(): a = float(input('3.请输入一个数字:')) a = math.fabs(a) print('绝对值为:%f' % a) abs_value1() abs_value2() abs_value3()
Related recommendations: "Python Video Tutorial"
The results are as follows:
1.请输入一个数字:-1 绝对值为:1.000000 2.请输入一个数字:0 绝对值为:0.000000 3.请输入一个数字:2 绝对值为:2.000000
The above is the detailed content of How to use absolute values in python. For more information, please follow other related articles on the PHP Chinese website!