udpsunil udpsunil - 1 year ago 76
Python Question

Why does 1+++2 = 3?

How does Python evaluate the expression 1+++2?

How many ever '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior

And for 1--2 it is printing 3 and for 1---2 it is printing -1

Answer Source

Your expression is the same as:


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

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download