1
x = 2
if x == 2:
    print x
else:
    x +

i get error like print x + ^ SyntaxError: invalid syntax

3 Answers3

0

+x calls __pos__(self) and is therefore a valid python statement; x + y is an expression that needs two operands (see binary arithmetic expressions); x + is invalid.

hiro protagonist
  • 40,708
  • 13
  • 78
  • 98
0

This is the unary operator +, which is like writing -x but doesn't really do anything. See this article for more information.

Oliver Ni
  • 2,489
  • 7
  • 28
  • 42
0

In +x, + is an operator that works on a single number, such as -x and ~x. You can refer the bottom table on https://docs.python.org/2/reference/expressions.html.

However, in x +, + is an operator for two numbers. In your code, the second number is not provided, so you got an error.

You can check this answer What's the purpose of the + (pos) unary operator in Python? for why we need +x in Python.

Hengfeng Li
  • 346
  • 1
  • 5
  • 12