x = 2
if x == 2:
print x
else:
x +
i get error like print x + ^ SyntaxError: invalid syntax
x = 2
if x == 2:
print x
else:
x +
i get error like print x + ^ SyntaxError: invalid syntax
+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.
This is the unary operator +
, which is like writing -x
but doesn't really do anything. See this article for more information.
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.