Programmazione in
Python
dario.pescini@unimib.it 1 / 24
Strutture di controllo
Outline
1 Strutture di controllo
2 / 24
Strutture di controllo
Strutture di controllo
Sequenza
if
Selezione if ... else
if ... elif ... else
Iterazione while
for
3 / 24
Strutture di controllo
Strutture di controllo
Sequenza
if
Selezione if ... else
if ... elif ... else
Iterazione while
for
3 / 24
Strutture di controllo
while condizione :
· · · ·lista comandi
4 / 24
Strutture di controllo
y=0.0
x=0.0
print ”y = ”, y, ”\tx = ”, x
while y <= 10 :
y = 0.5 * x + 2
print ”y = ”, y, ”\tx = ”, x
x = x + 1.65
5 / 24
Strutture di controllo
preverifica
Nel ciclo while la condizione viene verificata sempre prima di
eseguire il blocco di istruzioni.
6 / 24
Strutture di controllo
passo x y
y=0.0
x=0.0
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
y=0.0
x=0.0
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
7 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
8 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
8 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
8 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
8 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
9 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2 …
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65 45 16.5 10.25
9 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2 …
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65 45 16.5 10.25
9 / 24
Strutture di controllo
passo x y
1 – 0
2 0 0
y=0.0
x=0.0 5 0 2
7 1.65 2
print(”y = ”, y, ”\tx = ”, x)
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2 …
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65 45 16.5 10.25
47 18.25 10.25
9 / 24
Strutture di controllo
passo x y
1 – 0
y=0.0 2 0 0
x=0.0 5 0 2
print(”y = ”, y, ”\tx = ”, x)
7 1.65 2
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x) …
x = x + 1.65 45 16.5 10.25
47 18.25 10.25
10 / 24
Strutture di controllo
passo x y
1 – 0
y=0.0 2 0 0
x=0.0 5 0 2
print(”y = ”, y, ”\tx = ”, x)
7 1.65 2
9 1.65 2.825
while y <= 10 : 11 3.3 2.825
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x) …
x = x + 1.65 45 16.5 10.25
47 18.25 10.25
10 / 24
Strutture di controllo
y=0.0
x=0.0
print(”y = ”, y, ”\tx = ”, x)
while y <= 10 :
y = 0.5 * x + 2
print(”y = ”, y, ”\tx = ”, x)
x = x + 1.65
11 / 24
Strutture di controllo
Trace table
Per costruirla:
1 si identificano tutte le variabili dichiarate
2 si aggiunge una colonna per ogni variabile identificata
3 si aggiunge una riga ad ogni variazione del contenuto di
una delle variabili
4 si modifica il valore nella cella della tabella associato alla
variabile modificata
5 si riporta il valore delle altre variabili non modificate
12 / 24
Strutture di controllo
Esempio
temp = a;
a = b;
b = temp;
print(”a = ”,a, ”b = ”, b)
13 / 24
Strutture di controllo
Esempio
temp = a;
a = b;
b = temp;
print(”a = ”,a, ”b = ”, b)
13 / 24
Strutture di controllo
Esempio
print(”a = ”,a, ”b = ”, b)
13 / 24
Strutture di controllo
Esempio
print(”a = ”,a, ”b = ”, b)
13 / 24
Strutture di controllo
Esempio
print(”a = ”,a, ”b = ”, b)
13 / 24
Strutture di controllo
Esempio
print(”a = ”,a, ”b = ”, b)
13 / 24
Strutture di controllo
Esempio
13 / 24
Strutture di controllo
Esempio
13 / 24
Strutture di controllo
Esempio:
x = True
while x == True:
print(’iterazione’)
14 / 24
Strutture di controllo
Esercizio
15 / 24
Strutture di controllo
Esercizio
numero = 0
while numero >= 0 :
numero = eval(input(”inserire un numero positivo: ”))
16 / 24
Strutture di controllo
WHILE: contatori
stringa = ’si’
contatore = 0
17 / 24
Strutture di controllo
WHILE: contatori
contatore = 0
while contatore < numIterazioni:
print(’Hai eseguito ’, contatore, ’ iterazioni’)
contatore = contatore + 1
18 / 24
Strutture di controllo
operatori di incremento
19 / 24
Strutture di controllo
numIterazioni = 5
media = 0
contatore = 0
while contatore < numIterazioni:
valore = float(input(’inserisci un numero ’))
media += valore
contatore += 1
media /= numIterazioni
print(’il valor medio è ’, media)
20 / 24
Strutture di controllo
media = 0
valore = 0
iterazione = −1
while valore >= 0:
media += valore
iterazione +=1
valore = float(input(’inserisci un numero positivo (<0
termina esecuzione)’))
media /= iterazione
print(’il valor medio è ’, media)
21 / 24
Strutture di controllo
22 / 24
Strutture di controllo
23 / 24
Strutture di controllo
Algoritmo
24 / 24