Skip to content

Commit 31a1e50

Browse files
committed
Rework discussion of loops & range to decouple from lists
Even in Python 2 more than just lists were iterable by a for loop, though in Python 3 this becomes even more pronounced as most of the builtins which previously returned lists now do something else. Ranges in particular are now an immutable class rather than a function returning a mutable list. Their documentation has consequently changed a lot and no longer looks like the quote we had, so replace that with our own simple explanation.
1 parent ecf18be commit 31a1e50

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

tutorials/basic_motor_control.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ Again, as you've seen most of that before, it shouldn't be too difficult to get
216216
The `for` loop may be new, however.
217217

218218
The [`for`](https://docs.python.org/tutorial/controlflow.html#for-statements)
219-
loop accepts a Python `list` (a `list`, when `print`ed, appears in square brackets like so: `[1, 2, 3]`).
219+
loop accepts anything that Python can iterate over to get multiple values, such as a `list` (a `list`, when `print`ed, appears in square brackets like so: `[1, 2, 3]`).
220+
220221
For a comprehensive introduction to to `list`s, have a look at [this WikiBooks article](https://en.wikibooks.org/wiki/Python_Programming/Lists).
221222
The `for` loop will iterate over the `list` (i.e. take each element in turn)
222223
and make it available in the loop's body as the variable after the the `for` keyword.
@@ -235,25 +236,22 @@ The above would output:
235236
3
236237
~~~~~
237238

238-
Then there's the [`range()`](https://docs.python.org/library/functions.html#range) function.
239-
The `range()` function returns a `list` with its contents dependent on the arguments passed to it.
240-
The Python documentation explains it quite nicely:
239+
Then there's the [`range()`](https://docs.python.org/3/library/stdtypes.html#typesseq-range) object.
240+
241+
`range()` objects represent a sequence of numbers. They can be constructed with
242+
one, two or three arguments to describe what the sequence of numbers should be.
241243

242-
> `range([start], stop[, step])` (note: the square brackets here mean 'optional')
243-
>
244-
> If the `step` argument is omitted, it defaults to `1`. If the `start` argument is
245-
> omitted, it defaults to `0`. The full form returns a list of plain integers
246-
> `[start, start + step, start + 2 * step, ...]`. If `step` is positive, the last
247-
> element is the largest `start + i * step` less than `stop`; if `step` is negative,
248-
> the last element is the smallest `start + i * step` greater than `stop`.
244+
If only one argument is provided then the sequence starts at `0` and stops just
245+
before the given number. Otherwise the first argument is the number to start at,
246+
the second argument is the number to stop before and the (optional) third
247+
argument is how big the steps should be (by default steps are `1`).
249248

250-
So, based on that, `range(3)` would return the list `[0, 1, 2]` because it is shorthand for `range(0, 3, 1)`.
251-
From the quote, you can see that this would return a list starting from `0`,
252-
and finishing with the integer one less than `3`, hence the `[0, 1, 2]`.
249+
So, based on this, `range(3)` would represent the sequence `0`, `1`, `2` because
250+
it is shorthand for `range(0, 3, 1)`.
253251

254252
So, taking `range(10, 80, 10)`, for example, would output `10` as the first element,
255253
then `20, 30, ...` up until `x=10+i*10` for some `i` where `i` ensures `x < stop` (which, in this case, is `80`).
256-
So, the `80` I've used could equally have been `77` or even `71` and the outputted `list` would still be `[10, 20, 30, 40, 50, 60, 70]`.
254+
So, the `80` we've used could equally have been `77` or even `71` and the outputted sequence would still be `10`, `20`, `30`, `40`, `50`, `60`, `70`.
257255

258256
Putting all of that together should mean you understand the above code.
259257
You might want to run the code on your kit to see if it does what you expect it to.

0 commit comments

Comments
 (0)