What is the property of unary plus and minus operators in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is the property of unary plus and minus operators in python?

Unary plus represented as +@ and unary minus as -@. How do they differ from conventional (mathematical) + and -.

25th Aug 2019, 2:43 PM
Byk
1 Answer
+ 6
You can use both versions for example for incrementing a variable: num *= 2 num = num * 2 If you take a look at the byte code, you can see that 2 differnt methods of calculation are applied: 2 0 LOAD_FAST 0 (x) 2 LOAD_CONST 1 (2) 4 INPLACE_MULTIPLY <<<<< 6 STORE_FAST 0 (x) 3 8 LOAD_FAST 0 (x) 10 RETURN_VALUE ********** 2 0 LOAD_FAST 0 (x) 2 LOAD_CONST 1 (2) 4 BINARY_MULTIPLY <<<<< 6 STORE_FAST 0 (x) 3 8 LOAD_FAST 0 (x) 10 RETURN_VALUE ********** so first version uses an INPLACE_MULTIPLY, the second version uses a BINARY_MULTIPLY. I dont know the difference. Execution time has to be checked separately.
25th Aug 2019, 3:42 PM
Lothar
Lothar - avatar