Commit 1aca6568 authored by paugier's avatar paugier
Browse files

Again basic

parent 1119ed37
Pipeline #74995 passed with stage
in 45 seconds
......@@ -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]
result0 = sum(numbers) / len(numbers)
avg0 = sum(numbers) / len(numbers)
result1 = 0
tmp = 0
i = 0
while i < len(numbers):
result1 += numbers[i]
tmp += numbers[i]
i = i + 1
result1 /= i
avg1 = tmp/ len(numbers)
assert result0 == result1
assert avg0 == avg1
```
%% 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
......
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