Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente |
python:les_boucles [2019/10/13 15:12] – [While] physix | python:les_boucles [2020/07/24 03:31] (Version actuelle) – modification externe 127.0.0.1 |
---|
===== While ===== | ===== While ===== |
| |
<code> | <code python> |
i = 0 | i = 0 |
while i <= 10 : | while i <= 10 : |
</code> | </code> |
| |
{{url>http://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=35&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false}} | {{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}} |
| |
| |
===== ===== | ===== ===== |
| |
<code> | <code python> |
s = "Hello world" | s = "Hello world" |
| |
donne : | donne : |
| |
<file>>>> | <file> |
| >>> |
H | H |
e | e |
==== For avec Range ==== | ==== For avec Range ==== |
| |
===== ===== | <code python> |
| |
<code> | |
for i in range(0,10) : | for i in range(0,10) : |
print(i) | print(i) |
| |
</code> | </code> |
| |
7 | 7 |
8 | 8 |
9>>> | 9 |
</file> | </file> |
| |
| <code python> |
| for i in range(2,10,3): |
| print(i) |
| |
| </code> |
| |
| {{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}} |
| |
| <code python> |
| 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) |
| |
| </code> |
| |
| {{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}} |
| |
| <code python> |
| 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) |
| |
| </code> |
| |
| {{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}} |
| |
| <code python> |
| x = [0., 0.1, 0.2, 0.3, 0.4, 0.5] |
| y = [ i ** 2 + 4. for i in x] |
| |
| print(y) |
| |
| </code> |
| |
| {{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 avec Break ==== |
| |
<code> | <code python> |
for i in range(0,10) : | for i in range(0,10) : |
print(i) | print(i) |
donne : | donne : |
| |
<file>>>> | <file> |
| >>> |
0 | 0 |
1 | 1 |