Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c26a34

Browse files
authored
bpo-31757: Make Fibonacci examples consistent (#3991)
1 parent 073150d commit 8c26a34

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

Doc/tutorial/introduction.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,18 @@ First Steps Towards Programming
458458
===============================
459459

460460
Of course, we can use Python for more complicated tasks than adding two and two
461-
together. For instance, we can write an initial sub-sequence of the *Fibonacci*
462-
series as follows::
461+
together. For instance, we can write an initial sub-sequence of the
462+
`Fibonacci series <https://en.wikipedia.org/wiki/Fibonacci_number>`_
463+
as follows::
463464

464465
>>> # Fibonacci series:
465466
... # the sum of two elements defines the next
466467
... a, b = 0, 1
467-
>>> while b < 10:
468-
... print(b)
468+
>>> while a < 10:
469+
... print(a)
469470
... a, b = b, a+b
470471
...
472+
0
471473
1
472474
1
473475
2
@@ -483,7 +485,7 @@ This example introduces several new features.
483485
first before any of the assignments take place. The right-hand side expressions
484486
are evaluated from the left to the right.
485487

486-
* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
488+
* The :keyword:`while` loop executes as long as the condition (here: ``a < 10``)
487489
remains true. In Python, like in C, any non-zero integer value is true; zero is
488490
false. The condition may also be a string or list value, in fact any sequence;
489491
anything with a non-zero length is true, empty sequences are false. The test
@@ -516,11 +518,11 @@ This example introduces several new features.
516518
or end the output with a different string::
517519

518520
>>> a, b = 0, 1
519-
>>> while b < 1000:
520-
... print(b, end=',')
521+
>>> while a < 1000:
522+
... print(a, end=',')
521523
... a, b = b, a+b
522524
...
523-
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
525+
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
524526

525527

526528
.. rubric:: Footnotes

Doc/tutorial/modules.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ called :file:`fibo.py` in the current directory with the following contents::
2929

3030
def fib(n): # write Fibonacci series up to n
3131
a, b = 0, 1
32-
while b < n:
33-
print(b, end=' ')
32+
while a < n:
33+
print(a, end=' ')
3434
a, b = b, a+b
3535
print()
3636

3737
def fib2(n): # return Fibonacci series up to n
3838
result = []
3939
a, b = 0, 1
40-
while b < n:
41-
result.append(b)
40+
while a < n:
41+
result.append(a)
4242
a, b = b, a+b
4343
return result
4444

@@ -52,17 +52,17 @@ the current symbol table; it only enters the module name ``fibo`` there. Using
5252
the module name you can access the functions::
5353

5454
>>> fibo.fib(1000)
55-
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
55+
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
5656
>>> fibo.fib2(100)
57-
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
57+
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
5858
>>> fibo.__name__
5959
'fibo'
6060

6161
If you intend to use a function often you can assign it to a local name::
6262

6363
>>> fib = fibo.fib
6464
>>> fib(500)
65-
1 1 2 3 5 8 13 21 34 55 89 144 233 377
65+
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
6666

6767

6868
.. _tut-moremodules:
@@ -92,7 +92,7 @@ module directly into the importing module's symbol table. For example::
9292

9393
>>> from fibo import fib, fib2
9494
>>> fib(500)
95-
1 1 2 3 5 8 13 21 34 55 89 144 233 377
95+
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
9696

9797
This does not introduce the module name from which the imports are taken in the
9898
local symbol table (so in the example, ``fibo`` is not defined).
@@ -101,7 +101,7 @@ There is even a variant to import all names that a module defines::
101101

102102
>>> from fibo import *
103103
>>> fib(500)
104-
1 1 2 3 5 8 13 21 34 55 89 144 233 377
104+
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
105105

106106
This imports all names except those beginning with an underscore (``_``).
107107
In most cases Python programmers do not use this facility since it introduces
@@ -145,7 +145,7 @@ executed as the "main" file:
145145
.. code-block:: shell-session
146146
147147
$ python fibo.py 50
148-
1 1 2 3 5 8 13 21 34
148+
0 1 1 2 3 5 8 13 21 34
149149
150150
If the module is imported, the code is not run::
151151

0 commit comments

Comments
 (0)