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
Prev Previous commit
Next Next commit
Fix review hints. Explain condition better.
Fix parts where a paragraph ended both with full-stop and colon.
Added new paragraph, where example of simple loop is explained in
detail. Plus warning about indefinite loops.
  • Loading branch information
TommyUnreal committed Jul 29, 2023
commit e87530d17a7536cca551789c2cd42a3621956b13
27 changes: 16 additions & 11 deletions Doc/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ Lists
Python knows a number of *compound* data types, used to group together other
values. The most versatile is the *list*, which can be written as a list of
comma-separated values (items) between square brackets. Lists might contain
items of different types, but usually the items all have the same type. ::
items of different types, but usually the items all have the same type::

>>> squares = [1, 4, 9, 16, 25]
>>> squares
Expand Down Expand Up @@ -488,7 +488,7 @@ Multiple Assignment

A *multiple assignment* of variables us allows to set values of more than one
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
A *multiple assignment* of variables us allows to set values of more than one
A *multiple assignment* of variables us allows to set the values of more than one

variable on a single line. Notice that if the value assignment is a
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
variable on a single line. Notice that if the value assignment is a
variable on a single line. Notice that if the value assignment is an

expression, it is first evaluated and then assigned.::
expression, it is first evaluated and then assigned::

>>> a, b = 1, 5 # multiple assignment of two variables
>>> a
Expand All @@ -505,10 +505,10 @@ Repeating our code
------------------

The :keyword:`while` loop is example of a repeating cycle. It performs
a block of code over as long as the condition (in example: ``a < 10``) is
a block of code over as long as the condition is
fulfilled. The test used in the example is a simple comparison you might
know from arithmetics. Fulfilled condition have the value True. Unfulfilled
condition have the value False.::
know from arithmetics. Fulfilled condition have the value ``True``.
Unfulfilled condition have the value ``False``::

>>> 1 < 3 # 1 is less than 3
True
Expand All @@ -526,21 +526,26 @@ In Python, the following comparison operators are used:
* ``>=``: Greater than or equal to
* ``!=``: Not equal to

::
Let's create a simple loop. We need the keyword ``while`` and a condition;
in this case, ``count < 5``. To ensure that the loop finishes, we must make
sure that the condition is `not fulfilled` at a certain step. Otherwise, the
code would repeat indefinitely. To achieve that, we can increase the value
of count. As soon as the variable count reaches the value 5, the condition
will be ``False``::

>>> count = 0; # define variable to which we will be adding 1 in a loop
D100 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*.
Note that block inside the `while` loop, or *body* of the loop is *indented*.
Indentation is Python's way of grouping statements together. When you use
the Python shell, you need to type a tab or space(s) for each indented line.
Each line within a block must be indented by the same amount.
When we want to exit the indented block of code in the Python shell, we must
follow it by a blank line to indicate its completion. This way, parser knows
we have finished typing the last line).::
follow it by a blank line to indicate its completion. This way, the parser
knows we have finished typing the last line)::

>>> number = 1
>>> while number < 5:
Expand All @@ -559,7 +564,7 @@ We already know :func:`print` function, that writes the value of the
argument(s) it receives on screen. The arguments are enclosed within
parentheses ``()``. In simplest form, like ``print(a, b)`` the arguments
are positional, meaning the function processes them in the same order
as they are written.::
as they are written::

>>> group = "Knights"
>>> say_what = '"Ni!"'
Expand All @@ -582,7 +587,7 @@ Fibonacci series
----------------

Great! Now that we have the knowledge, let's write code to print all numbers
from the Fibonacci series that are lower than 150.
from the Fibonacci series that are lower than 150::

>>> a, b = 0, 1
>>> while a < 150:
Expand Down
0