cycle for incrementa una variabile con delle sequenze in looping. Nel senso che cambia il valore di una variabile in un ciclo vediamo un esempio:
#!/usr/bin/python
for num in range ( 0,10 ) :
print num
Output:
0
1
2
3
4
5
6
7
8
9
Che succede?? for a in range serve a cambiare il valore della variabile num in sequenza che va da 0 a 10. range serve proprio a questo for ... in range(inizio, fine) , se volessimo aggiungere uno step (una specie di salto nel conteggio) basta aggiungere un terzo elemento a range(0,10,2) Proviamo:
#!/usr/bin/python
for num in range ( 0 , 10 , 2 ) : #2 e' lo step
print num
As you can see the sky missed the second sequence of numbers printed on the screen 0,2,4,6,8.
Other uttilizzi of for with strings look in the example:
What's up? will simply output the string "H ello W orld" the cycle takes its value from the first point until the 'last.
for can also use variables such as: for a in b where b is a string maybe.
* * REMEMBER TO
If you are using for in (0.10) without printing on the screen range 0 10 range just need to get a count from the first value in the second .
The comma used in the function print () (eg: print a,) is not to go head adds more space.
# / usr / bin / python
for num in ( " H " , " and " , " the " , " the " , " or ", " , " W " , " or " , " r " , " the " , " d) :
print num, # not the comma is used to wrap
What's up? will simply output the string "H ello W orld" the cycle takes its value from the first point until the 'last.
for can also use variables such as: for a in b where b is a string maybe.
* * REMEMBER TO
If you are using for in (0.10) without printing on the screen range 0 10 range just need to get a count from the first value in the second .
The comma used in the function print () (eg: print a,) is not to go head adds more space.
0 comments:
Post a Comment