![](/img/trans.png)
[英]Negative numbers in range() function - how to print from positive to negative numbers?
[英]How the (-) operator converting negative numbers to positive works?
这是一个例子:
def absolute(x):
if x >= 0:
return x
else:
return -x
print(absolute(3))
print(absolute(-119))
Output:
3
119
现在,仅使用 (-) 减法运算符, -119
是如何变成正数的?
更新:
对于那些误解我的q的人,
所以我再说一遍,-119 如何通过使用 (-) 运算符变成 119! 如上面代码中的return -x
一样。 不,我知道这将是一个乘法,但它是如何通过使用 (-) 运算符来实现的?
您似乎将二元运算符x - y
又名“x 减 y”与一元运算符-x
又名“否定 x”混淆了。 它们是不同的东西。
-x
返回x
的否定。 如果x
为正,则-x
为负,反之亦然。
一元-
是: https://docs.python.org/3/library/operator.html#operator.neg
二进制-
是: https://docs.python.org/3/library/operator.html#operator.sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.