With mutable object like ``list`` when we mutate the object the state of the object is modified.
With mutable object like ``list``, when we mutate the object, the state of the object is modified.
But the reference to the object is still unchanged.
But the reference to the object is still unchanged.
So in this example we have two ways to access to the list [1,2] if we modify the state of the list itself.
but not the references to this object, then the 2 variables x and y still reference the list containing
[1,2,3,4].
compare with the exercise on string and integers:
Comparison with the exercise on strings and integers:
Since list are mutable, when ``+=`` is used the original list object is modified, so no rebinding of *x* is necessary.
Since lists are mutable, when ``+=`` is used, the original list object is modified, so no rebinding of *x* is necessary.
We can observe this using *id()* which give the memory address of an object. This address does not change after the
We can observe this using *id()* which gives the memory address of an object. This address does not change after the
``+=`` operation.
``+=`` operation.
.. note::
.. note::
even the results is the same there is a subtelty to use augmented operator.
Even the results are the same, there is a subtelty to use augmented operator.
in ``a operator= b`` python looks up ``a`` ’s value only once, so it is potentially faster
In ``a operator= b`` opeeration, Python looks up ``a``'s value only once, so it is potentially faster
than the ``a = a operator b``.
than the ``a = a operator b`` operation.
compare ::
Compare ::
x = 3
x = 3
y = x
y = x
...
@@ -95,41 +92,55 @@ and ::
...
@@ -95,41 +92,55 @@ and ::
:figclass: align-center
:figclass: align-center
In this example we have two ways to access to the list ``[1, 2]``.
If we modify the state of the list itself, but not the references to this object, then the two variables ``x`` and ``y`` still reference the list containing
``[1, 2, 3, 4]``.
Exercise
Exercise
--------
--------
wihout using python shell, what is the results of the following statements:
.. note::
.. note::
sum is a function which return the sum of each elements of a list.
``sum`` is a function that returns the sum of all the elements of a list.
Wihout using the Python shell, tell what are the effects of the following statements::
::
x = [1, 2, 3, 4]
x = [1, 2, 3, 4]
x[3] = -4 # what is the value of x now ?
x[3] = -4 # What is the value of x now?
y = sum(x)/len(x) #what is the value of y ? why ?
y = sum(x) / len(x) # What is the value of y? Why?
Solution (using the Python shell ;) )::
>>> x = [1, 2, 3, 4]
>>> x[3] = -4
>>> x
[1, 2, 3, -4]
>>> y = sum(x) / len(x)
>>> y
0.5
y = 0.5
Here, we compute the mean of the values contained in the list ``x``, after having changed its last element to -4.
.. warning::
In python2 the result is ::
.. .. warning::
y = 0
..In python2 the result is ::
because sum(x) is an integer, len(x) is also an integer so in python2.x the result is an integer,
.. y = 0
.. because sum(x) is an integer, len(x) is also an integer so in python2.x the result is an integer,
all the digits after the periods are discarded.
all the digits after the periods are discarded.
Exercise
Exercise
--------
--------
Draw the representation in memory of the following expressions. ::
Draw the representation in memory of the ``x`` and ``y`` variables when the following code is executed::
x = [1, ['a','b','c'], 3, 4]
x = [1, ['a','b','c'], 3, 4]
y = x[1]
y = x[1]
y[2] = 'z'
y[2] = 'z'
# what is the value of x?
# What is the value of x?
.. figure:: _static/figs/list_2-1.png
.. figure:: _static/figs/list_2-1.png
:width: 400px
:width: 400px
...
@@ -141,7 +152,7 @@ Draw the representation in memory of the following expressions. ::
...
@@ -141,7 +152,7 @@ Draw the representation in memory of the following expressions. ::
.. image :: _static/figs/spacer.png
.. image :: _static/figs/spacer.png
When we execute *y = x[1]*, we create ``y`` wich reference the list ``['a', 'b', 'c']``.
When we execute *y = x[1]*, we create ``y`` which references the list ``['a', 'b', 'c']``.
This list has 2 references on it: ``y`` and ``x[1]`` .
This list has 2 references on it: ``y`` and ``x[1]`` .
...
@@ -178,16 +189,22 @@ or ::
...
@@ -178,16 +189,22 @@ or ::
Exercise
Exercise
--------
--------
generate a list containing all codons.
.. note::
A codon is a triplet of nucleotides.
A nucleotide can be one of the four letters A, C, G, T
Write a function that returns a list containing strings representing all possible codons.
Write the pseudocode before proposing an implementation.
pseudocode:
pseudocode:
"""""""""""
"""""""""""
| *function all_codons()*
| *function all_codons()*
| *all_codons <- empty list*
| *all_codons <- empty list*
| *let varying the first base*
| *let vary the first base*
| *for each first base let varying the second base*
| *for each first base let vary the second base*
| *for each combination first base, second base let varying the third base*
| *for each combination first base, second base let vary the third base*
| *add the concatenation base 1 base 2 base 3 to all_codons*
| *add the concatenation base 1 base 2 base 3 to all_codons*
| *return all_codons*
| *return all_codons*
...
@@ -202,7 +219,7 @@ first implementation:
...
@@ -202,7 +219,7 @@ first implementation:
python -i codons.py
python -i codons.py
>>> codons = all_codons()
>>> codons = all_codons()
:download:`codons.py <_static/code/codons.py>` .
:download:`codons.py <_static/code/codons.py>`.
second implementation:
second implementation:
""""""""""""""""""""""
""""""""""""""""""""""
...
@@ -334,6 +351,7 @@ Compare the pseudocode of each of them and implement the fastest one. ::
...
@@ -334,6 +351,7 @@ Compare the pseudocode of each of them and implement the fastest one. ::
>>> s = ">{name} {comment}\n{sequence}".format(id=id, comment=comment, sequence=sequence)
or
or
>>> s = f""{name} {comment} \n{sequence}"
>>> s = f">{name} {comment}\n{sequence}"
Exercise
Exercise
...
@@ -180,7 +181,7 @@ Exercise
...
@@ -180,7 +181,7 @@ Exercise
For the following exercise use the python file :download:`sv40 in fasta <_static/code/sv40_file.py>` which is a python file with the sequence of sv40 in fasta format
For the following exercise use the python file :download:`sv40 in fasta <_static/code/sv40_file.py>` which is a python file with the sequence of sv40 in fasta format
already embeded, and use python -i sv40_file.py to work.
already embeded, and use python -i sv40_file.py to work.