Is there any difference between a+=1 and a=a+1?(although i think it's the same I want to know if there's any exception ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Is there any difference between a+=1 and a=a+1?(although i think it's the same I want to know if there's any exception )

4th Oct 2018, 1:11 PM
partha
partha - avatar
13 Answers
+ 11
a+=1 is the exact same as a=a+1, and is used just a shorter way of writing it (I can't really think of any situation where you would need to use a=a+1 rather than a+=1, so I would just recommend sticking with the latter as writing the least amount of code is always your best option.
4th Oct 2018, 1:24 PM
Faisal
Faisal - avatar
+ 5
Its the same!
22nd Nov 2018, 2:39 PM
Ale Izquierdo Picasso
Ale Izquierdo Picasso - avatar
+ 4
The difference is one character in the source code discounting white space.
25th Mar 2019, 5:58 AM
Sonic
Sonic - avatar
+ 3
If incrementing by 1 just use ++ ... otherwise use += ... or varname = varname + num. Not sure if there's a difference in performance from one over another. Careful here too... note that using + can also concatenate strings. So be sure what you are incrementing is a number.
8th Oct 2018, 2:40 PM
Mike
Mike - avatar
+ 1
thanks Faisal
4th Oct 2018, 1:25 PM
partha
partha - avatar
+ 1
hi no, both have same result.
6th Nov 2018, 4:47 PM
mohammed
mohammed - avatar
+ 1
No
7th Nov 2018, 7:37 PM
Mohd. Mudassir Ansari
Mohd. Mudassir Ansari - avatar
+ 1
As I know there is a little diference on CPU timming, independently of the language. Check this code to comprobe: https://code.sololearn.com/cH4Pt8zaFR3d/?ref=app
25th Mar 2019, 7:30 AM
BinaryEden
BinaryEden - avatar
0
Nozoo
22nd Jan 2019, 2:17 AM
Chahat singh
Chahat singh - avatar
0
It is the same ~no difference.
27th Jan 2019, 9:48 PM
David Lee
David Lee - avatar
0
a+=1 is exactly the same to a =a+1; There is no program error as I think
31st Jan 2019, 10:43 AM
Yuusuf Abdirahman Hussein
Yuusuf Abdirahman Hussein - avatar
0
No, a+=1 is the shortest form of a=a+1
20th Feb 2019, 11:38 AM
Alex
Alex - avatar
- 2
Of course!
23rd Jan 2019, 7:37 PM
Barra Mohamed
Barra Mohamed - avatar