How to use absolute values ​​in python

爱喝马黛茶的安东尼
Release: 2019-06-22 16:42:52
Original
9190 people have browsed it

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()
Copy after login

Related recommendations: "Python Video Tutorial"

The results are as follows:

1.请输入一个数字:-1
绝对值为:1.000000
2.请输入一个数字:0
绝对值为:0.000000
3.请输入一个数字:2
绝对值为:2.000000
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!