Turtle Graphics In Python? Cool…

I just learned that Python has Turtle graphics built in.  Very cool.  How did I not know about this before?  Now I guess I know what Maggie’s first programming language will be. Inspired by this nice post showing how to draw fractal trees, here’s a quick Sierpinsky triangle algorithm in Python:

import turtle

def striangle(depth,base):
   turtle.down()
   if depth == 0:
      turtle.fill(1)
      for i in 0,1,2:
         turtle.forward(base)
         turtle.left(120)
      turtle.fill(0)
   else:
      for i in 0,1,2:
         striangle(depth-1,base)
         turtle.up()
         turtle.forward(base*2**depth)
         turtle.left(120)
         turtle.down()

turtle.reset()
striangle(6,5)
turtle-sierpinski
Depth 5 Sierpinski Triangle

Fun!

Leave a comment