Here is my version, not using the "variable", but using "reference", which seem natural.

On 4 Dec, 2005, at 0:26, RaeNye wrote:

Scenario 1:
----------------
list1 = [2, 3]
Let there be a list built up from two elements: the number 2 and the number
3. We call it "list1".

Create a list with two numbers and bind it to the name "list1"

It will be a good idea to print the list now - before we change it:
>>> list1
[2, 3]


list1[0] = 1
We change the thing called "list1" (which happens to be the object we
created one sentence ago) by replacing its first element from whatever it
was to the number 1.

Why use "thing"? simply talk about "list1"

Set the first item of list1 to the number 1

print list1
   [1, 3]
Now examine the thing called "list1" - it contains two elements: the first is the number 1 (since we changed it to be so) and the second is the number
3 (since it didn't change from the time the object was born).

We can see that the first item was replaced.

Scenario 2:
----------------
a = 1; b = 2; c = 3
We name the number 1 "a", the number 2 "b" and the number 3 "c".

This is very confusing - you should not talk about "a" which is a string!

Create 3 names and bind the numbers 1, 2, and 3 to the names.

list1 = [a, b, c]
We create a list built up from three elements: the object named "a" (i.e.,
the number 1), the object named "b" (i.e., the number 2) and the object
named "c" (i.e., the number 3). We call it "list1".

Create a list containing those numbers.

It can make it more clear to ask: is [a, b, c] equal to [1, 2, 3]?

and let the students find the result, or show it:
>>> [a, b, c] == [1, 2, 3]
True

It should be clear that a is NOT an object, its a name, and it is bound to the object "1" which is a number.

And to make this more clear:

>>> a = 1
>>> type(a)
<type 'int'>
>>> a = 3.14
>>> type(a)
<type 'float'>
>>> a = 'String'
>>> type(a)
<type 'str'>

a is a name, we can bind it to any object.

And:
>>> a = 1
>>> b = 1
>>> a == b
True
>>> a is b
True

Same object can be bound to may names.

list2 = [b, c]
We create a list built up from two elements: the object named "b" (i.e., the number 2) and the object named "c" (i.e., the number 3). We call it "list2".

Create another list from the same numbers

print list1
   [1, 2, 3]
The object named "list1" consists of the number 1, the number 2 and the
number 3. That exactly how it was bulit.

Printing the lists, not much to explain.

print list2
   [2, 3]
The object named "list2" consists of the number 2 and the number 3. That
exactly how it was bulit.

Again.

Scenario 3:
----------------
list1 = [1, 2]
list2 = [3, 4]
We create two lists, named "list1" and "list2" respectively (with the
obvious content: the number 1 ...)

Same as before.

mlist_a = [list1, list2]
We create a list built of the object named "list1" and the object named
"list2" (which are the same two lists created above). We call it "mlist_a".

Why use this strange name "mlist_a"? Instead use:

multiple_lists = [list1, list2]

print mlist_a
   [[1, 2], [3, 4]]
Obvious.

mlist_a[1] = list1
We change the thing called "mlist_a" (which happens to be the object we
created two sentences ago) by replacing its first element to the object
named "list1".
Note that now the object named "mlist_a" contains the same object as both the first and the seconds element: a list that consists of the number 1 and
the number 2.

This is not a good example, because you can show the same thing using much simpler code:

>>> name = 'Foo'
>>> list = [name, name]
>>> list[0]
'Foo'
>>> list[1]
'Foo'
>>> list[0] == list[1]
True
>>> list[0] is list[1]
True

A list contain reference to other objects. The same object can appear multiple times in the same list.

print mlist_a
   [[1, 2], [1, 2]]
Obvious, considering the last note.

But all this code is too dry - better show interesting usage examples for each Scenario. The student does not have to really understand how Python works in this stage, a partial and simple "picture" is needed, the picture may be more complete and correct in the end of the course.

Anyway how you explain things is not the most important thing - its more important that the students will spend a lot of time with Python, trying to do simple things, make errors and learn from them.


Best Regards,

Nir Soffer

לענות