diff --git a/source/Collection_Data_Types.rst b/source/Collection_Data_Types.rst index a332d9756d9c1c55932ba557479ca0e217c1fc89..0f98ee2508c417caa98fa4961adfc9b1ffa78367 100644 --- a/source/Collection_Data_Types.rst +++ b/source/Collection_Data_Types.rst @@ -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.