Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hub-courses
python_one_week_4_biologists_solutions
Commits
81af9a1f
Commit
81af9a1f
authored
Jun 08, 2021
by
Blaise Li
Browse files
Hide Python 2 comment.
parent
4d17374f
Pipeline
#58080
passed with stages
in 12 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
source/Collection_Data_Types.rst
View file @
81af9a1f
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment