It's commonly stated the the += operator is simply a more efficient and shorter to code alternative to using a + operator and saving back to the same variable ... in other words that
original += extra
and
original = original + extra
do the same thing.
But - in Python at least - that's not
quite the case. If we had given the
original variable another name prior to the operation above:
aka = original
and we modify the alternative name after the operation:
aka[1] = 5
we will get different results when we print out
original again.
Using "+" original is unchanged by the reassignment on an element:
[2, 4, 6, 8, 10, 12, 14, 16]
But using +=, aka remains an alias to the variable, so it is changed:
[2, 5, 6, 8, 10, 12, 14, 16]
There are complete samples using +=
[here] and using reassignment
[here] in our source library.
Note - if you are defining your own class, you can set up the + and += operator to perform completely different actions if you like, by defining the __add__ and __iadd__ methods with different code.
(written 2011-02-23, updated 2011-02-24)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y104 - Python - Lists and Tuples [383] Overloading of operators on standard objects in Python - (2005-07-19)
[657] The ternary operator in Python - (2006-03-25)
[899] Python - extend v append on a list - (2006-10-20)
[955] Python collections - mutable and imutable - (2006-11-29)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[1789] Looking for a value in a list - Python - (2008-09-08)
[2280] Creating and iterating through Python lists - (2009-07-12)
[2284] Strings as collections in Python - (2009-07-12)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2719] Traffic lights in Python - (2010-04-13)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3763] Spike solutions and refactoring - a Python example - (2012-06-13)
[4027] Collections in Python - list tuple dict and string. - (2013-03-04)
[4368] Shuffling a list - Ruby and Python - (2014-12-28)
[4722] Embedding more complex code into a named block - (2016-11-04)
Y102 - Python - Fundamentals [328] Making programs easy for any user to start - (2005-05-29)
[633] Copying a reference, or cloning - (2006-03-05)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[956] Python security - trouble with input - (2006-11-30)
[1430] Integer v float - Python - (2007-11-12)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1461] Python - input v raw input - (2007-12-06)
[1878] Pascals Triangle in Python and Java - (2008-11-10)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
[4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
Some other Articles
Day Trip to Hereford, in picturesVisiting relativesRedevelopment on the banks of the Avon in MelkshamWhat we look for in a lunchtime cafe restaurant in MelkshamBeware - a=a+b and a+=b are different - PythonAge limits for Well House Consultants Open Source courses?Oops - I typed ci not vi, and have lost my file ...The cost of roadworks on the economy of our town - 1800 pounds per hour?Insurance against any errors - Volcanoes and PythonComparing German, Dutch and British Railways