Commit 81af9a1f authored by Blaise Li's avatar Blaise Li
Browse files

Hide Python 2 comment.

parent 4d17374f
Pipeline #58080 passed with stages
in 12 seconds
......@@ -100,25 +100,35 @@ If we modify the state of the list itself, but not the references to this object
Exercise
--------
wihout using python shell, what is the results of the following statements:
.. note::
sum is a function which return the sum of each elements of a list.
``sum`` is a function which return 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[3] = -4 # what is the value of x now ?
y = sum(x)/len(x) #what is the value of y ? why ?
x[3] = -4 # What is the value of x now?
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
Here, we compute the mean of the values contained in the list ``x``, after having changed its last element to -4.
y = 0.5
.. warning::
.. .. warning::
In python2 the result is ::
.. In python2 the result is ::
y = 0
.. y = 0
because sum(x) is an integer, len(x) is also an integer so in python2.x the result is an integer,
.. 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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment