问题描述
Python如何计算表达式1+++2
?
How does Python evaluate the expression 1+++2
?
我插入了多少个+
,它正在打印3
作为答案.任何人都可以解释这种行为
How many ever +
I put in between, it is printing 3
as the answer. Please can anyone explain this behavior
对于1--2
,它正在打印3
,对于1---2
,它正在打印-1
And for 1--2
it is printing 3
and for 1---2
it is printing -1
推荐答案
您的表达式与以下内容相同:
Your expression is the same as:
1+(+(+2))
任何数字表达式都可以在-
之前使其为负,或者在+
之后不执行任何操作(存在对称性选项).带有负号:
Any numeric expression can be preceded by -
to make it negative, or +
to do nothing (the option is present for symmetry). With negative signs:
1-(-(2)) = 1-(-2)
= 1+2
= 3
和
1-(-(-2)) = 1-(2)
= -1
我看到您澄清了您的问题,说您来自C背景.在Python中,C中没有像++
和--
这样的增量运算符,这可能是造成混淆的原因.要在Python中递增或递减变量i
或j
,请使用以下样式:
I see you clarified your question to say that you come from a C background. In Python, there are no increment operators like ++
and --
in C, which was probably the source of your confusion. To increment or decrement a variable i
or j
in Python use this style:
i += 1
j -= 1
这篇关于为什么1 +++ 2 = 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!