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::

``sum`` is a function that returns the sum of all the elements of a list.

.. note::

sum is a function which return the sum of each 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?

y = 0.5

Solution (using the Python shell ;) )::

.. warning::

>>> x = [1, 2, 3, 4]

>>> x[3] = -4

>>> x

[1, 2, 3, -4]

>>> y = sum(x) / len(x)

>>> y

0.5

Here, we compute the mean of the values contained in the list ``x``, after having changed its last element to -4.

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

:alt: set

:alt: set

:figclass: align-center

:figclass: align-center

.. container:: clearer

.. container:: clearer

.. 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]`` .

.. figure:: _static/figs/list_2-2.png

.. figure:: _static/figs/list_2-2.png

:width: 400px

:width: 400px

:alt: set

:alt: set

:figclass: align-center

:figclass: align-center

.. container:: clearer

.. container:: clearer

.. image :: _static/figs/spacer.png

.. image :: _static/figs/spacer.png

This object is a list so it is a mutable object.

This object is a list so it is a mutable object.

So we can access **and** modify it by the two ways ``y`` or ``x[1]`` ::

So we can access **and** modify it by the two ways ``y`` or ``x[1]`` ::

x = [1, ['a','b','z'], 3, 4]

x = [1, ['a','b','z'], 3, 4]

Exercise

Exercise

--------

--------

...

@@ -177,17 +188,23 @@ or ::

...

@@ -177,17 +188,23 @@ 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*

...

@@ -201,14 +218,14 @@ first implementation:

...

@@ -201,14 +218,14 @@ 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:

""""""""""""""""""""""

""""""""""""""""""""""

Mathematically speaking the generation of all codons can be the cartesian product

Mathematically speaking the generation of all codons can be the cartesian product

between 3 vectors 'acgt'.

between 3 vectors 'acgt'.

In python there is a function to do that in ``itertools module``: `https://docs.python.org/3/library/itertools.html#itertools.product <product>`_

In python there is a function to do that in ``itertools module``: `https://docs.python.org/3/library/itertools.html#itertools.product <product>`_

...

@@ -220,14 +237,14 @@ In python there is a function to do that in ``itertools module``: `https://docs.

...

@@ -220,14 +237,14 @@ In python there is a function to do that in ``itertools module``: `https://docs.