Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Beware - a=a+b and a+=b are different - Python

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
Y102 - Python - Fundamentals
  [4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
  [4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
  [4324] Learning to program - variables and constants - (2014-11-22)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3886] Formatting output - why we need to, and first Python example - (2012-10-09)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [1878] Pascals Triangle in Python and Java - (2008-11-10)
  [1461] Python - input v raw input - (2007-12-06)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [1430] Integer v float - Python - (2007-11-12)
  [956] Python security - trouble with input - (2006-11-30)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [633] Copying a reference, or cloning - (2006-03-05)
  [328] Making programs easy for any user to start - (2005-05-29)

Y104 - Python - Lists and Tuples
  [4722] Embedding more complex code into a named block - (2016-11-04)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2719] Traffic lights in Python - (2010-04-13)
  [2284] Strings as collections in Python - (2009-07-12)
  [2280] Creating and iterating through Python lists - (2009-07-12)
  [1789] Looking for a value in a list - Python - (2008-09-08)
  [1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [955] Python collections - mutable and imutable - (2006-11-29)
  [899] Python - extend v append on a list - (2006-10-20)
  [657] The ternary operator in Python - (2006-03-25)
  [383] Overloading of operators on standard objects in Python - (2005-07-19)


Back to
Age limits for Well House Consultants Open Source courses?
Previous and next
or
Horse's mouth home
Forward to
What we look for in a lunchtime cafe restaurant in Melksham
Some other Articles
Day Trip to Hereford, in pictures
Visiting relatives
Redevelopment on the banks of the Avon in Melksham
What we look for in a lunchtime cafe restaurant in Melksham
Beware - a=a+b and a+=b are different - Python
Age 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 Python
Comparing German, Dutch and British Railways
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3181_Bew ... ython.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat