Skip to content

Commit c14d583

Browse files
foonicornprojectgus
authored andcommitted
Sphinx build
1 parent 469864d commit c14d583

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4503
-0
lines changed

Diff for: .nojekyll

Whitespace-only changes.

Diff for: en/.buildinfo

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: 5ce55d88e65e0217306dc230d617f309
4+
tags: fbb0d17656682115ca4d033fb2f83ba1

Diff for: en/_images/dashed.png

196 Bytes
Loading

Diff for: en/_images/dashedprogressing.png

219 Bytes
Loading

Diff for: en/_images/default.png

197 Bytes
Loading

Diff for: en/_images/forward.png

192 Bytes
Loading

Diff for: en/_images/hexagon.png

708 Bytes
Loading

Diff for: en/_images/honeycomb.png

1.85 KB
Loading

Diff for: en/_images/house.png

680 Bytes
Loading

Diff for: en/_images/left.png

194 Bytes
Loading

Diff for: en/_images/rectangle.png

228 Bytes
Loading

Diff for: en/_images/square.png

208 Bytes
Loading

Diff for: en/_images/tiltedsquares.png

1.29 KB
Loading

Diff for: en/_sources/functions.txt

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
User-defined funtions
2+
*********************
3+
4+
Introduction
5+
============
6+
7+
There is still a lot of duplicated code --- the actual drawing of the rectangle
8+
--- around. If you need to copy and paste code, that is usually a sign of
9+
lacking abstractions. (Programmers call it a *code smell.*)
10+
11+
Functions are one way to express abstractions in Python. Let's take
12+
``reset()`` for example. It is actually an abstraction for a number of steps,
13+
namely:
14+
15+
* Erase the drawing board
16+
* Set the width and color back to default
17+
* Move the turtle back to its initial position
18+
19+
A function can be defined with the ``def`` keyword in Python::
20+
21+
def line_without_moving():
22+
forward(50)
23+
backward(50)
24+
25+
You can access names in functions as well::
26+
27+
size = 50
28+
def line_without_moving():
29+
forward(size)
30+
backward(size)
31+
32+
A function for a square
33+
=======================
34+
35+
Exercise
36+
--------
37+
38+
Write a function that draws a square. Can you see how you could improve the
39+
tilted squares program with that and greatly relieve experimentation?
40+
41+
42+
A function for a hexagon
43+
========================
44+
45+
Exercise
46+
--------
47+
48+
Write a function that draws a hexagon.
49+
50+
.. image:: /images/hexagon.png
51+
52+
Now combine that function into a honeycomb.
53+
54+
.. image:: /images/honeycomb.png
55+
56+
Solution
57+
--------
58+
59+
::
60+
61+
def hexagon():
62+
forward(100)
63+
left(60)
64+
forward(100)
65+
left(60)
66+
forward(100)
67+
left(60)
68+
forward(100)
69+
left(60)
70+
forward(100)
71+
left(60)
72+
forward(100)
73+
left(60)
74+
75+
hexagon()
76+
forward(100)
77+
right(60)
78+
79+
hexagon()
80+
forward(100)
81+
right(60)
82+
83+
hexagon()
84+
forward(100)
85+
right(60)
86+
87+
hexagon()
88+
forward(100)
89+
right(60)
90+
91+
hexagon()
92+
forward(100)
93+
right(60)
94+
95+
hexagon()
96+
forward(100)
97+
right(60)
98+

Diff for: en/_sources/functions_parameters.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Functions with parameters
2+
*************************
3+
4+
Todo

Diff for: en/_sources/index.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Introduction to Programming with Python
2+
***************************************
3+
4+
5+
Table of contents:
6+
7+
.. toctree::
8+
:maxdepth: 2
9+
10+
interactive_work
11+
simple_drawing
12+
names
13+
functions
14+
loops
15+
functions_parameters
16+

Diff for: en/_sources/interactive_work.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Interactive work
2+
****************
3+
4+
Todo

Diff for: en/_sources/loops.txt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Loops
2+
*****
3+
4+
Introduction
5+
============
6+
7+
One more thing: Our programs often featured repetition. There is a powerful
8+
concept in Python called looping, which we will elaborate later on. For now,
9+
take that easy example::
10+
11+
for i in range(10):
12+
print "Hello!"
13+
14+
Drawing a dashed line
15+
=====================
16+
17+
Exercise
18+
--------
19+
20+
Draw a dashed line. You can move the turtle without tracing a line behind you
21+
with the ``up()`` function; put it back on the ground with ``down()``.
22+
23+
.. image:: /images/dashed.png
24+
25+
Solution
26+
27+
::
28+
29+
for i in range(10):
30+
forward(15)
31+
up()
32+
forward(5)
33+
down()
34+
35+
Bonus
36+
-----
37+
38+
Can you make the dashes become larger as the line progresses?
39+
40+
.. image:: /images/dashedprogressing.png
41+
42+
Honeycomb loops
43+
===============
44+
45+
Exercise
46+
--------
47+
48+
Take your honeycomb program and make it easier with loops. How small can you
49+
get it?
50+
51+
Solution
52+
--------
53+
54+
::
55+
56+
def hexagon():
57+
for i in range(6):
58+
forward(100)
59+
left(60)
60+
61+
for i in range(6):
62+
hexagon()
63+
forward(100)
64+
right(60)

Diff for: en/_sources/names.txt

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Names
2+
*****
3+
4+
Introduction
5+
============
6+
7+
Whew. Experimenting with the angles requires you to change three different
8+
places each time. Imagine you'd want to experiment with the square sizes, let
9+
alone with rectangles! We can do better than that.
10+
11+
This is where names come into play: You can tell Python that from now on,
12+
whenever you refer to a name, you actually mean something else. That concept
13+
might be familiar from symbolic maths, where you would write: *Let x be 5.*
14+
Then x*2 will obviously be 10. That's why those names are known as variables,
15+
too.
16+
17+
In Python syntax, that very statement translates to::
18+
19+
x = 5
20+
21+
After that statement, if you do ``print x``, it will actually output its value
22+
--- 5. You can well use that for turtle too::
23+
24+
forward(x)
25+
26+
A variable called angle
27+
=======================
28+
29+
Exercise
30+
--------
31+
32+
If we have a variable called ``angle``, how could we use that to experiment
33+
much faster with our tilted squares program?
34+
35+
Solution
36+
--------
37+
38+
::
39+
40+
from turtle import *
41+
angle = 20
42+
43+
left(angle)
44+
45+
forward(50)
46+
left(90)
47+
forward(50)
48+
left(90)
49+
forward(50)
50+
left(90)
51+
forward(50)
52+
left(90)
53+
54+
left(angle)
55+
# ...
56+
57+
Bonus
58+
-----
59+
60+
Can you apply that principle to the size of the squares, too?
61+
62+
The house of santa claus
63+
========================
64+
65+
Exercise
66+
--------
67+
68+
Draw a house.
69+
70+
.. image:: /images/house.png
71+
72+
You can calculate the length of the diagonal line with Pythagoras. That value
73+
is a good candidate for a name binding.
74+
75+

0 commit comments

Comments
 (0)