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

Skip to content

Commit f058d2d

Browse files
committed
Issue python#7369: Fibonacci series should start at 0 in tutorial example.
1 parent e13dc3e commit f058d2d

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Doc/tutorial/controlflow.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ boundary::
194194
>>> def fib(n): # write Fibonacci series up to n
195195
... """Print a Fibonacci series up to n."""
196196
... a, b = 0, 1
197-
... while b < n:
198-
... print b,
197+
... while a < n:
198+
... print a,
199199
... a, b = b, a+b
200200
...
201201
>>> # Now call the function we just defined:
202202
... fib(2000)
203-
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
203+
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
204204

205205
.. index::
206206
single: documentation strings
@@ -244,7 +244,7 @@ mechanism::
244244
<function fib at 10042ed0>
245245
>>> f = fib
246246
>>> f(100)
247-
1 1 2 3 5 8 13 21 34 55 89
247+
0 1 1 2 3 5 8 13 21 34 55 89
248248

249249
Coming from other languages, you might object that ``fib`` is not a function but
250250
a procedure since it doesn't return a value. In fact, even functions without a
@@ -264,22 +264,22 @@ Fibonacci series, instead of printing it::
264264
... """Return a list containing the Fibonacci series up to n."""
265265
... result = []
266266
... a, b = 0, 1
267-
... while b < n:
268-
... result.append(b) # see below
267+
... while a < n:
268+
... result.append(a) # see below
269269
... a, b = b, a+b
270270
... return result
271271
...
272272
>>> f100 = fib2(100) # call it
273273
>>> f100 # write the result
274-
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
274+
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
275275

276276
This example, as usual, demonstrates some new Python features:
277277

278278
* The :keyword:`return` statement returns with a value from a function.
279279
:keyword:`return` without an expression argument returns ``None``. Falling off
280280
the end of a function also returns ``None``.
281281

282-
* The statement ``result.append(b)`` calls a *method* of the list object
282+
* The statement ``result.append(a)`` calls a *method* of the list object
283283
``result``. A method is a function that 'belongs' to an object and is named
284284
``obj.methodname``, where ``obj`` is some object (this may be an expression),
285285
and ``methodname`` is the name of a method that is defined by the object's type.
@@ -288,7 +288,7 @@ This example, as usual, demonstrates some new Python features:
288288
object types and methods, using *classes*, see :ref:`tut-classes`)
289289
The method :meth:`append` shown in the example is defined for list objects; it
290290
adds a new element at the end of the list. In this example it is equivalent to
291-
``result = result + [b]``, but more efficient.
291+
``result = result + [a]``, but more efficient.
292292

293293

294294
.. _tut-defining:

0 commit comments

Comments
 (0)