44Introduction
55============
66
7- One more thing: Our programs often feature repetition. There is a powerful
8- concept in Python called looping (jargon: *iteration*), which we will
9- elaborate later on. For now, **try this easy example**::
7+ Something you might have noticed: our programs often feature repetition.
8+ Python has a powerful concept it makes use of called looping
9+ (jargon: *iteration*), which we can use to cut out our reptitive code!
10+ For now, **try this easy example**::
1011
11- for i in range(10) :
12- print("Hello!" )
12+ for name in "John", "Sam", "Jill" :
13+ print("Hello " + name )
1314
1415This 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
16- once. But it gets better ::
16+ drawing the individual border lines of a shape --- but only want to write that
17+ action once. Here's another version of a loop ::
1718
1819 for i in range(10):
1920 print(i)
2021
21- Notice how we write only one line of code using ``i`` but it has 10
22+ Notice how we write only one line of code using ``i``, but it takes on 10
2223different values?
2324
25+ The :samp:`range(n)` function can be considered a shorthand
26+ for ``0, 1, 2, ..., n-1``. If you want to know more about it, you can use
27+ the help in the Python shell by typing ``help(range)``.
28+ Use the :kbd:`q` key to exit the help again.
29+
2430You can also loop over elements of your choice::
2531
32+ total = 0
2633 for i in 5, 7, 11, 13:
2734 print(i)
35+ total = total + i
36+ print(total)
2837
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
31- typing ``help(range)``. Use the :kbd:`q` key to exit the help again.
38+ Write this example out and run it with python, to check it works how you might
39+ expect.
3240
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::
41+ .. note::
42+
43+ Notice how above, the lines of code that are *looped*, are the ones that
44+ are *indented*. This is an important concept in Python - that's how it
45+ knows which lines should be used in the ``for`` loop, and which come
46+ after, as part of the rest of your program. Use four spaces (hitting tab)
47+ to indent your code.
48+
49+ Sometime you want to repeat some code a number of times, but don't care about
50+ the value of the ``i`` variable; so it can be good practice to replace it
51+ with ``_`` instead. This signifies that we don't care about its value, or
52+ don't wish to use it. Here's a simple example::
3753
3854 for _ in range(10):
3955 print("Hello!")
4056
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
57+ You may or may not be wondering about the variable ``i`` - why is it used all
58+ the time above? Well, it simply stands for "index" and is one of the most
59+ common variable names ever found in code. But if you are looping over something
4460other than just numbers, be sure to name it something better! For instance::
4561
46- for name in list_of_first_names :
47- print("Hi there, " + name )
62+ for drink in list_of_beverages :
63+ print("Would you like a " + drink + "?" )
4864
49- This is easier and clearer to read than if we had used ``i`` instead of ``name``.
65+ This is immediately clearer to understand than if we had used ``i``
66+ instead of ``drink``.
5067
5168Drawing a dashed line
5269=====================
@@ -90,7 +107,7 @@ Can you make the dashes become larger as the line progresses?
90107 variable --- to get increasing step sizes?
91108
92109Comments
93- --------
110+ ========
94111
95112In the example above, the line that starts with a ``#`` is called a
96113comment. In Python, anything that goes on a line after ``#`` is ignored
@@ -100,7 +117,7 @@ to easily and temporarily disable, or "comment out" some lines of code.
100117
101118Comments can also go at the end of a line, like this::
102119
103- turtle.left(20) # now we can change the angle only here
120+ turtle.left(20) # tilt our next square slightly
104121
105122More Efficient Squares
106123======================
@@ -132,8 +149,8 @@ drawing code that's inside both. Here's what it can look like::
132149 # drawing code inside the inner loop goes here
133150 ...
134151 # you can put some code here to move
135- # around after
152+ # around after!
136153 ...
137154
138- Replace the ``...`` with your own stuff, and see if you can come up with
139- something funny or interesting! Mistakes are encouraged.
155+ Replace the ``...``'s with your own stuff, and see if you can come up with
156+ something funny or interesting! :sup:` Mistakes are encouraged!`
0 commit comments