← Python Foundations with Turtle

Python Foundations with Turtle: recap

Answer each question in your head or on paper first, then check it.

Commands, loops and values

1. Drawing with a turtle

Try firstA turtle runs forward(100), right(90), forward(50). How many lines does it draw, and which is longer, the first or the second?

Check your answer

Two lines (the turn does not draw). The first is longer: 100 steps against 50.

A program is a list of commands the computer follows in order, from the top down. forward(100) moves the turtle 100 steps and draws a line. right(90) turns it on the spot, a quarter of a full turn clockwise; it does not move or draw. left(90) turns the other way.

Because each turn changes the way the turtle faces, the order of the commands is the picture. Swapping two lines gives a different drawing.

The first line, from turtle import *, gives a program the turtle commands. Students copy it for now; Functions and Modules explain how it works.

The turtle follows your commands from the top down · Sound on for the voiceover
Open the lesson →

2. Repeat with a loop

Try firstA turtle draws a shape with 5 equal sides using a loop. How many times does the loop repeat, and how far does the turtle turn each time?

Check your answer

5 times, turning 72 degrees each time (360 divided by 5).

A loop repeats the lines indented under it. for i in range(4): means "do the indented lines 4 times". Instead of writing the same two lines four times, you write them once and say how often. The name i is a counter that takes the values 0, 1, 2, 3 on the four passes. A square does not use it, but later programs do.

forward(100) moves the turtle 100 steps and draws a line. right(90) turns it on the spot, a quarter of a full turn.

To close any shape, the turtle turns all the way round once in total: 360 degrees. Split that between the corners: 360 ÷ 4 = 90 for a square, 360 ÷ 3 = 120 for a triangle.

A loop repeats lines, so you write them once · Sound on for the voiceover
Open the lesson →

3. Name a value

Try firstA program has sides = 8 and angle = 360 / sides. How far does the turtle turn at each corner?

Check your answer

45 degrees (360 divided by 8).

A variable is a name for a value. sides = 5 means "store 5 in a box called sides". After that, every time the program sees sides, it uses 5.

Variables can hold the result of a calculation: angle = 360 / sides works out the turn for you. Change sides and the turn updates by itself.

A line uses the value a name holds at the moment it runs. Storing a new value changes the lines that come after it, not the ones already run.

A variable is a name for a value · Sound on for the voiceover
Open the lesson →

4. Use the counter

Try firstIn for i in range(5): forward(i * 10), what is the length of the third line?

Check your answer

20. The third pass has i = 2, and 2 * 10 = 20.

A loop made with for i in range(30): keeps a counter called i. It is 0 on the first pass, 1 on the second, 2 on the third, and so on up to 29.

Any line inside the loop can use i. forward(i * 5) draws a line that is 0, then 5, then 10 steps long, so each one is longer than the last and the path spirals outward.

The counter i changes every pass · Sound on for the voiceover
Open the lesson →

5. A value that grows

Try firstWhat does size = size + 5 do if size is 20?

Check your answer

It works out 20 + 5 = 25 and stores 25 back in size.

size = size + 5 looks odd, but read it right to left: work out size + 5, then store the answer back in size. That is how a value grows each time round a loop.

Put the update inside the loop and it happens on every pass. Put it after the loop and it happens once.

Last lesson the counter i made the lines grow. A variable that updates itself does the same job without the counter.

A variable can change itself · Sound on for the voiceover
Open the lesson →

6. A street of houses

Try firstThe street has houses = 3, width = 50 and gap = 10. What does the last line print, and where does house 2 start if house 1 starts at x = -220?

Check your answer

170 (3 × 50 + 2 × 10). House 2 starts at -160 (-220 + 50 + 10).

A longer program is made of parts, each with one job. Here: name the numbers, move to the start, then a loop that repeats one house (walls, roof, step along), and a last line that works out the street's length.

The house loop has a loop inside it: the inner loop draws the four walls, the outer loop repeats the whole house. Everything indented under for house in range(houses): happens once per house.

The parts are joined by variables. width is used by the walls, the roof and the length, so changing it changes all three. That is why reading a program means asking, for each variable, "where else is this used?"

Open the lesson →

Choices and lists

7. If and else

Try firstA loop colours pass i red if i < 2, else blue. For range(5), how many passes are blue?

Check your answer

Three. The red passes are i = 0 and 1, so 2, 3 and 4 are blue.

An if lets your program choose. if i < 3: asks a question. If the answer is yes (True), the indented lines under it run. If not (False), the lines under else run instead. Only one of the two paths runs on each pass.

< means "less than". Careful: one = stores a value, two == ask whether two things are equal.

if lets the program choose which lines run · Sound on for the voiceover
Open the lesson →

8. Every other

Try firstA loop colours pass i red if i % 2 == 0, else blue. What colour is pass 5, and why?

Check your answer

Blue. 5 % 2 is 1, so the condition is false and the else path runs.

% gives the remainder after dividing. Even numbers divided by 2 leave nothing over (remainder 0), so i % 2 == 0 is true for 0, 2, 4, 6: every second pass. i % 3 == 0 is true for 0, 3, 6, 9: every third pass.

elif adds another question, asked only when the earlier ones were no. Python asks the questions in order and stops at the first true one.

The remainder picks every second pass · Sound on for the voiceover
Open the lesson →

9. Lists

Try firstcolours = ["red", "blue", "green", "gold"]. What is colours[1], and what is the place of "gold"?

Check your answer

colours[1] is "blue" (counting starts at 0). "gold" is in place 3.

A list keeps many values in order under one name: colours = ["red", "blue", "green"].

Each item has a place, counted from 0. colours[0] is the first item, colours[2] the third. A list of 3 items has places 0, 1 and 2, and asking for place 3 is an error.

len(colours) counts the items. for i in range(len(colours)): runs once for each item, with i as its place.

A list keeps many values in order, under one name · Sound on for the voiceover
Open the lesson →

10. For each item

Try firstsizes = [10, 20, 30]. for s in sizes: forward(s). How far does the turtle go on the second pass?

Check your answer

20. On the second pass s holds the second item, 20.

for c in colours: takes the items of a list one at a time. Each time round the loop, c holds the next item itself, so the loop runs once for every item. You don't count places.

Use the place (colours[i]) when the position matters. Use for-each when only the item matters.

for c in colours: takes the items one at a time · Sound on for the voiceover
Open the lesson →

11. Challenge: a tile

Try firstA grid has 5 rows and 6 columns. How many squares is that, and which line would you change to get 8 columns?

Check your answer

30 squares. Change the number in the inner loop, range(6), and the backward(size * 6) line to match.

A loop inside a loop draws a grid: the outer loop makes the rows and the inner loop makes the squares in each row. The remainder of row + col divided by the length of the list gives a colour place that repeats in a pattern.

Predict first, then run. When the result differs from your prediction, work out which part you misread.

Open the lesson →