Repository navigation
Expand file tree
/
Copy pathloops.html
More file actions
353 lines (319 loc) · 18.9 KB
/
Copy pathloops.html
File metadata and controls
353 lines (319 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Loops — Introduction to Programming with Python</title>
<link rel="stylesheet" href="_static/basic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-3.3.4/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-3.3.4/css/bootstrap-theme.min.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2015.11.01',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/spoilers.js"></script>
<script type="text/javascript" src="_static/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="_static/js/jquery-fix.js"></script>
<script type="text/javascript" src="_static/bootstrap-3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="_static/bootstrap-sphinx.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="Introduction to Programming with Python" href="index.html" />
<link rel="next" title="User-defined functions" href="functions.html" />
<link rel="prev" title="Variables" href="variables.html" />
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1'>
<meta name="apple-mobile-web-app-capable" content="yes">
</head>
<body role="document">
<div id="navbar" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">
Python for Beginners</a>
<span class="navbar-text navbar-version pull-left"><b>2015.11.01</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="dropdown globaltoc-container">
<a role="button"
id="dLabelGlobalToc"
data-toggle="dropdown"
data-target="#"
href="index.html">Material <b class="caret"></b></a>
<ul class="dropdown-menu globaltoc"
role="menu"
aria-labelledby="dLabelGlobalToc"><ul class="current">
<li class="toctree-l1"><a class="reference internal" href="getting_started.html">Getting started</a></li>
<li class="toctree-l1"><a class="reference internal" href="simple_drawing.html">Simple drawing with turtle</a></li>
<li class="toctree-l1"><a class="reference internal" href="variables.html">Variables</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="functions.html">User-defined functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="functions_parameters.html">Functions with parameters</a></li>
<li class="toctree-l1"><a class="reference internal" href="conditionals.html">Conditional statements</a></li>
<li class="toctree-l1"><a class="reference internal" href="conditional_loops.html">Conditional Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="logical_operators.html">Logical operators</a></li>
<li class="toctree-l1"><a class="reference internal" href="where_to_go.html">Where to go from here</a></li>
<li class="toctree-l1"><a class="reference internal" href="copyright.html">License</a></li>
</ul>
</ul>
</li>
<li class="dropdown">
<a role="button"
id="dLabelLocalToc"
data-toggle="dropdown"
data-target="#"
href="#">Chapter <b class="caret"></b></a>
<ul class="dropdown-menu localtoc"
role="menu"
aria-labelledby="dLabelLocalToc"><ul>
<li><a class="reference internal" href="#">Loops</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#drawing-a-dashed-line">Drawing a dashed line</a><ul>
<li><a class="reference internal" href="#exercise">Exercise</a></li>
<li><a class="reference internal" href="#solution">Solution</a></li>
<li><a class="reference internal" href="#bonus">Bonus</a></li>
</ul>
</li>
<li><a class="reference internal" href="#comments">Comments</a></li>
<li><a class="reference internal" href="#more-efficient-squares">More Efficient Squares</a><ul>
<li><a class="reference internal" href="#id1">Exercise</a></li>
<li><a class="reference internal" href="#id2">Solution</a></li>
<li><a class="reference internal" href="#id3">Bonus</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</li>
<li class="navbar-rel">
<a href="variables.html" title="Previous Chapter: Variables">
<span class="glyphicon glyphicon-step-backward"></span>
<span>Previous</span>
</a>
</li>
<li class="navbar-rel">
<a href="functions.html" title="Next Chapter: User-defined functions">
<span class="glyphicon glyphicon-step-forward"></span>
<span>Next</span>
</a>
</li>
<li class="dropdown">
<a role="button"
data-toggle="dropdown"
data-target="#"
href="#">Languages <b class="caret"></b></a>
<ul class="dropdown-menu"
role="menu">
<li><a href="../en/index.html">English</a></li>
<li><a href="../de/index.html">Deutsch (German)</a></li>
<li><a href="../es_CL/index.html">Español (Spanish)</a></li>
<li><a href="../ru/index.html">русский (Russian)</a></li>
<li><a href="../ko/index.html">한국인 (Korean)</a></li>
<li><a href="../ro/index.html">Română (Romanian)</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section" id="loops">
<h1>Loops<a class="headerlink" href="#loops" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Something you might have noticed: our programs often feature repetition.
Python has a powerful concept it makes use of called looping
(jargon: <em>iteration</em>), which we can use to cut out our reptitive code!
For now, <strong>try this easy example</strong>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="s">"John"</span><span class="p">,</span> <span class="s">"Sam"</span><span class="p">,</span> <span class="s">"Jill"</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Hello "</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
</pre></div>
</div>
<p>This is incredibly helpful if we want to do something multiple times — say,
drawing the individual border lines of a shape — but only want to write that
action once. Here’s another version of a loop:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</pre></div>
</div>
<p>Notice how we write only one line of code using <code class="docutils literal"><span class="pre">i</span></code>, but it takes on 10
different values?</p>
<p>The <code class="samp docutils literal"><span class="pre">range(n)</span></code> function can be considered a shorthand
for <code class="docutils literal"><span class="pre">0,</span> <span class="pre">1,</span> <span class="pre">2,</span> <span class="pre">...,</span> <span class="pre">n-1</span></code>. If you want to know more about it, you can use
the help in the Python shell by typing <code class="docutils literal"><span class="pre">help(range)</span></code>.
Use the <code class="kbd docutils literal"><span class="pre">q</span></code> key to exit the help again.</p>
<p>You can also loop over elements of your choice:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">total</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">13</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">total</span> <span class="o">=</span> <span class="n">total</span> <span class="o">+</span> <span class="n">i</span>
<span class="k">print</span><span class="p">(</span><span class="n">total</span><span class="p">)</span>
</pre></div>
</div>
<p>Write this example out and run it with python, to check it works how you might
expect.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Notice how above, the lines of code that are <em>looped</em>, are the ones that
are <em>indented</em>. This is an important concept in Python - that’s how it
knows which lines should be used in the <code class="docutils literal"><span class="pre">for</span></code> loop, and which come
after, as part of the rest of your program. Use four spaces (hitting tab)
to indent your code.</p>
</div>
<p>Sometimes you want to repeat some code a number of times, but don’t care about
the value of the <code class="docutils literal"><span class="pre">i</span></code> variable; so it can be good practice to replace it
with <code class="docutils literal"><span class="pre">_</span></code> instead. This signifies that we don’t care about its value, or
don’t wish to use it. Here’s a simple example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Hello!"</span><span class="p">)</span>
</pre></div>
</div>
<p>You may or may not be wondering about the variable <code class="docutils literal"><span class="pre">i</span></code> - why is it used all
the time above? Well, it simply stands for “index” and is one of the most
common variable names ever found in code. But if you are looping over something
other than just numbers, be sure to name it something better! For instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">drink</span> <span class="ow">in</span> <span class="n">list_of_beverages</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Would you like a "</span> <span class="o">+</span> <span class="n">drink</span> <span class="o">+</span> <span class="s">"?"</span><span class="p">)</span>
</pre></div>
</div>
<p>This is immediately clearer to understand than if we had used <code class="docutils literal"><span class="pre">i</span></code>
instead of <code class="docutils literal"><span class="pre">drink</span></code>.</p>
</div>
<div class="section" id="drawing-a-dashed-line">
<h2>Drawing a dashed line<a class="headerlink" href="#drawing-a-dashed-line" title="Permalink to this headline">¶</a></h2>
<div class="section" id="exercise">
<h3>Exercise<a class="headerlink" href="#exercise" title="Permalink to this headline">¶</a></h3>
<p>Draw a dashed line. You can move the turtle without the turtle drawing its
movement by using the <code class="docutils literal"><span class="pre">turtle.penup()</span></code> function; to tell it to draw again,
use <code class="docutils literal"><span class="pre">turtle.pendown()</span></code>.</p>
<img alt="_images/dashed.png" src="_images/dashed.png" />
</div>
<div class="solution section" id="solution">
<h3>Solution<a class="headerlink" href="#solution" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">penup</span><span class="p">()</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">pendown</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="bonus">
<h3>Bonus<a class="headerlink" href="#bonus" title="Permalink to this headline">¶</a></h3>
<p>Can you make the dashes become larger as the line progresses?</p>
<img alt="_images/dashedprogressing.png" src="_images/dashedprogressing.png" />
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p>Feeling lost? Inspect <code class="docutils literal"><span class="pre">i</span></code> at every run of the loop:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="c"># write more code here</span>
</pre></div>
</div>
<p class="last">Can you utilize <code class="docutils literal"><span class="pre">i</span></code> — commonly called the <strong>index</strong> variable or loop
variable — to get increasing step sizes?</p>
</div>
</div>
</div>
<div class="section" id="comments">
<h2>Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2>
<p>In the example above, the line that starts with a <code class="docutils literal"><span class="pre">#</span></code> is called a
comment. In Python, anything that goes on a line after <code class="docutils literal"><span class="pre">#</span></code> is ignored
by the computer. Use comments to explain what your program does,
without changing the behaviour for the computer. They can also be used
to easily and temporarily disable, or “comment out” some lines of code.</p>
<p>Comments can also go at the end of a line, like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="c"># tilt our next square slightly</span>
</pre></div>
</div>
</div>
<div class="section" id="more-efficient-squares">
<h2>More Efficient Squares<a class="headerlink" href="#more-efficient-squares" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id1">
<h3>Exercise<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>The squares we were drawing at the start of this tutorial had a lot of
repeated lines of code. Can you write out a square drawing program in fewer
lines by utilizing loops?</p>
</div>
<div class="solution section" id="id2">
<h3>Solution<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="n">turtle</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="id3">
<h3>Bonus<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<p>Try <em>nesting</em> loops, by putting one right under (<em>inside</em>) the other, with some
drawing code that’s inside both. Here’s what it can look like:</p>
<div class="highlight-python"><div class="highlight"><pre>for ...:
for ...:
# drawing code inside the inner loop goes here
...
# you can put some code here to move
# around after!
...
</pre></div>
</div>
<p>Replace the <code class="docutils literal"><span class="pre">...</span></code>‘s with your own code, and see if you can come up with
something funny or interesting! <sup>Mistakes are encouraged!</sup></p>
</div>
</div>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-right">
<a href="#">Back to top</a>
<br/>
<ul id="sourcelink" class="list-inline">
<li>
<a href="https://github.com/opentechschool/python-beginners/edit/master/source/loops.rst" title="loops.rst">
Edit on Github
</a>
</li>
<li>
<a href="https://github.com/OpenTechSchool/python-beginners/issues/new?title=Problem+with+loops">
Report a problem
</a>
</li>
</ul>
</p>
<p>
© <a href="copyright.html">Copyright</a> 2012–2014, OpenTechSchool and contributors.<br/>
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.<br/>
</p>
</div>
</footer>
</body>
</html>