In this article we will cover :
- Review of loops and conditionals
- Further study of conditionals
- Cleaner syntax for interaction with variables
- Draw the following shapes
Hint! If you're stuck review material covered in the previous class! Introducing Python to students in more detail
Alternating Behaviour using
Make sure you understand how to draw a pentagon using loops before proceeding!
There are plenty of times we are going to want to flip between 2 (or more!) options like
Consider the following pattern
Here we wish to flip from thick to thin lines. Let's look at a few ways of accomplishing this!
import turtle as tl thin = False sides = 8 edge = 100 for _ in range(sides): if thin: width = 1 thin = False else: width = 10 thin = True tl.width(width) tl.forward(edge) tl.left(360 / sides) tl.exitonclick()
Here we can see I've introduced a new variable, thin. This doesn't hold a round number, but what we call a boolean. A boolean can be True or False only. In the previous lesson we looked at tests that produce these.
In the code the variable thin alternates between True and False, changing the pen width.
Another way to write this is
import turtle as tl thin = False sides = 8 edge = 100 for _ in range(sides): width = 1 if thin else 10 thin = not thin tl.width(width) tl.forward(edge) tl.left(360 / sides) tl.exitonclick()
Here, I've rewritten the if statement using just two lines
width = 1 if thin else 10 we say width is 1 if thin is True, otherwise set it to be 10.
This is known as the ternary operator and is very useful!
On the second line I use
thin = not thin.
The not keyword flips True to become False and False to become True.
Again, very handy!
Exercise Set 1
Draw the following shapes
Looking at the
for loop in more detail
So far we have used
for _ in range(5) to repeat a section of code, in this case 5 times.
Let's alter this and see some more interesting behaviour!
sides = 8 edge = 100 for j in range(sides): current_width = 5 * j + 1 tl.width(current_width) tl.forward(edge) tl.left(360 / sides) tl.exitonclick()
Here instead of the underscore I've written
for j in range(sides).
j will count through
0, 1, 2, 3, 4, 5, 6, 7.
Each run, will alter the value of the variable
current_width which will go through
1, 6, 11, 16, 21, 26, 31, 36.
So the question becomes, how can we alter which numbers we count through?
This is where
range comes in.
This creates a number pattern for us to count through.
Here are some examples of using it :
Therefore we can alter the code to be a little nicer like so
import turtle as tl sides = 8 edge = 100 for current_width in range(1, 40, 5): tl.width(current_width) tl.forward(edge) tl.left(360 / sides) tl.exitonclick()
Remember if you type,
help(range) into the interactive mode of Python, text describing how it works will be given to you!
Exercise Set 2
import turtle as tl for width in range(1, 10): tl.width(width) step = width * 10 tl.forward(step) tl.exitonclick()
- Write the code to produce the following patterns
In this post we have continued our exploration of
for loop and