====== Les boucles ======
===== While =====
i = 0
while i <= 10 :
print(i)
i = i+1
donne :
>>>
0
1
2
3
4
5
6
7
8
9
10>>>
{{url>https://pythontutor.com/iframe-embed.html#code=i%20%3D%200%0Awhile%20i%20%3C%3D%2010%20%3A%0A%20%20%20%20print%28i%29%0A%20%20%20%20i%20%3D%20i%2B1&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false}}
===== For =====
===== =====
s = "Hello world"
for lettre in s :
print(lettre)
donne :
>>>
H
e
l
l
o
w
o
r
l
d>>>
==== For avec Range ====
for i in range(0,10) :
print(i)
donne
>>>
0
1
2
3
4
5
6
7
8
9
for i in range(2,10,3):
print(i)
{{url>https://pythontutor.com/iframe-embed.html#code=for%20i%20in%20range%282,10,3%29%3A%0A%20%20%20%20print%28i%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false}}
x = [0., 0.1, 0.2, 0.3, 0.4, 0.5]
y = [ ]
for i in range( len( x ) ): #i est un int, c'est un indice
y.append( x[ i ] ** 2 + 4. )
print(y)
{{url>https://pythontutor.com/iframe-embed.html#code=x%20%3D%20%5B0.,%200.1,%200.2,%200.3,%200.4,%200.5%5D%0Ay%20%3D%20%5B%20%5D%0A%0Afor%20i%20in%20range%28%20len%28%20x%20%29%20%29%3A%20%23i%20est%20un%20int,%20c'est%20un%20indice%0A%20%20%20%20y.append%28%20x%5B%20i%20%5D%20**%202%20%2B%204.%20%29%0A%20%20%20%20%0Aprint%28y%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false}}
x = [0., 0.1, 0.2, 0.3, 0.4, 0.5]
y = [ ]
for i in x: #i est un float, c'est un élément de x
y.append( i ** 2 + 4. )
print(y)
{{url>https://pythontutor.com/iframe-embed.html#code=x%20%3D%20%5B0.,%200.1,%200.2,%200.3,%200.4,%200.5%5D%0Ay%20%3D%20%5B%20%5D%0A%0Afor%20i%20in%20x%3A%20%20%23i%20est%20un%20float,%20c'est%20un%20%C3%A9l%C3%A9ment%20de%20x%0A%20%20%20%20y.append%28%20i%20**%202%20%2B%204.%20%29%0A%0Aprint%28y%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false}}
x = [0., 0.1, 0.2, 0.3, 0.4, 0.5]
y = [ i ** 2 + 4. for i in x]
print(y)
{{url>https://pythontutor.com/iframe-embed.html#code=x%20%3D%20%5B0.,%200.1,%200.2,%200.3,%200.4,%200.5%5D%0Ay%20%3D%20%5B%20i%20**%202%20%2B%204.%20for%20i%20in%20x%5D%0A%0Aprint%28y%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false}}
==== For avec Break ====
for i in range(0,10) :
print(i)
if i == 5 :
break
donne :
>>>
0
1
2
3
4
5>>>