Python - jednoduchý cyklus while: Porovnání verzí
mBez shrnutí editace |
m sablona python |
||
(Není zobrazeno 9 mezilehlých verzí od 2 dalších uživatelů.) | |||
Řádek 1: | Řádek 1: | ||
Ukázka použití jednoduchých cyklů | Ukázka použití jednoduchých cyklů | ||
<source lang="python"> | |||
#!/usr/bin/python | #!/usr/bin/python | ||
# Fibonacciho posloupnost | # Fibonacciho posloupnost | ||
a = b = 1 | a = b = 1 | ||
print b | print "%d\n" % b | ||
while a < 100: | while a < 100: | ||
print a | print a, | ||
a = a + b | a = a + b | ||
b = a - b | b = a - b | ||
</source> | |||
1 | |||
1 2 3 5 8 13 21 34 55 89 | |||
<source lang="python"> | |||
#!/usr/bin/python | #!/usr/bin/python | ||
import | from math import sin | ||
s = c = x = 0.4 | s = c = x = 0.4 | ||
n = 1 | x2 = x * x | ||
n = 1 | |||
while abs(c) > 1e-12: | while abs(c) > 1e-12: | ||
c = -c* | c = -c * x2 / (n + 1) / (n + 2) | ||
n = n + 2 | n = n + 2 | ||
s = s + c | s = s + c | ||
print s | print "%g\t%g\t%g" % (x, s, sin (x)) | ||
</source> | |||
0.4 0.389418 0.389418 | |||
[ [[Python|Zpět]] ] | |||
{{Python}} |
Aktuální verze z 20. 4. 2008, 19:38
Ukázka použití jednoduchých cyklů
#!/usr/bin/python
# Fibonacciho posloupnost
a = b = 1
print "%d\n" % b
while a < 100:
print a,
a = a + b
b = a - b
1 1 2 3 5 8 13 21 34 55 89
#!/usr/bin/python
from math import sin
s = c = x = 0.4
x2 = x * x
n = 1
while abs(c) > 1e-12:
c = -c * x2 / (n + 1) / (n + 2)
n = n + 2
s = s + c
print "%g\t%g\t%g" % (x, s, sin (x))
0.4 0.389418 0.389418
[ Zpět ]