# Quelques examples simples#

## Case 1#

Que calcule le programme suivant ?

```res = [[1]]
for i in range(1, 10):
row = [1]
for j in range(1, i):
n = sum(res[-1][j - 1: j + 1])
row.append(n)
row.append(1)
res.append(row)

print(res)
```
```[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
```

Un autre affichage peut peut-être aider.

```import pprint
pprint.pprint(res)
```
```[[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
```

## Case 2#

```import cProfile

nombres = [9, 7, 5, 4, 6, 7, 3, 1, 7, 8]

def moyenne(ens):
return sum(ens) / len(ens)

def ecarttype(ens):
var = [(n - moyenne(ens)) ** 2 for n in ens]
return (sum(var) / len(var)) ** 0.5

print(moyenne(nombres))
print(ecarttype(nombres))

with cProfile.Profile() as pr:
for n in range(100000):
ecarttype(nombres)

pr.print_stats()
```
```5.7
2.3259406699226015
3400002 function calls in 5.596 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1    0.000    0.000    0.000    0.000 cProfile.py:117(__exit__)
1000000    1.412    0.000    2.829    0.000 plot_simple_example.py:47(moyenne)
100000    0.547    0.000    5.596    0.000 plot_simple_example.py:51(ecarttype)
100000    2.028    0.000    4.857    0.000 plot_simple_example.py:52(<listcomp>)
1100000    0.395    0.000    0.395    0.000 {built-in method builtins.len}
1100000    1.214    0.000    1.214    0.000 {built-in method builtins.sum}
1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
```

## Case 3#

```def bizarre(ensemble):
# premier zero
ensemble.append(0)
for i in range(len(ensemble)):
if ensemble[i] == 0:
return i

res = [1, 4, 5]
print("bizarre=", bizarre(res))
print("res=", res)
```
```bizarre= 3
res= [1, 4, 5, 0]
```

Total running time of the script: ( 0 minutes 5.952 seconds)

Gallery generated by Sphinx-Gallery