Skip to content

Commit a0ccc16

Browse files
committed
Rebuilt site
1 parent b8fbd2b commit a0ccc16

300 files changed

Lines changed: 101453 additions & 2920 deletions

File tree

Some content is hidden

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

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ DESTINATION = docs
88

99
develop:
1010
sleep 1 && echo "Opening local browser..." && open http://localhost:1313/pythonbook &
11-
hugo server --buildDrafts --watch --destination $(DESTINATION)
11+
hugo server --config hugo.toml --buildDrafts --watch --destination $(DESTINATION)
1212

1313
html:
14-
hugo --destination $(DESTINATION)
14+
hugo --config hugo.toml --destination $(DESTINATION)
1515

1616
github: html
1717
git add -A :/; git commit -m "Rebuilt site" ; git push

content/Chapter_03/_index.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
+++
2-
title = "Program flow"
2+
title = "Program Flow"
33
date = 2023-09-22T11:30:50-04:00
44
weight = 300
55
chapter = true
66
pre = "<b>3. </b>"
77
+++
88

9-
### A first library use
9+
### Using Libraries
1010

1111
There are many *modules* in Python that provide very powerful features that we
1212
can use in our own programs. Some of these can send email, or fetch web pages.
1313
The one use at in this chapter allows us to create turtles and use them
1414
to draw shapes and patterns.
1515

1616
The turtles are fun, but the real purpose of the chapter is to teach ourselves
17-
a little more Python, and to develop our theme of *computational thinking*,
18-
or *thinking like a computer scientist*. Most of the Python covered here
19-
will be explored in more depth later.
17+
a little more Python, and to develop our abilities to *think like computer scientists*. Most of the Python covered here will be explored in more depth later.

content/Chapter_03/s01.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
11
+++
2-
title = "Hello, little turtles!"
2+
title = "Hello Turtles!"
33
weight = 301
44
+++
55

6-
76
## Our first turtle program
87

9-
Let&#8217;s write a couple of lines of Python program to create a new
10-
turtle and start drawing a rectangle. (We&#8217;ll call the variable that
11-
refers to our first turtle ```alex```, but we can choose another
12-
name if we follow the naming rules from the previous chapter).
13-
14-
To use the turtle library in a Collab Jupyter notebook, we start by installing the library.
8+
To use the turtle library in a Collab Jupyter notebook, we start by
9+
installing the library.
1510

1611
**You will have to execute this cell every time you restart a new session.**
1712

18-
```
13+
```Python
1914
!pip install ColabTurtlePlus
2015
```
2116

22-
Here is a first program we will analyze together.
17+
With the turtle library, we can draw by moving a virtual turtle around
18+
the screen. Let&#8217;s write a couple of lines of Python program to
19+
create a new turtle and start drawing a rectangle. (We&#8217;ll call
20+
the variable that refers to our first turtle ```shelly```, but you can
21+
choose any name you want so long as it follows the Python variable
22+
naming rules from the previous chapter).
2323

24-
```
25-
import ColabTurtlePlus.Turtle as turtle # Allows us to use turtles
24+
Here is a first program we will analyze together:
2625

27-
turtle.clearscreen() # Remove drawings from previous runs.
28-
turtle.setup(200,100) # Sets the dimensions of the viewing window.
26+
```Python
27+
import ColabTurtlePlus.Turtle as turtle # Imports the turtle library
2928

30-
alex = turtle.Turtle() # Create a turtle, assign to alex
29+
turtle.clearscreen() # Remove drawings from previous runs.
30+
turtle.setup(200, 100) # Sets the dimensions of the viewing window.
3131

32-
alex.forward(50) # Tell alex to move forward by 50 units
33-
alex.left(90) # Tell alex to turn left by 90 degrees
34-
alex.forward(30) # Complete the second side of a rectangle
32+
shelly = turtle.Turtle() # Create a turtle, assign to shelly
3533

34+
shelly.forward(50) # Tell shelly to move forward by 50 units
35+
shelly.left(90) # Tell shelly to turn left by 90 degrees
36+
shelly.forward(30) # Complete the second side of a rectangle
3637
```
3738

3839
Here are a couple of things we will need to understand about this program. (Do not worry if you do not understand them all now&#8212; we will cover them in more detail as we work through the book.)
3940

4041

4142
The first line, ```import ColabTurtlePlus.Turtle as turtle```,
42-
tells Python to load a module named ```ColabTurtlePlus.Turtle``` that will be used in this notebook with the name ```turtle```.
43-
That module brings us a new type that we can use: the ```Turtle``` type. The dot
43+
tells Python to load a module (we also this a _library_) named ```ColabTurtlePlus.Turtle``` that will be used in this notebook with the name ```turtle```.
44+
45+
The turtle library defines a new type, ```Turtle``` that has operations like ```forward()``` for moving the a turtle. The dot
4446
notation ```turtle.Turtle``` means *&#8220;The Turtle type that is defined within
4547
the turtle module&#8221;*. (Remember that Python is case sensitive, so the
4648
module name, with a lowercase *t*, is different from the type ```Turtle```.)
4749

48-
We then clear the screen to remove drawings from previous runs with ```turtle.clearscreen()``` and set the dimensions to a size that is big enough for the drawing with ```turtle.setup(200,100)```.
50+
The next line clears the screen to remove drawings from previous runs with ```turtle.clearscreen()``` and set the dimensions to a size that is big enough for the drawing with ```turtle.setup(200,100)```.
4951

50-
In the next line, ```alex = turtle.Turtle()```, we create a turtle and assign it to the variable ```alex```.
52+
Then, ```shelly = turtle.Turtle()``` creates a new turtle and assigns it to the variable ```shelly```.
5153

52-
Now we are ready to draw on the canvas using the turtle.
53-
```
54-
alex.forward(50) # Tell alex to move forward by 50 units
55-
alex.left(90) # Tell alex to turn left by 90 degrees
56-
alex.forward(30) # Complete the second side of a rectangle
54+
Now we are ready to draw using the turtle:
55+
```Python
56+
shelly.forward(50) # Tell shelly to move forward by 50 units
57+
shelly.left(90) # Tell shelly to turn left by 90 degrees
58+
shelly.forward(30) # Complete the second side of a rectangle
5759
```
5860

59-
In the last three lines, we instruct the **object** ```alex``` to move, and to turn. We
60-
do this by **invoking**, or activating, ```alex```&#8216;s **methods** &#8212; these are
61-
the instructions that all turtles know how to respond to.
61+
These lines instruct the Turtle **object** that the ```shelly```
62+
variable references to move, and to turn. We do this by **invoking**,
63+
or calling, **methods** of that Turtle object. Methods are functions defined by the turtle library that can be called on Turtle objects. Here, the ```forward``` method takes a parameter that gives the distance, and the Turtle object will move that distance in the direction it is pointing. The ```left``` method causes the Turtle object to turn so it is now facing a new direction.
6264

6365
Running the program produces the output below.
6466

content/Chapter_03/s02.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
+++
2-
title = "Colors!"
2+
title = "Color"
33
weight = 302
44
+++
55

6-
An object can have various methods &#8212; things it can do &#8212; and it can also have
7-
**attributes** (sometimes called *properties*). For example, each turtle has
8-
a *color* attribute. The method invocation
9-
```alex.color("red")``` will make ```alex``` red, and drawing will be red too.
6+
An object can have various methods &#8212; things it can do &#8212;
7+
and it can also have **attributes** (sometimes called
8+
*properties*). These are like internal variables that can be used to
9+
keep track of the state of the object.
10+
11+
For the Turtle objects, we can't manipulate the internal state
12+
directly, but instead can invoke methods on a Turtle object to change
13+
its internal state. We've already seen this with the facing direction
14+
in the last part &mdash; something must be keeping track of the
15+
direction the Turtle object is facing since it moves in a different
16+
forward direction after we called ```shelly.left(90)```.
1017

1118
The color of the turtle, the width of its pen, the position of the
1219
turtle within the window, which way it is facing, and so on are all part of its
1320
current **state**. Similarly, the window has a background color.
1421

15-
From the several methods that allow us to modify the turtle and the
16-
window we will show only a couple in the program below.
22+
The example below produces a nicer drawing by using the ```color``` and ```pensize``` methods to change the state of the Turtle object, and the ```bgcolor``` method to change the state of the canvas.
1723

18-
```
24+
```Python
1925
import ColabTurtlePlus.Turtle as turtle
2026

2127
turtle.clearscreen()
2228
turtle.setup(200,200)
2329

24-
turtle.bgcolor("lightgreen") # Set the background color
30+
turtle.bgcolor("lightgreen") # Set the background color
2531

26-
tess = turtle.Turtle()
27-
tess.color("blue") # Tell tess to change her color
28-
tess.pensize(3) # Tell tess to set her pen width
32+
shelly = turtle.Turtle()
33+
shelly.color("blue") # Tell shelly to change her color
34+
shelly.pensize(3) # Tell shelly to set her pen width
2935

30-
tess.forward(50)
31-
tess.left(120)
32-
tess.forward(50)
36+
shelly.forward(50)
37+
shelly.left(120)
38+
shelly.forward(50)
3339
```
3440

3541
Running the program produces the output below. Notice the chages in the size of the drawing area, color of the background and lines, and the thickness of the lines.

content/Chapter_03/s03.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
+++
2-
title = "Turtles!"
2+
title = "More Turtles"
33
weight = 303
44
+++
55

6-
## A bale of turtles
6+
## A Bale of Turtles
77

8-
Just like we can have many different integers in a program, we can have many turtles.
9-
Each of them is called an **instance**. Each instance has its own attributes and
10-
methods &#8212; so ```alex``` might draw with a thin black pen and be at some position,
11-
while ```tess``` might be going in her own direction with a thick pink pen.
8+
Just like we can have many different integers in a program, we can have many Turtle objects.
9+
Each of them is called an **instance**. Methods can be called on each instance to change its state and make it move.
1210

13-
```
11+
```Python
1412
import ColabTurtlePlus.Turtle as turtle
1513

1614
turtle.clearscreen()
1715
turtle.setup(200,200)
1816
turtle.bgcolor("lightgreen")
19-
# Create tess and set some attributes
20-
tess = turtle.Turtle()
17+
18+
19+
tess = turtle.Turtle() # Create tess and set some attributes
2120
tess.color("hotpink")
2221
tess.pensize(5)
23-
# Create alex
24-
alex = turtle.Turtle()
2522

26-
# Make tess draw equilateral triangle
23+
alex = turtle.Turtle() # Create alex
24+
25+
# Make tess draw an equilateral triangle
2726
tess.forward(80)
2827
tess.left(120)
2928
tess.forward(80)
@@ -47,9 +46,9 @@ alex.left(90)
4746

4847
There are 360 degrees in a full circle. If we add up all the turns that a turtle makes,
4948
*no matter what steps occurred between the turns*, we can easily figure out if they
50-
add up to some multiple of 360. This should convince us that ```alex``` is facing in
49+
add up to some multiple of 360. Since ```alex``` has turned ```90 + 90 + 90 + 90 = 360``` degrees, after all the turns he is facing in
5150
exactly the same direction as he was when he was first created. (Geometry
52-
conventions have 0 degrees facing East, and that is the case here too!)
51+
conventions have 0 degrees facing to the right, and that is the case here too!)
5352

5453
We could have left out the last turn for ```alex```, but if we are asked to draw a closed shape like a square or a rectangle, it is a good idea to
5554
complete all the turns and to leave the turtle back where it started, facing the
@@ -60,11 +59,14 @@ easier.
6059
We did the same with ```tess```: she drew her triangle, and turned through a full 360 degrees.
6160
Then we turned her around and moved her aside.
6261

63-
Blank and lines are used to separate groups of movements by shape. ```tess```&#8216; movements were gr as &#8220;draw the triangle&#8221;
62+
Blank lines are used to separate groups of movements by shape.The movements for
63+
```tess``` were &#8220;draw the triangle&#8221;
6464
(lines 12-17) and then &#8220;move away from the origin&#8221; (lines 19 and 20).</li>
65-
<li>One of the key uses for comments is to record our mental chunking, and big ideas.
65+
66+
One of the key uses for comments is to record our mental chunking, and big ideas.
6667
They&#8217;re not always explicit in the code.</li>
67-
<li>And, uh-huh, two turtles may not be enough for a herd. But the important idea is that the
68-
turtle module gives us a kind of factory that lets us create as many turtles as we
69-
need. Each instance has its own state and behavior.</li>
70-
</ul>
68+
69+
Two turtles may not be enough for a herd. But the important idea is
70+
that the turtle module gives us a kind of factory that lets us create
71+
as many turtles as we need. Each instance has its own state and
72+
behavior.

0 commit comments

Comments
 (0)