Skip to content
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

gh-107017: Rework the Fibonacci example. #107132

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

TommyUnreal
Copy link
Contributor

@TommyUnreal TommyUnreal commented Jul 23, 2023

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.

Needs proofread. Please comment with your ideas for improvement.


📚 Documentation preview 📚: https://cpython-previews--107132.org.readthedocs.build/

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.
@TommyUnreal TommyUnreal marked this pull request as ready for review July 23, 2023 21:53

A *multiple assignment* of variables us allows to set values of more than one
variable on a single line. Notice that if the value assignment is a
expression, it is first evaluated and then assigned.::
Copy link
Contributor

Choose a reason for hiding this comment

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

.:: will produce a full-stop and a colon:

.:

One or the other. I suggest losing the full-stop.

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

"in example" > "for example"

Copy link
Contributor Author

@TommyUnreal TommyUnreal Jul 29, 2023

Choose a reason for hiding this comment

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

What I wanted to say is that "in the example below the condition is the following expression a < 10", but that is no longer present in the examples. I will rework that.

>>> 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.

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
Copy link
Contributor

Choose a reason for hiding this comment

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

"... the parser"

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).::
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, here we have a full-stop and a colon in the output.

... print(count, end=' ') # end the output of print() with a whitespace
... count = count - 1 # lower the value by one with each repetition
...
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 :)

... print('', a, '', end='->')
... a, b = b, a+b
...
0 -> 1 -> 1 -> 2 -> 3 -> 5 -> 8 -> 13 -> 21 -> 34 -> 55 -> 89 -> 144 ->
Copy link
Contributor

Choose a reason for hiding this comment

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

And I like the way that the example illustrates the value of using end - no need to explain, the example does it much more effectively. This will stick in someone's mind.

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.

>>> a, b = 0, 1
>>> while a < 150:
... print('', a, '', end='->')

Choose a reason for hiding this comment

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

I understand that this utilities the print sep param, however this is nor explained nor inferred, so to a beginner it may be confusing

Perhaps it could be beneficial to show off f-strings like

Suggested change
... print('', a, '', end='->')
... print(f'{a} ->', end='')

Or to keep end as ->

Suggested change
... print('', a, '', end='->')
... print(f'{a} ', end="->")

Copy link
Contributor

Choose a reason for hiding this comment

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

I very strongly prefer the original print('', a, '', end='->'), which reduces the punctuation the learner has to deal with to a minimum, and emphasises instead the connection between more easily assimilated things, like the English word "end" and the "arrow" ("->").

Showing off f-strings while demonstrating a different functionality introduces unnecessary cognitive burden. Two different kinds of quotes and two different kinds of brackets requires a lot of parsing and stopping. I don't think this is the right solution here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My reasoning: As this is an Introduction chapter, I wanted to keep the number of introduced concepts to the minimum. There are ways to code this better, but it is not formally wrong. The default separator is already shown in the example above and this just builds on that, so it's hopefully not confusing to the reader.

Doc/tutorial/introduction.rst Outdated Show resolved Hide resolved
>>> count = 10
>>> while count > 5:
... print(count, end=' ') # end the output of print() with a whitespace
... count = count - 1 # lower the value by one with each repetition

Choose a reason for hiding this comment

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

Suggested change
... count = count - 1 # lower the value by one with each repetition
... count = count - 1 # decrement the value each iteration

Copy link
Contributor

Choose a reason for hiding this comment

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

I would strongly prefer "decrease the value by one".

The reason is that many people will not be familiar with the word "decrement", it's used very little in English outside technical contexts.

expression, it is first evaluated and then 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?

Doc/tutorial/introduction.rst Outdated Show resolved Hide resolved
Doc/tutorial/introduction.rst Outdated Show resolved Hide resolved
TommyUnreal and others added 3 commits July 29, 2023 09:52
Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
More precise and clearer wording.

Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
Doc/tutorial/introduction.rst Outdated Show resolved Hide resolved
>>> count = 10
>>> while count > 5:
... print(count, end=' ') # end the output of print() with a whitespace
... count = count - 1 # lower the value by one with each repetition
Copy link
Contributor

Choose a reason for hiding this comment

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

I would strongly prefer "decrease the value by one".

The reason is that many people will not be familiar with the word "decrement", it's used very little in English outside technical contexts.


>>> a, b = 0, 1
>>> while a < 150:
... print('', a, '', end='->')
Copy link
Contributor

Choose a reason for hiding this comment

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

I very strongly prefer the original print('', a, '', end='->'), which reduces the punctuation the learner has to deal with to a minimum, and emphasises instead the connection between more easily assimilated things, like the English word "end" and the "arrow" ("->").

Showing off f-strings while demonstrating a different functionality introduces unnecessary cognitive burden. Two different kinds of quotes and two different kinds of brackets requires a lot of parsing and stopping. I don't think this is the right solution here.

Copy link

@CaedenPH CaedenPH left a comment

Choose a reason for hiding this comment

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

Other than these grammatical mistakes, looks like a good change to me

Doc/tutorial/introduction.rst Outdated Show resolved Hide resolved
Doc/tutorial/introduction.rst Outdated Show resolved Hide resolved
TommyUnreal and others added 2 commits July 29, 2023 12:10
Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
lower the value -> decrease the value
Copy link
Contributor

@SylvainDe SylvainDe left a comment

Choose a reason for hiding this comment

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

Very nice touch! Here are 2 cosmetic details

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
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

Function argumets
-----------------

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 ?

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

A suggestion and some little wording things, otherwise looks good!

We can use Python for more complex tasks than adding two and two together.
For instance, we can compute the numbers of the
`Fibonacci series <https://en.wikipedia.org/wiki/Fibonacci_number>`_.
To do that we need to utilize three following concepts.
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
To do that we need to utilize three following concepts.
To do that we need to use the three following concepts.

Let's use the plain English "use" instead of "utilize".

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 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

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

A *multiple assignment* of variables us allows to set 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

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

The :keyword:`while` loop is example of a repeating cycle. It performs
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
The :keyword:`while` loop is example of a repeating cycle. It performs
The :keyword:`while` loop is an example of a repeating cycle. It performs

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
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
of count. As soon as the variable ``count`` reaches the value 5, the
of ``count``. As soon as the variable ``count`` reaches the value 5, the

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
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

Comment on lines +537 to +542
>>> 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
Member

Choose a reason for hiding this comment

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

The example above uses a one-liner: while count < 5: count = count + 1

Let's use indentation in the example, to match the text. Something like this?

Suggested change
>>> 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*.
>>> 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*.

Comment on lines +565 to +566
parentheses ``()``. In simplest form, like ``print(a, b)``, the arguments
are positional, meaning the function processes them in the same order
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
parentheses ``()``. In simplest form, like ``print(a, b)``, the arguments
are positional, meaning the function processes them in the same order
parentheses ``()``. In its simplest form, like ``print(a, b)``, the arguments
are positional, meaning the function processes them in the same order

@hauntsaninja
Copy link
Contributor

Thanks for working on this! As compared to the version on the left hand side, I found presenting all the concepts up front in detailed sections a little intimidating. I preferred seeing the four lines we're building towards first, and then an explanation of what's going on.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes and removed needs backport to 3.11 only security fixes labels May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review docs Documentation in the Doc dir needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants