8000 gh-107017: Rework the Fibonacci example by TommyUnreal · Pull Request #107132 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-107017: Rework the Fibonacci example #107132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Draft. Rework ´the Fibonacci example.
Rework the example to be simple to follow and also the concepts needed
for that are now explained one by one with their own shorter examples.
  • Loading branch information
TommyUnreal committed Jul 23, 2023
commit 2383c73c9a9fdd234188e706fc6007000dfed0e2
141 changes: 85 additions & 56 deletions Doc/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -478,74 +478,103 @@ example::
First Steps Towards Programming
===============================

Of course, we can use Python for more complicated tasks than adding two and two
together. For instance, we can write an initial sub-sequence of the
We can use Python for more complex tasks than adding two and two together.
For instance, we can compute some of initial numbers of the
`Fibonacci series <https://en.wikipedia.org/wiki/Fibonacci_number>`_
as follows::

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while a < 10:
... print(a)
... a, b = b, a+b
...
To do that we need to use four more concepts.¨

Multiple Assignment
-------------------

A *multiple assignment* of variables allows to set values of multiple
variables on a single line. Notice that if the value assignment is a
expression, it is first evaluated and only after that it is assigned.::

>>> a, b = 1, 5 # multiple assignment of two variables
>>> a
0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be 1
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a doctest bug?

1
>>> b
5
>>> a, b = b, a + b # first a is set to value of b, then b is set to sum of a and b
>>> a
5
>>> b
6

Repeating our code
------------------

The :keyword:`while` loop is example of a loop. It repeats a block of code
as long as the condition (in example: ``a < 10``) remains true. The test
used in the example is a simple comparison you might know from arithmetics.
In Python, ``<`` stands for less than, ``>`` greater than, ``==``
equal to, ``<=`` less than or equal to, ``>=`` greater than or equal to
and ``!=`` not equal to.::

>>> count = 0; # define variable to which we will be adding 1 in a loop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> count = 0; # define variable to which we will be adding 1 in a loop
>>> count = 0 # define variable to which we will be adding 1 in a loop

>>> while count < 5: count = count + 1 # hit enter one more time to start the loop
...
>>> count # when count reached value of 5, while loop finished
5

Note that block inside the while loop, or *body* of the loop is *indented*.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using while, to draw attention to it.

Indentation is Python's way of grouping statements. At the interactive
prompt, you have to type a tab or space(s) for each indented line.
In practice you will prepare more complex Python code with a text editor.
Suitable text editors usually have an auto-indent functionality. When
we want to exit the indented block of code in the Python shell, we must
follow it by a blank line to indicate completion (since the parser cannot
guess when you have typed the last line). Note that each line within a basic
block must be indented by the same amount.::

>>> number = 1
>>> while number < 5:
... print(number)
... number = number + 1
...
1
2
3
5
8

This example introduces several new features.

* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
simultaneously get the new values 0 and 1. On the last line this is used again,
demonstrating that the expressions on the right-hand side are all evaluated
first before any of the assignments take place. The right-hand side expressions
are evaluated from the left to the right.

* The :keyword:`while` loop executes as long as the condition (here: ``a < 10``)
remains true. In Python, like in C, any non-zero integer value is true; zero is
false. The condition may also be a string or list value, in fact any sequence;
anything with a non-zero length is true, empty sequences are false. The test
used in the example is a simple comparison. The standard comparison operators
are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
(equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
and ``!=`` (not equal to).

* The *body* of the loop is *indented*: indentation is Python's way of grouping
statements. At the interactive prompt, you have to type a tab or space(s) for
each indented line. In practice you will prepare more complicated input
for Python with a text editor; all decent text editors have an auto-indent
facility. When a compound statement is entered interactively, it must be
followed by a blank line to indicate completion (since the parser cannot
guess when you have typed the last line). Note that each line within a basic
block must be indented by the same amount.

* The :func:`print` function writes the value of the argument(s) it is given.
It differs from just writing the expression you want to write (as we did
earlier in the calculator examples) in the way it handles multiple arguments,
floating point quantities, and strings. Strings are printed without quotes,
and a space is inserted between items, so you can format things nicely, like
this::

>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536

The keyword argument *end* can be used to avoid the newline after the output,
or end the output with a different string::
4

Function argumets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here

-----------------

We already know :func:`print` function, that writes the value of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already know the print function ?

argument(s) it is given on screen. The arguments are specified inside
parentheses ``()``. In simplest form, f.e. ``print(a, b)`` the arguments
are positional. That means function process them in same order we wrote
them.::

>>> group = "Knights"
>>> say_what = '"Ni!"'
>>> print(group, 'Who Say', say_what)
Knights Who Say "Ni!"

The keyword argument *end* can be used to avoid the newline after the output,
or end the output with a different string::

>>> count = 0
>>> count = 10
>>> while count > 5:
... print(count, end=' ')
... count = count - 1
...
10 9 8 7 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an elegant touch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you :)


Fibonacci series
----------------

We now know all we need to write code that will print all numbers from the
Fibonacci series! Let's check what nubers in it are lower than 1000.

>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... print(a, end=', ')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,


.. rubric:: Footnotes

.. [#] Since ``**`` has higher precedence than ``-``, ``-3**2`` will be
Expand Down
0