Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python_one_week_4_biologists_solutions
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hub-courses
python_one_week_4_biologists_solutions
Commits
81af9a1f
Commit
81af9a1f
authored
4 years ago
by
Blaise Li
Browse files
Options
Downloads
Patches
Plain Diff
Hide Python 2 comment.
parent
4d17374f
No related branches found
No related tags found
No related merge requests found
Pipeline
#58080
passed
4 years ago
Stage: build
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
source/Collection_Data_Types.rst
+21
-11
21 additions, 11 deletions
source/Collection_Data_Types.rst
with
21 additions
and
11 deletions
source/Collection_Data_Types.rst
+
21
−
11
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.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment