How do I find the difference between two values without knowing which is larger?

前端 未结 11 2230
借酒劲吻你
借酒劲吻你 2020-12-11 14:22

I was wondering if there was a function built into Python that can determine the distance between two rational numbers but without me telling it which number is larger. e.g.

11条回答
  •  误落风尘
    2020-12-11 15:06

    abs(x-y) will do exactly what you're looking for:

    In [1]: abs(1-2)
    Out[1]: 1
    
    In [2]: abs(2-1)
    Out[2]: 1
    

提交回复
热议问题