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
meige-legi
scientific-computing-m2-efm
Commits
1aca6568
Commit
1aca6568
authored
Sep 16, 2021
by
paugier
Browse files
Again basic
parent
1119ed37
Pipeline
#74995
passed with stage
in 45 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
ipynb/020_basic_statements.ipynb
View file @
1aca6568
...
...
@@ -921,11 +921,12 @@
%% Cell type:markdown id: tags:
- Extract the list of items (i.e. "wind", "temperature", "pressure"; see `str.split`).
- Add "Snow level" to the list of items (see `list.append`)
- Build a new header such that items are capitalized (with `str.join` and `str.capitalize` and iterating on the list)
- Capitalize all elements of the list (with `str.capitalize` and iterating on the list)
- Build a new header such that items are capitalized (with `str.join`)
%% Cell type:markdown id: tags:
#### A possible solution:
...
...
@@ -1092,12 +1093,14 @@
### Do it yourself
- Edit a script with Spyder that calculates the average of a set of numbers. For example `numbers = [67, 12, 2, 9, 23, 5]`
* using the functions `sum` and `len`
* manually, using the keyword `while`
* check that the 2 methods give the same results with `assert result0 == result1`
* manually (without `sum`), using the keyword `while`
* check that the 2 methods give the same results with
```assert avg0 == avg1```
- Run the script
* in Spyder,
...
...
@@ -1111,20 +1114,20 @@
%% Cell type:code id: tags:
```
python
numbers = [67, 12, 2, 9, 23, 5]
result
0 = sum(numbers) / len(numbers)
avg
0 = sum(numbers) / len(numbers)
result1
= 0
tmp
= 0
i = 0
while i < len(numbers):
result1
+= numbers[i]
tmp
+= numbers[i]
i = i + 1
result
1
/
=
i
avg
1 =
tmp/ len(numbers)
assert
result0 == result
1
assert
avg0 == avg
1
```
%% Cell type:markdown id: tags:
### Loops with the keyword `for`
...
...
@@ -1175,11 +1178,11 @@
%% Cell type:markdown id: tags:
### Do it yourself
-
Simplify
your script
by using a `for` loops
.
-
Extend
your script
with another method (using a `for` loop) to compute the average
.
- In IPython, try to understand how the function `enumerate` works. Use it in your script.
%% Cell type:markdown id: tags:
...
...
@@ -1188,15 +1191,15 @@
%% Cell type:code id: tags:
```
python
l = [67, 12, 2, 9, 23, 5]
result
= 0
avg2
= 0
for i, e in enumerate(l):
result
+= e
result
/= i + 1
assert
result
== sum(l)/len(l)
avg2
+= e
avg2
/= i + 1
assert
avg2
== sum(l)/len(l)
```
%% Cell type:markdown id: tags:
### Do it yourself
...
...
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