+ 4
-- in Python ? [ Solved ]
One day playing challenges I saw this question : https://code.sololearn.com/cXwsn5i1JS1Y/?ref=app I answered it wrong and my opponent too. I thought this is a wrong question but when I ran it, it surprisingly worked with any errors. Don't know how this '--' syntax came into Python.
15 Respostas
+ 6
-(-5)=5
In languages like C,C++ ,JS ,etc. these are known as decrement/increment operators.
+ 8
These are the sort of questions that made me realise that Python challenges are a waste of time.
Python promotes readable script.
I can't think of any reason why this concept would be written in a real world code.
+ 5
Funk You Valid point
Thanks for giving me food for thought
+ 3
Thanks Hima i was convinced "--" did absolutley nothing.
+ 2
Rik Wittkopp: I think it is not a concept that was coded on purpose, it's rather a 'side effect' of the definition/handling of the operator '-'
+ 2
If you look at mathematical perspective you are basically multiplying signs with each other.
That means if you have even numbers of minus it will turn to a positive number
+ 1
Yes I agree Rik Wittkopp, it is exactly this effect. Well I guess they included it into the challenge to 'show' that there is a difference to C/C++ and Java etc...
+ 1
Rik Wittkopp You're welcome. Thanks for appreciating ^^
+ 1
Thank you fellows for answering.
As Hima Hima said -(-5)=5
That's why ++a was also giving same answer because : +(+5)=5
It has nothing to do with In/decrement.
+ 1
Hima
Idk about C and JS, but in C++ such behaviour is the works of unary plus/minus operator.
https://en.cppreference.com/w/cpp/language/operator_arithmetic#Unary_arithmetic_operators
0
Php vs python who is best?
0
Its just like maths ,even the comments given clarifies that( - plus - equals +) i.e minus plus minus equals plus.
b= --a
b=-(-a) #the minus multiplies the minus in the bracket to give plus
b=a