|
4 | 4 | Introduction |
5 | 5 | ============ |
6 | 6 |
|
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 |
8 | 8 | concept in Python called looping (jargon: *iteration*), which we will |
9 | 9 | elaborate later on. For now, **try this easy example**:: |
10 | 10 |
|
11 | 11 | for i in range(10): |
12 | 12 | print("Hello!") |
13 | 13 |
|
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 |
16 | 16 | once. But it gets better:: |
17 | 17 |
|
18 | 18 | for i in range(10): |
19 | 19 | print(i) |
20 | 20 |
|
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? |
23 | 23 |
|
24 | 24 | You can also loop over elements of your choice:: |
25 | 25 |
|
26 | 26 | for i in 5, 7, 11, 13: |
27 | 27 | print(i) |
28 | 28 |
|
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 |
31 | 31 | typing ``help(range)``. Use the :kbd:`q` key to exit the help again. |
32 | 32 |
|
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:: |
37 | 37 |
|
38 | 38 | for _ in range(10): |
39 | 39 | print("Hello!") |
40 | 40 |
|
| 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 | + |
41 | 51 | Drawing a dashed line |
42 | 52 | ===================== |
43 | 53 |
|
@@ -74,22 +84,21 @@ Can you make the dashes become larger as the line progresses? |
74 | 84 |
|
75 | 85 | for i in range(10): |
76 | 86 | print(i) |
77 | | - # ... |
| 87 | + # write more code here |
78 | 88 |
|
79 | | - Can you utilize ``i`` --- commonly called the **index** variable or loop |
| 89 | + Can you utilize ``i`` --- commonly called the **index** variable or loop |
80 | 90 | variable --- to get increasing step sizes? |
81 | 91 |
|
82 | 92 | Comments |
83 | 93 | -------- |
84 | 94 |
|
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. |
89 | 100 |
|
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:: |
93 | 102 |
|
94 | 103 | turtle.left(20) # now we can change the angle only here |
95 | 104 |
|
@@ -126,4 +135,5 @@ drawing code that's inside both. Here's what it can look like:: |
126 | 135 | # around after |
127 | 136 | ... |
128 | 137 |
|
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