Skip to content

Commit ed2865e

Browse files
committed
Update with latest commits, expanding on loops mainly
1 parent 591ad67 commit ed2865e

File tree

14 files changed

+216
-114
lines changed

14 files changed

+216
-114
lines changed

de/_images/shapes.png

1.15 KB
Loading

de/_sources/functions_parameters.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Exercise
5555
Write a function that allows you to draw hexagons of any size you want, each
5656
time you call the function.
5757

58+
Solution
59+
--------
60+
5861
::
5962

6063
def hexagon(size):
@@ -69,7 +72,11 @@ Exercise
6972
--------
7073

7174
Write a function that will draw a shape of *any* number of sides (let's assume
72-
greater than two) of any side length. Get it to draw some different shapes.
75+
greater than two) of any side length. Get it to draw some different shapes.
76+
77+
Here's an example of drawing shapes with this function:
78+
79+
.. image:: /images/shapes.png
7380

7481
.. tip::
7582

de/_sources/loops.txt

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,66 @@ Loops
44
Introduction
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

1415
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-
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
2223
different 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+
2430
You 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
4460
other 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

5168
Drawing 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

92109
Comments
93-
--------
110+
========
94111

95112
In the example above, the line that starts with a ``#`` is called a
96113
comment. 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

101118
Comments 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

105122
More 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!`

de/functions_parameters.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ <h2>Eine parametrisierte Funktion für ein Hexagon mit variabler Grösse<a class
9393
<div class="section" id="exercise">
9494
<h3>Übung<a class="headerlink" href="#exercise" title="Permalink zu dieser Überschrift"></a></h3>
9595
<p>Schreibe eine Funktion die Dir jedes Mal wenn Du die Funktion aufrufsts, erlaubt Hexagone mit beliebiger Grösse zu zeichnen.</p>
96+
</div>
97+
<div class="section" id="solution">
98+
<h3>Lösung<a class="headerlink" href="#solution" title="Permalink zu dieser Überschrift"></a></h3>
9699
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">hexagon</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
97100
<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">6</span><span class="p">):</span>
98101
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
@@ -107,13 +110,15 @@ <h2>Eine Funktion mit mehreren Parametern<a class="headerlink" href="#a-function
107110
<h3>Übung<a class="headerlink" href="#id1" title="Permalink zu dieser Überschrift"></a></h3>
108111
<p>Write a function that will draw a shape of <em>any</em> number of sides (let&#8217;s assume
109112
greater than two) of any side length. Get it to draw some different shapes.</p>
113+
<p>Here&#8217;s an example of drawing shapes with this function:</p>
114+
<img alt="_images/shapes.png" src="_images/shapes.png" />
110115
<div class="admonition tip">
111116
<p class="first admonition-title">Tipp</p>
112117
<p class="last">The sum of the internal angles of any shape is always 360 degrees!</p>
113118
</div>
114119
</div>
115-
<div class="section" id="solution">
116-
<h3>Lösung<a class="headerlink" href="#solution" title="Permalink zu dieser Überschrift"></a></h3>
120+
<div class="section" id="id2">
121+
<h3>Lösung<a class="headerlink" href="#id2" title="Permalink zu dieser Überschrift"></a></h3>
117122
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">draw_shape</span><span class="p">(</span><span class="n">sides</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span>
118123
<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="n">sides</span><span class="p">):</span>
119124
<span class="n">turtle</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">length</span><span class="p">)</span>
@@ -150,11 +155,12 @@ <h3><a href="index.html">Inhalt</a></h3>
150155
<li><a class="reference internal" href="#introduction">Einführung</a></li>
151156
<li><a class="reference internal" href="#a-parameterized-function-for-a-variable-size-hexagon">Eine parametrisierte Funktion für ein Hexagon mit variabler Grösse</a><ul>
152157
<li><a class="reference internal" href="#exercise">Übung</a></li>
158+
<li><a class="reference internal" href="#solution">Lösung</a></li>
153159
</ul>
154160
</li>
155161
<li><a class="reference internal" href="#a-function-of-several-parameters">Eine Funktion mit mehreren Parametern</a><ul>
156162
<li><a class="reference internal" href="#id1">Übung</a></li>
157-
<li><a class="reference internal" href="#solution">Lösung</a></li>
163+
<li><a class="reference internal" href="#id2">Lösung</a></li>
158164
<li><a class="reference internal" href="#bonus">Bonus</a></li>
159165
</ul>
160166
</li>

de/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ <h1>Programmiereinführung mit Python<a class="headerlink" href="#introduction-t
7373
<li class="toctree-l1"><a class="reference internal" href="loops.html">Schleifen</a><ul>
7474
<li class="toctree-l2"><a class="reference internal" href="loops.html#introduction">Einführung</a></li>
7575
<li class="toctree-l2"><a class="reference internal" href="loops.html#drawing-a-dashed-line">Eine gestrichelte Linie zeichnen</a></li>
76+
<li class="toctree-l2"><a class="reference internal" href="loops.html#comments">Kommentare</a></li>
7677
<li class="toctree-l2"><a class="reference internal" href="loops.html#more-efficient-squares">More Efficient Squares</a></li>
7778
</ul>
7879
</li>

0 commit comments

Comments
 (0)