N = N + 1
The origin of the confusion is easy to understand. Expressions like
N = N + 1 often occurs in a computer program dealing with mathematical concepts. And, in mathematics, the expression
N = N + 1
is just plain wrong. Ergo the confusion. To get around the confusion, some programming language introduce a different symbol for "variable assignment". Something like ":=" , or "<--" can be used for that purpose. Python uses the equality symbol for variable assignments. As a result, beginners are often presented with the baffling N = N + 1 before they have understood enough about the language to grasp quickly what is meant by that expression. I suggest that examples based on a "human language context" as opposed to a traditional "mathematical context" could be more useful to teach the meaning of variable assignment.
1. A little story
John, Pierre and Antonio are three good friends that share a house. John is originally from England, Pierre from France and Antonio from Spain.
One day, John was speaking in English to Pierre. During that conversation, John said the word "once", referring to an event that occurred a single time. Pierre translated this concept in his mind as "une_fois".
>>> once = 1
>>> une_fois = once
>>> print 'once = ', once, ' une_fois = ', une_fois
once = 1 une_fois = 1
Later that day, John was speaking in Spanish to Antonio. He mentioned his favourite number: "once" (11), made up of the same letters as the English word "once".
>>> once = 11
However, the conversation that John is having with Antonio has not changed the meaning of "une_fois" for Pierre.
>>> print 'once = ', once, ' une_fois = ', une_fois
once = 11 une_fois = 1
The various words in human languages are associated with concepts or objects that are independent of the words used to represent them. So, "une_fois" is associated with the number 1, through its association with the word "once". However, "once" is just a word, not an object. So, when "once" takes on a different value later, the concept represented by "une_fois" is unchanged.
2. The meaning of = in Python
In Python the symbol "=" associates a synonym on its left hand side to a concept on its right hand side.
synonym = concept/object
Words that are chosen as synonyms, are chosen in a context dependent way. In our later story above, names are chosen based on the "human language context". The same combination of letters can mean two different things in different languages, or even in the same language. I remember a little test a friend of mine did in grad school, showing parts of a newspaper headline to fellow classmates (all science geeks) and asking them to quickly read it aloud. The word "unionized" appeared in the headline. Now, think "labour" and "chemistry" and you will think of two different way of pronouncing this word.
Now, in the typical programming situation, "N+1" represents a mathematical concept. We can give it a (temporary) synonym of our own chosing, depending on the context...
3. Another short story
John makes a groceries shopping list, with two items: "bananas" and "pears". He then tapes it on the fridge door.
Later that day, Pierre walks by and notices the list too (une liste d'épicerie) which he associates with the word epicerie. Still later that day, John adds "apples" to the list on the fridge door. He notices that Antonio had already bought pears and remembers that they also need oranges. He thus scratches the item "pears" and replace it by "oranges".
>>> groceries = ["bananas", "pears"]
>>> epicerie = groceries
>>> print groceries
['bananas', 'pears']
>>> print epicerie
['bananas', 'pears']
The list has changed throughout the day, but it remains taped to the fridge. When they think about the grocery list, both John and Pierre are referring to the same object in their own language. When that object changes, it changes for both of them in the same way.
>>> groceries.append("apples")
>>> print groceries
['bananas', 'pears', 'apples']
>>> groceries[1]="oranges"
>>> print groceries
['bananas', 'oranges', 'apples']
>>> print epicerie
['bananas', 'oranges', 'apples']
4. Yet another short story
John makes a groceries shopping list, with two items: "bananas" and "pears". He then tapes it on the fridge door. Later that day, Pierre walks by and notices the list and decides to make his own copy.
>>> groceries = ["bananas", "pears"]
>>> epicerie = list(groceries)
Still later that day, John adds "apples" to the list on the fridge door. He notices that Antonio had already bought pears and remembers that they also need oranges. He thus scratches the item "pears" and replace it by "oranges".
Pierre's list, however, is unchanged.
>>> groceries.append("apples")
>>> groceries[1] = "oranges"
>>> print groceries
['bananas', 'oranges', 'apples']
>>> print epicerieLet's just hope that Pierre won't go out and buy pears!
['bananas', 'pears']
5. Conclusion?
Your comments are going to provide a much better conclusion than I could write at this point!