+ 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.

26th Aug 2020, 8:10 AM
Krishna Pandey
Krishna Pandey - avatar
15 Antworten
+ 6
-(-5)=5 In languages like C,C++ ,JS ,etc. these are known as decrement/increment operators.
26th Aug 2020, 8:36 AM
Hima
Hima - avatar
+ 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.
26th Aug 2020, 8:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 6
Funk You Perhaps, but I think it is the effect as shown by Hima -(-5)=5 The fact that it was presented as a challenge is detrimental to the coding ideals of Python. NOTE* My opinion only, and I am often wrong. 😁👍
26th Aug 2020, 10:10 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
Funk You Valid point Thanks for giving me food for thought
26th Aug 2020, 10:14 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Thanks Hima i was convinced "--" did absolutley nothing.
26th Aug 2020, 8:43 AM
Slick
Slick - avatar
+ 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 '-'
26th Aug 2020, 10:06 AM
Chris Crixel
+ 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
27th Aug 2020, 5:44 PM
Ömer Ayhan
Ömer Ayhan - avatar
+ 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...
26th Aug 2020, 10:13 AM
Chris Crixel
+ 1
Rik Wittkopp You're welcome. Thanks for appreciating ^^
26th Aug 2020, 10:16 AM
Chris Crixel
+ 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.
26th Aug 2020, 10:48 AM
Krishna Pandey
Krishna Pandey - avatar
+ 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
27th Aug 2020, 5:06 AM
Ipang
+ 1
Hima I was confused I never knew Python had ++/-- operator But Thanks to your analysis now I know it's just a nested unary minus operator 😂
27th Aug 2020, 5:15 AM
Ipang
0
Ipang You are correct with your words. I meant "++/--" are operators which do exist in those languages unlike in python .
27th Aug 2020, 5:12 AM
Hima
Hima - avatar
0
Php vs python who is best?
27th Aug 2020, 4:38 PM
Owais Baloch
Owais Baloch - avatar
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
28th Aug 2020, 5:40 AM
Kelvin Adu-Darko
Kelvin Adu-Darko - avatar