The thing about computers is they love repetitive tasks. If you wanted to draw a grid of 100 rectangles you could write 100 lines of code, each line drawing a rect() with a slightly different position. But that's the hard way, we can get by with only a few lines of code if we have Python repeat the command for us.

The easiest way to have Python repeat itself is to first get it counting. Python has a range() function that only exists to make a list of numbers in a particular range. Let's ask Python to bulid a range of 20 numbers, range(20), which we'll pick through one at a time.

Download: file
for i in range(20):
  print(i)

You'll remember that we did something similar with a for loop in a previous section of this guide, where we had a list of names and picked one name out at a time. This example is really similar, Python built a range of numbers, and we fetched and printed each one, one at a time. Go ahead and add a few more lines each time it fetches a number from the range:

Download: file
for i in range(20):
  print("New number:")
  print(i)
  print(i * 20)
  print(i * 100)

If you can print a sequential number 20 times, try to draw a rect() instead using this number as a variable

Download: file
size(300, 300)
for i in range(20):
  print(i)
  rect(i*10, i*10, 20, 20)

With each new number, the same line of code is being drawn, rect(i*10, i*10, 20, 20), but the variable i changes each time.

Remember that the four numbers in the rect() function are for horizontal position, vertical position, width and height. So, the first rectangle is drawn at position 0*10, 0*10, which computes to 0, 0. The second rectangle is drawn starting at 1*10, 1*10, or 10, 10, and so on.

Try adding a fill() color that uses the variable i to see the color change with each new step:

Download: file
size(300, 300)
for i in range(20):
  print(i)
  fill(i/20, 0, 1)
  rect(i*10, i*10, 20, 20)

Each rectangle is 100% of the blue component, 0% of the green component, but has some fractional percentage of red each step of the way.

The fun part about having this code written is you can make small changes to the variables to get quick variations on the same drawing.

For example, changing the width and height values of the rectangle to 100, 100:

Download: file
size(300, 300)
for i in range(20):
  print(i)
  fill(i/20, 0, 1)
  rect(i*10, i*10, 100, 100)

Or, make the width and height be based on the number taken out of the range:

Download: file
size(300, 300)
for i in range(20):
  print(i)
  fill(i/20, 0, 1)
  rect(i*10, i*10, i*10, i*10)

The first rectangle is actually 0, 0 in width and height, so it didn't even show up! But You can see where this is going, it can be a lot of fun to build ot a very simple program of only a few lines, and then spend time tweaking the numbers to get something surprising and different than what you had in mind.

Let's go one step more complex, I'll use one for loop within another loop to get two numbers for the horizontal and vertical positioning of the rectangles.

Download: file
size(300, 300)
for x in range(10):
  for y in range(10):
  	print(x, y)
  	rect(20*x, 20*y, 15, 15)

We have two number generators, each making numbers in a range of 0 to 9 (for a total of 10 numbers in the range). You can see the numbers it's choosing with the print statement — the first time through x is equal to 0 for as long as it takes the second counter to count through all ten numbers. Then, the x steps up to 1 and the loop continues.

Go ahead and set a fill() color that also uses these x and y values, for example make the rectangles more red as the x value goes up:

Download: file
size(300, 300)
for x in range(10):
  for y in range(10):
  	print(x, y)
    fill(x/10, 0, 0.5)
  	rect(20*x, 20*y, 15, 15)

...or more green as the y value goes up, which might help illustrate what's happening:

Download: file
size(300, 300)
for x in range(10):
  for y in range(10):
  	print(x, y)
    fill(0, y/10, 0.5)
  	rect(20*x, 20*y, 15, 15)

Or a little bit of both!

Download: file
size(300, 300)
for x in range(10):
  for y in range(10):
  	print(x, y)
    fill(x/10, y/10, 0.5)
  	rect(20*x, 20*y, 15, 15)

Nice! But don't stop there, keep messing around with the numbers in your code and see what happens:

It can be fun to introduce some randomness into your code.

To have Python generate random numbers, first import random at the top of your script, which enables you to use several different random functions. We'll use the random.random() function which generates a random number between 0 and 1. Try printing some random numbers:

Download: file
import random

for i in range(20):
  print(random.random())

Each time through, the random number is never less than 0 and never greater than 1. This can be really useful, for instance if we introduce a new concept of doing one thing when this random value is greater than 0.5 and doing something else when the value was less:

Download: file
import random

for i in range(20):
  if random.random() > 0.5:
    print("Yes!")
  else:
    print("No!")

Half of the time one thing happens, and the other half of the time another thing happens. Let's bring this concept back into our drawing code, try drawing a rect() half of the time and an oval() the other half:

Download: file
import random
size(300, 300)
for x in range(10):
  for y in range(10):
    print(x, y)
    fill(x/10, y/10, 0.5)
    if random.random() > 0.5:
      rect(30*x, 30*y, 20, 20)
    else:
      oval(30*x, 30*y, 20, 20)

Keep running the code over and over to see it change, since the random numbers will be different each time you run the script.

These random numbers between 0 and 1 can be very useful, remember that the color values also need to be between 0 and 1. Try asking for three unique random numbers for each color component:

Download: file
import random
size(300, 300)
for x in range(10):
  for y in range(10):
    fill(random.random(), random.random(), random.random())
    oval(30*x, 30*y, 45, 45)
This guide was first published on Dec 14, 2018. It was last updated on Dec 14, 2018.
This page (Looping to make a more complex image) was last updated on Oct 25, 2020.