Merged revisions 76460 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r76460 | mark.dickinson | 2009-11-23 16:39:05 +0000 (Mon, 23 Nov 2009) | 2 lines Issue #7369: Fibonacci series should start at 0 in tutorial example. ........
This commit is contained in:
parent
dfb1562410
commit
c099ee2761
|
@ -225,14 +225,14 @@ boundary::
|
||||||
>>> def fib(n): # write Fibonacci series up to n
|
>>> def fib(n): # write Fibonacci series up to n
|
||||||
... """Print a Fibonacci series up to n."""
|
... """Print a Fibonacci series up to n."""
|
||||||
... a, b = 0, 1
|
... a, b = 0, 1
|
||||||
... while b < n:
|
... while a < n:
|
||||||
... print(b, end=' ')
|
... print(a, end=' ')
|
||||||
... a, b = b, a+b
|
... a, b = b, a+b
|
||||||
... print()
|
... print()
|
||||||
...
|
...
|
||||||
>>> # Now call the function we just defined:
|
>>> # Now call the function we just defined:
|
||||||
... fib(2000)
|
... fib(2000)
|
||||||
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
|
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
single: documentation strings
|
single: documentation strings
|
||||||
|
@ -276,7 +276,7 @@ mechanism::
|
||||||
<function fib at 10042ed0>
|
<function fib at 10042ed0>
|
||||||
>>> f = fib
|
>>> f = fib
|
||||||
>>> f(100)
|
>>> f(100)
|
||||||
1 1 2 3 5 8 13 21 34 55 89
|
0 1 1 2 3 5 8 13 21 34 55 89
|
||||||
|
|
||||||
Coming from other languages, you might object that ``fib`` is not a function but
|
Coming from other languages, you might object that ``fib`` is not a function but
|
||||||
a procedure since it doesn't return a value. In fact, even functions without a
|
a procedure since it doesn't return a value. In fact, even functions without a
|
||||||
|
@ -296,14 +296,14 @@ Fibonacci series, instead of printing it::
|
||||||
... """Return a list containing the Fibonacci series up to n."""
|
... """Return a list containing the Fibonacci series up to n."""
|
||||||
... result = []
|
... result = []
|
||||||
... a, b = 0, 1
|
... a, b = 0, 1
|
||||||
... while b < n:
|
... while a < n:
|
||||||
... result.append(b) # see below
|
... result.append(a) # see below
|
||||||
... a, b = b, a+b
|
... a, b = b, a+b
|
||||||
... return result
|
... return result
|
||||||
...
|
...
|
||||||
>>> f100 = fib2(100) # call it
|
>>> f100 = fib2(100) # call it
|
||||||
>>> f100 # write the result
|
>>> f100 # write the result
|
||||||
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
|
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
|
||||||
|
|
||||||
This example, as usual, demonstrates some new Python features:
|
This example, as usual, demonstrates some new Python features:
|
||||||
|
|
||||||
|
@ -311,7 +311,7 @@ This example, as usual, demonstrates some new Python features:
|
||||||
:keyword:`return` without an expression argument returns ``None``. Falling off
|
:keyword:`return` without an expression argument returns ``None``. Falling off
|
||||||
the end of a function also returns ``None``.
|
the end of a function also returns ``None``.
|
||||||
|
|
||||||
* The statement ``result.append(b)`` calls a *method* of the list object
|
* The statement ``result.append(a)`` calls a *method* of the list object
|
||||||
``result``. A method is a function that 'belongs' to an object and is named
|
``result``. A method is a function that 'belongs' to an object and is named
|
||||||
``obj.methodname``, where ``obj`` is some object (this may be an expression),
|
``obj.methodname``, where ``obj`` is some object (this may be an expression),
|
||||||
and ``methodname`` is the name of a method that is defined by the object's type.
|
and ``methodname`` is the name of a method that is defined by the object's type.
|
||||||
|
@ -320,7 +320,7 @@ This example, as usual, demonstrates some new Python features:
|
||||||
object types and methods, using *classes*, see :ref:`tut-classes`)
|
object types and methods, using *classes*, see :ref:`tut-classes`)
|
||||||
The method :meth:`append` shown in the example is defined for list objects; it
|
The method :meth:`append` shown in the example is defined for list objects; it
|
||||||
adds a new element at the end of the list. In this example it is equivalent to
|
adds a new element at the end of the list. In this example it is equivalent to
|
||||||
``result = result + [b]``, but more efficient.
|
``result = result + [a]``, but more efficient.
|
||||||
|
|
||||||
|
|
||||||
.. _tut-defining:
|
.. _tut-defining:
|
||||||
|
|
Loading…
Reference in New Issue