Skip to content

Commit cd2d68f

Browse files
committed
Expand some paragraphs, clean up line-endings,
general copy-editing.
1 parent e7bc302 commit cd2d68f

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

‎source/loops.rst‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,50 @@ Loops
44
Introduction
55
============
66

7-
One more thing: Our programs often feature repetition. There is a powerful
7+
One more thing: Our programs often feature repetition. There is a powerful
88
concept in Python called looping (jargon: *iteration*), which we will
99
elaborate later on. For now, **try this easy example**::
1010

1111
for i in range(10):
1212
print("Hello!")
1313

14-
This is incredibly helpful if we want to do something multiple times --- say,
15-
drawing the individual border lines of a shape --- but only want to write it
14+
This is incredibly helpful if we want to do something multiple times --- say,
15+
drawing the individual border lines of a shape --- but only want to write it
1616
once. But it gets better::
1717

1818
for i in range(10):
1919
print(i)
2020

21-
Notice how we write only one line of code using ``i`` but it has 10 different
22-
values?
21+
Notice how we write only one line of code using ``i`` but it has 10
22+
different values?
2323

2424
You can also loop over elements of your choice::
2525

2626
for i in 5, 7, 11, 13:
2727
print(i)
2828

29-
The ``range(n)`` function can be considered a shorthand for ``0, 1, 2, ..., n-1``.
30-
If you want to know more about it, you can use the help in the Python shell by
29+
The ``range(n)`` function can be considered a shorthand for ``0, 1, 2, ..., n-1``.
30+
If you want to know more about it, you can use the help in the Python shell by
3131
typing ``help(range)``. Use the :kbd:`q` key to exit the help again.
3232

33-
If you want to repeat some code a number of times, but don't care about the value
34-
of the ``i`` variable, it can be good practice to replace it with ``_`` instead.
35-
This signifies that we don't care about its value, or don't wish to use it. So
36-
you could rewrite the first example as::
33+
If you want to repeat some code a number of times, but don't care about the
34+
value of the ``i`` variable, it can be good practice to replace it with
35+
``_`` instead. This signifies that we don't care about its value, or don't
36+
wish to use it. So you could rewrite the first example as::
3737

3838
for _ in range(10):
3939
print("Hello!")
4040

41+
You may or may not be wondering about the variable ``i`` - why is it used all the
42+
time above? Well, it simply stands for "index" and is one of the most common
43+
variable names ever found in code. But if you are looping over something
44+
other than just numbers, be sure to name it something better! For instance::
45+
46+
for name in list_of_first_names:
47+
print("Hi there, " + name)
48+
49+
This is easier and clearer to read than if we had used ``i`` instead of ``name``.
50+
4151
Drawing a dashed line
4252
=====================
4353

@@ -74,22 +84,21 @@ Can you make the dashes become larger as the line progresses?
7484

7585
for i in range(10):
7686
print(i)
77-
# ...
87+
# write more code here
7888

79-
Can you utilize ``i`` --- commonly called the **index** variable or loop
89+
Can you utilize ``i`` --- commonly called the **index** variable or loop
8090
variable --- to get increasing step sizes?
8191

8292
Comments
8393
--------
8494

85-
In the solution above, the line that starts with a ``#`` is called a
86-
comment. In Python, anything that goes on a line after ``#`` is ignored
87-
by the computer. Use comments to explain what your program does,
88-
without changing the behaviour for the computer.
95+
In the example above, the line that starts with a ``#`` is called a
96+
comment. In Python, anything that goes on a line after ``#`` is ignored
97+
by the computer. Use comments to explain what your program does,
98+
without changing the behaviour for the computer. They can also be used
99+
to easily and temporarily disable, or "comment out" some lines of code.
89100

90-
Comments can also go at the end of a line, like this:
91-
92-
::
101+
Comments can also go at the end of a line, like this::
93102

94103
turtle.left(20) # now we can change the angle only here
95104

@@ -126,4 +135,5 @@ drawing code that's inside both. Here's what it can look like::
126135
# around after
127136
...
128137

129-
Replace the ``...`` with your own stuff!
138+
Replace the ``...`` with your own stuff, and see if you can come up with
139+
something funny or interesting! Mistakes are encouraged.

0 commit comments

Comments
 (0)