Introduction to Python

Student thinking in Python

Students entering CIS 41A from C++ or other similar languages need to learn how Python is built on references and objects.
Otherwise, they will be confused trying to apply their knowledge of a memory based language to Python.
For students from CIS40 and others with experience with reference-object programming this is a review.

Students entering CIS 41A generally come in with a background in C++ or other similar memory based languages .  
However, Pyhon is a very different language, being built on references and objects.

This is a sketch of a few things CIS 41A entering students are likely to need to learn.

1. Python is based on references and objects.
When you type a name you get the name and also an untyped mutable reference associated with the name.
The name must be initialized to refer to an object by using the set reference operator. 
The operator sets areference (=) in Python looks like the equal sign in mathematics and 
the assign operator in the C programming language, but does not work like either equal or assign.
An identifier for an object can be obtained by using the id(a_name) function.
The identifier for an object is a unique integer (typically implemented by using the memory address of the object).

1a. Notice that I have not used the terminology borrowed from the C programming language
 because this confuses the beginning CIS 41A students.
The students need to learn to think in Python, and confusing terminology does not help.
The word "variable" is commonly used in Python, but with a very different meaning.
Variable is commonly used to refer to a name and its associated reference.
I use the word "name" rather than identifier.  Of course the name must meet the requirements for a C identifier.
I use the name "identifier" for the number returned by the id(a_name) function, which identifies the data object.
We might choose different terminology, but do need to get away from reusing C terminology when discussing Python.

2. lists and tuples do not contain data objects; they only contain references.

3. There are three kinds of copying in Python.

3a. Reference copy:  
This is done by copying the reference, but not copying any objects.  
This is done when using the set reference operator (=).
It is also done when copying an argument to a parameter when calling a function.

3b. Deep copy: 
deepcopy() makes a new copy of all mutable objects found by recursively following the references in a data structure.
The immutable objects may, or may not, be copied; it does not matter that they may be shared because they cannot be changed anyway.

3c. Shallow copy: 
The copy function and many other techniques used in Python copy one object, but do not follow the associated references.

4. Hashable is clear in the Python standard.  This item is not one of the first things students need to know. 
I mention it here because it is something often presented incorrectly in the Internet and other sources.

4a. Immutable objects that contain no references are hashable.

4b. Immutable objects that contain references are hashable only if every reference refers to a hashable object.

4c. All other objects are not hashable.

Of course this short list is not everything students need to learn to think in Python, but is a minimal set of things to get started.