You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: tutorials/basic_motor_control.md
+13-15Lines changed: 13 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -216,7 +216,8 @@ Again, as you've seen most of that before, it shouldn't be too difficult to get
216
216
The `for` loop may be new, however.
217
217
218
218
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
+
220
221
For a comprehensive introduction to to `list`s, have a look at [this WikiBooks article](https://en.wikibooks.org/wiki/Python_Programming/Lists).
221
222
The `for` loop will iterate over the `list` (i.e. take each element in turn)
222
223
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:
235
236
3
236
237
~~~~~
237
238
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.
241
243
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`).
249
248
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)`.
253
251
254
252
So, taking `range(10, 80, 10)`, for example, would output `10` as the first element,
255
253
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`.
257
255
258
256
Putting all of that together should mean you understand the above code.
259
257
You might want to run the code on your kit to see if it does what you expect it to.
0 commit comments