Why does 1+++2 = 3?
Asked Answered
P

6

32

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

Pacifism answered 22/1, 2009 at 17:41 Comment(3)
So does that mean there are no increment operators in python?Pacifism
To increment a variable i in python: i += 1Janice
-1: The question makes a huge number of assumptions about ++ and --; things which are answered directly in python.org/doc/2.5.2/ref/expressions.htmlSub
J
63

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

and

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
Janice answered 22/1, 2009 at 17:46 Comment(1)
Btw: That was a design decision from the Python creators. It should exactly prohibit writing such indetermined code like i+++j.Sputnik
I
15

The extra +'s are not incrementors (like ++a or a++ in c++). They are just showing that the number is positive.

There is no such ++ operator. There is a unary + operator and a unary - operator though. The unary + operator has no effect on its argument. The unary - operator negates its operator or mulitplies it by -1.

+1

-> 1

++1

-> 1

This is the same as +(+(1))

   1+++2

-> 3 Because it's the same as 1 + (+(+(2))

Likewise you can do --1 to mean - (-1) which is +1.

  --1

-> 1

For completeness there is no * unary opeartor. So *1 is an error. But there is a ** operator which is power of, it takes 2 arguments.

 2**3

-> 8

Infective answered 22/1, 2009 at 17:47 Comment(0)
A
4

1+(+(+2)) = 3

1 - (-2) = 3

1 - (-(-2)) = -1

Augmentation answered 22/1, 2009 at 17:46 Comment(0)
P
4

Trying Unary Plus and Unary minus:

The unary - (minus) operator yields the negation of its numeric argument.

The unary + (plus) operator yields its numeric argument unchanged.

>>> +2
2
>>> ++2
2
>>> +++2
2
>>> -2
-2
>>> --2
2
>>> ---2
-2
>>> 1+(++2)
3
Plumage answered 22/1, 2009 at 17:47 Comment(2)
but I am getting the result 3 for 1+++++++++++++2 alsoPacifism
No sweat: 1+(+(+(+(+(+(+(+(+(+(+(+(+2)))))))))))) 12 +'s are unary - see Greg Hewgill's reply.Plumage
L
1

Think it as 1 + (+1*(+1*2))). The first + is operator and following plus signs are sign of second operand (= 2).

Just like 1---2 is same as 1 - -(-(2)) or 1- (-1*(-1*(2))

Leblanc answered 22/1, 2009 at 17:47 Comment(0)
D
1

I believe it's being parsed as, the first + as a binary operation (add), and the rest as unary operations (make positive).

 1 + (+(+2))
Dissociation answered 22/1, 2009 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.