1

I am new to python and I read that there is no ++ operator in Python but I am not able to understand the below code.

>>>print (2++3)
>>>5
partha
  • 23
  • 6

2 Answers2

2

+ and - act as unary as well as binary operators. So,

  • a ++ b is same as a + (+b)
  • a -+ b is same as a - (+b)
  • a -- b is same as a - (-b)
  • a +- b is same as a + (-b)

As can be seen below

>>> 2++3
5
>>> 2-+3
-1
>>> 2--3
5
>>> 2+-3
-1
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
1

This is not the ++ operator. Your code is interpreted as follows:

2 + (+3)

Now since +3 is 3, the final result is 5. For fun, try the following:

  • 2++++++++++++++3
  • 2+++++++++++++++++++++-8

See Expressions for details.

Maroun
  • 94,125
  • 30
  • 188
  • 241