One more thing: Our programs often feature repetition. There is a powerful concept in Python called looping (jargon: iteration), which we will elaborate later on. For now, try this easy example:
for i in range(10):
print("Hello!")
This is incredibly helpful if we want to do something multiple times --- say, drawing the individual border lines of a shape --- but only want to write it once. But it gets better:
for i in range(10):
print(i)
Notice how we write only one line of code using i but it has 10 different
values?
You can also loop over elements of your choice:
for i in 5, 7, 11, 13:
print(i)
The range(n) function can be considered a shorthand for 0, 1, 2, ..., n-1.
If you want to know more about it, you can use the help in the Python shell by
typing help(range). Use the q key to exit the help again.
If you want to repeat some code a number of times, but don't care about the value
of the i variable, it can be good practice to replace it with _ instead.
This signifies that we don't care about its value, or don't wish to use it. So
you could rewrite the first example as:
for _ in range(10):
print("Hello!")
Draw a dashed line. You can move the turtle without the turtle drawing its
movement by using the turtle.penup() function; to tell it to draw again,
use turtle.pendown().
for i in range(10):
turtle.forward(15)
turtle.penup()
turtle.forward(5)
turtle.pendown()
Can you make the dashes become larger as the line progresses?
Hint
Feeling lost? Inspect i at every run of the loop:
for i in range(10):
print(i)
# ...
Can you utilize i --- commonly called the index variable or loop
variable --- to get increasing step sizes?
Take your honeycomb program and make it easier with loops. How small can you get it?
def hexagon():
for i in range(6):
turtle.forward(100)
turtle.left(60)
for i in range(6):
hexagon()
turtle.forward(100)
turtle.right(60)
See if you can look at your earlier square-drawing code, and make it much shorter by using loops.

