Why do i get error when i use x+ but not when +x in this code?
x = 2 if x == 2: print x else: x +
i get error like print x + ^ SyntaxError: invalid syntax
This is the unary operator
+, which is like writing
-xbut doesn't really do anything. See this article for more information.
+is an operator that works on a single number, such as
~x. You can refer the bottom table on https://docs.python.org/2/reference/expressions.html.
+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