Turtle Recap for Learning Python Programming Essentials

Turtle Recap for Python Programming Essentials

Some people think turtle is such a waste of time. We don’t find these claims to be mature or grounded.

Most people only use turtle as a constructive fun activity or means to practice. With its visual output it’s probably reinforcing the learning process in the brain too.

Do you remember how you learned swimming? Hopefully, you know how to swim since it can be a life saver, literally. You do not go around thinking oh I didn’t pass across Atlantic Ocean when I was learning swimming today do you? So, when we talk about turtle it’s just a beautiful stepping tone in a beautiful learning process.

Also at the bottom of this article you can see a list of free Python Exercises that are relevant to the programming concepts discussed here. It’s always a good idea to supplement your programming skills.

So, what can you practice with turtle? Here are some ideas:

  • Loops
  • Python Operators
  • Conditional Statements
  • User-defined Functions
  • Classes
  • Data Types
  • User Input

How to start drawing with Python Turtle (Basics)

Let’s start with baby steps and build up. To draw with turtle we will need these steps:

  1. import turtle
  2. create and assign a turtle instance
  3. make turtle rotate and move using:
    • forward or backward
    • left or right
  4. terminate turtle operation (for convenience)
import turtle

t = turtle.Turtle()

t.forward(25)
t.left(60)
t.forward(45)


turtle.done()
Turtle drawing basics with Python

Now we know how to create turtle and make it move as we want, let’s get a little more creative.

Turtle pattern with different colors

Does it look like a uterus or is it just me? It’s so amazing how geometry is embedded in life whether it’s a plant seed, a flower, a bird’s wings or supermassive galaxies.

Anyway, let’s stay focused, we see 6 repetitive patterns in the image, let’s see what involves creating one single piece of the pattern.

Learning fundamental programming concepts with Python Turtle

When a person is learning programming, there are a few main concepts that are parallel in different programming languages. These concepts are not only much more simplified and syntax-minimal in Python but also there is a fancy drawing library that can be used to demonstrate each one of these vital concepts.

Loops (For & While)

Code snippet below includes a simple for loop to create a snail shell like spiral shape.

  • It uses 40 iterations with the help of range function: (range(40))
  • turtle steps are increasingly smaller. This is made possible by taking a fixed value; 15 and subtracting one third of the iteration number from it: t.forward(15-i/3)
  • Finally, turtle is made turn left with increasingly higher angles, again using the help of iteration number (i) (t.left(i*2+i/9))

You can read more about for loops here.

import turtle

t = turtle.Turtle()

for i in range(40):
    t.forward(15-i/3)
    t.left(i*2+i/9)

turtle.done()
Simple Turtle Spiral

Data Type Conversion (Dictionaries, Strings, Integers and Lists)

Let’s look at the same example by using strings of numbers, which will provide preparation for user inputs.

import turtle

t = turtle.Turtle()

parameters={"iterations":"40","fixed_step":"15"]
for i in range(int(parameters["iterations"])):
    t.forward(int(parameters["fixed_step"])-i/3)
    t.left(i*2+i/9)

turtle.done()

Here are refresher lessons about Python Type Conversion, Python Dictionaries, Lists, and strings.

Turtle with different parameters

User Input (input function)

User input is a cool way to get user’s input in Python. One caveat is that it always returns values in strings. This means regardless of what the user enters, letters, numbers or symbols, it will be returned in string format as below:

  • “John”
  • “45”
  • “&^*$”

In case you’re looking for a numerical input from the user you might need to convert it to int format in order to use it.

Here is a lesson about User Inputs with Python.

import turtle

parameter=input("Please enter iteration amount")
t = turtle.Turtle()

for i in range(int(parameter)):
    t.forward(15-i/3)
    t.left(i*2+i/9)

turtle.done()

Code above demonstrates a user input connected to a turtle which operates with a loop. Int function is also used to convert input’s string value to integer. At least 4 major concepts in a small turtle example. Let’s say user entered 10 after he was prompted “Please enter iteration amount”, you’d get the shape below:

Turtle for loop after 10 iterations

Let’s make another turtle example with user input. Let’s add some color to the turtle using .color() method.

import turtle

parameter=input("Please enter a color")

t = turtle.Turtle()
t.color(parameter)

for i in range(40):
    t.forward(15-i/3)
    t.left(i*2+i/9)

turtle.done()

In the event that user enters brown, we will get a shape similar to this:

Turtle for loop after 10 iterations

Python Operators (if, elif, else)

We’ve already used Python operators many times up to this point in this article. Operators mainly used are assignment operator (=) and some of the arithmetic operators (+, -, *, /)

You can see a great detail about Python Operators in this lesson here.

Lists (Color Scale Example)

It can be exciting to change turtle’s color on the go so we can end up with multi-color drawings.

import turtle

t = turtle.Turtle()
lst=["red", "blue","brown","pink","gray","yellow"]

for i in range(40):
    t.forward(15-i/3)
    t.left(i*2+i/9)
    t.color(lst[i%6])
    
t.left(-120)
for i in range(40):
    t.forward(15-i/3)
    t.left(i*2+i/9)
    t.color(lst[i%6])

t.left(-120)
for i in range(40):
    t.forward(15-i/3)
    t.left(i*2+i/9)
    t.color(lst[i%6])

t.left(-120)
for i in range(40):
    t.forward(15-i/3)
    t.left(i*2+i/9)
    t.color(lst[i%6])

turtle.done()
Multicolor turtle pattern

User Defined Functions (def t_draw():)

So, as you can notice from the last piece of code above, it’s starting to get messy already. We just created 4 turtle loops, added some color and that was enough for the code to start getting repetitive and tedious.

This is brilliant though for the sake of demonstration. Functions and classes are superb object structures that can help us create repeatable code with structure.

let’s attempt to create a function that creates 1 piece of our turtle pattern above:

 

import turtle

t = turtle.Turtle()

def t_draw():
    lst=["red", "blue","brown","pink","gray","yellow"]
    for i in range(40):
   
        t.forward(15-i/3)
        t.left(i*2+i/9)
        t.color(lst[i%6])
    t.left(-120)


t_draw()
t_draw()
t_draw()
t_draw()
turtle.done()

Let’s play with the color algorithm a little bit so that instead of every turtle step, color changes at every different loop.

We will add a parameter called “j” to the function t_draw and call the function with different j values pointing to the list of colors.

import turtle

t = turtle.Turtle()    
def t_draw(j):
    lst=["red", "blue","brown","pink","gray","yellow"]
    for i in range(40):        
        t.forward(15-i/3)
        t.left(i*2+i/9)
        t.color(lst[j])
    t.left(-120)

t_draw(0)
t_draw(1)
t_draw(2)
t_draw(3)

turtle.done()
Multicolor turtle pattern

Conditional Statements (if, elif, else) (Color / B&W)

Now let’s implement a conditional statement using if and else.

Let’s make a user input so that user has 2 options:

bw: (standing for black&white)

c: (standing for color)

import turtle

t = turtle.Turtle()

def t_draw(j, c_mode):
    lst=["red", "blue","brown","pink","gray","yellow","black"]
    for i in range(40):        
        t.forward(15-i/3)
        t.left(i*2+i/9)
        if c_mode=="bw":
            t.color(lst[-1])
        else:
            t.color(lst[j])
    t.left(-120)

c_mode=input("Please make a choice: 'bw' or 'c'")
t_draw(0,c_mode)
t_draw(1,c_mode)
t_draw(2,c_mode)
t_draw(3,c_mode)

turtle.done()

Lesson for Python Conditional Statements.

Python Classes

We’ll leave this one to you. Can you think of ways to implement a user defined class to make this turtle example even more sophisticated?

Feel free to brush up on your Python class knowledge with these class exercises here.

Finishing Thoughts

So, turtle can be a fun way to learn Python. It’s good to keep in mind that turtle is rather a learning concept than a tool to build things. But so what, as long as you have a purpose and aligned expectations there is nothing wrong with some fun turtle time while incredibly boosting your learning performance!

If you know what you want, if you know what turtle is and if you know what you can get from it, it will never be a waste of time.

Maybe a good thing to understand is that we can’t expect a baby to write a literature masterpiece but the baby still has to learn, practice and have fun. So take the time you need while learning programming and let’s realize that we are lucky to have Python turtle and all the amazing tech opportunities we have in 21st century! Again, turtle is just another method to supplement our learning journey in a fun and creative way.

Enjoy!

Suggested Python Exercises (Relevant to this article)

Turtle Rotating Rectangles

Drawing Rotating Rectangles

Here is another Python turtle example showing how to edit the background color of turtle screen and how to use RGB mode.

This example can give you a good understanding of RGB channels in images.

Here is a simply code that uses requests, json and webbrowser libraries:

import turtle
import random

x = 1
SN = turtle.Screen()
SN.bgcolor("gray")
a = turtle.Turtle()
a.speed(100)

i = 0
while x < 256:

    r = random.randint(0,255)
    g = 100
    b = 150
    
    turtle.colormode(255)
    a.pencolor(i,g,b)
    if x<252:
        a.pensize(1)
        a.forward(50 + x)
        a.right(91.5)
       
    x = x+1
    i = (i+1)%255    

turtle.done()   

Code below will print a bunch of other information regarding cocktails such as: Instructions and Ingredients and it will also open the cocktail image in a browser.

import turtle

a = turtle.Turtle()
for i in range(240):
    a.forward(2+i/4)
    a.left(30-i/12)

turtle.done()

If you need a refresher on Python Turtle, here is a link to our Python Turtle lesson.

Turtle Turbines

Turtle Turbines

You can draw nice turbines with Python turtle with the codes in this tutorial.

We will explain how you can twist to code to give more flavor to your drawing as well.

Basic turtle illustration - turbine
import turtle

t = turtle.Turtle()

for i in range(10):
    t.forward(200)
    t.right(120)
    t.forward(40)
    t.goto(0,0)
    t.left(84)

turtle.done()

Turtle Pensize

pensize() will let you control the thickness of the pen. Look at the code below:
Basic turtle illustration - turbine (pensize = 5)
import turtle

t = turtle.Turtle()
t.pensize(5)

for i in range(10):
    t.forward(200)
    t.right(120)
    t.forward(40)
    t.goto(0,0)
    t.left(84)
    
turtle.done()

Turtle Colors

Using color() you can give your pen a color. Using a list of predefined colors you can loop through the colors with a simple trick. % operator will give the remainder from a division.

colors[i%6] will make sure colors only takes an index from 1 to 6 so each color gets assigned and then the index goes back to zero when i reaches a multiple of 6.

Turtle spiral example with color iteration
import turtle

colors = ["red", "blue", "green", "gray", "orange", "black"]
a = turtle.Turtle()

for i in range(30):
    a.forward(20+i)
    a.left(30 - i/1.5)
    a.color(colors[i%6])

turtle.done()
Basic turtle illustration - turbine (iterating colors)
import turtle

t = turtle.Turtle()
colors = ['red', 'blue', 'orange', 'green', 'black', 'brown']

for i in range(10):
    t.color(colors[i%6])
    t.forward(200)
    t.right(120)
    t.forward(40)
    t.goto(0,0)
    t.left(84)

turtle.done()

Combining codes

Let’s add a post to our turbine so it can rotate one day.

Basic turtle illustration - turbine (with the post)
import turtle

t = turtle.Turtle()
colors = ['red', 'blue', 'orange', 'green', 'black', 'brown']
t.pensize(20)

t.goto(0,150)
t.right(90)
t.color('gray')
t.forward(470)

t.pensize(3)

t.penup()
t.goto(0,150)
t.pendown()

for i in range(10):
    t.color(colors[i%6])
    t.forward(200)
    t.right(120)
    t.forward(40)
    t.goto(0,150)
    t.fillcolor("black")
    t.left(84)



turtle.done()

Basic turtle illustration - turbine (more elegant shapes)
import turtle

def turbase(thickness):

    t = turtle.Turtle()
    t.pensize(thickness)
    t.color('lightgray')
    t.penup()    
    t.goto(4,0)
    t.pendown()
    t.right(90)
    t.forward(400)


def turwings(thickness, startingdegree):
    t = turtle.Turtle()
    t.pensize(thickness)
    colors = ['red', 'blue', 'orange', 'green', 'black', 'brown']    
    t.color("darkblue")
    i=30
    
    t.penup()
    t.forward(20)
    t.pendown()
    t.right(startingdegree)
    t.forward(300)
    t.left(174)
    t.forward(300)
    
    
    t.right(45)
    t.forward(300)
    t.left(175)
    t.forward(300)
    
    
    t.right(70)
    t.forward(300)
    t.left(175)
    t.forward(300)
    
    t.fillcolor('red')    
turbase(20)
turwings(3, 257.5)
turtle.done()

If you enjoyed this Python turtle lesson, feel free to spread it.

Turtle Spirals

Turtle Spirals

Drawing with Python Turtles can be a lot of fun! You can draw nice turbines with Python turtle with the codes in this tutorial.

We will explain how you can twist the code to give more flavor to your drawings and practice coding while drawing or vice versa, who knows 🙂

Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Simple Turtle Spiral
import turtle

a = turtle.Turtle()

for i in range(100):
    a.forward(5+i)
    a.right(15)

turtle.done()

Turtle Step and Direction Adjustment (forward, backward, left, right)

Let’s make the spiral more dense with decreasing the steps ( a.forward(2+i/4) and
a.left(30-i/12)) and increasing the amount of turns (for i in range(240):).

Turtle Spiral with denser pattern
import turtle

a = turtle.Turtle()
for i in range(240):
    a.forward(2+i/4)
    a.left(30-i/12)

turtle.done()

Smaller Loop (Fibonacci Sequence)

Fibonacci sequence is an interesting number sequence, sometimes referred to as golden ratio, that can be traced in many natural patterns in universe such as flowers, shells, snails, trees, leaves, storms, galaxies and fingerprints. 

Here is an attempt to draw on Fibonacci proportions. Not exactly it, but close.

Turtle Spiral - Shell or Fibonacci Shape
a = turtle.Turtle()

for i in range(30):
    a.forward(20+i)
    a.left(30 - i/1.5)

turtle.done()

Turtle with Colors

You can escape Black & White or Mono-Color drawings by implementing .color() method of turtle.

Mono-color Turtle Drawing (Only implements one color throughout)

Let’s add some color to our Turtle drawings. a.color(colors[0])
Turtle Spiral - Shell or Fibonacci Shape (red colored)
import turtle

colors = ["red", "blue", "green", "gray", "orange", "black"]
a = turtle.Turtle()

for i in range(30):
    a.forward(20+i)
    a.left(30 - i/1.5)
    a.color(colors[0])

turtle.done()

Multi-color Turtle Drawing (Navigates through a list of different colors)

Another fun idea is iterating through different colors. This can be easily achieved by defining a set of color and some basic loop iteration with turtle.

Color list is iterated using the help of “Modulus” operator (%) in Python. If you’d like to read an extensive article about Python operators including Modulus you can click here.

a.color(colors[i%6])

Turtle Spiral - Shell or Fibonacci Shape (rainbow)
import turtle

colors = ["red", "blue", "green", "gray", "orange", "black"]
a = turtle.Turtle()

for i in range(30):
    a.forward(20+i)
    a.left(30 - i/1.5)
    a.color(colors[i%6])

turtle.done()

That’s it. What you can do with Python Turtle is up to your imagination, so there is no limit. Try something that’s relevant to you and enjoy practicing!

If you find turtle interesting we have a very extensive tutorial that explains different Python concepts (such as if-else, user functions, user input, operators, data types, loops etc.) through turtle here: Python Turtle Tutorial.

ps: Don’t forget to include turtle.done() in the end so your turtle window can be terminated.

pss: We recommend Spyder IDE which comes with Anaconda Open Source All-in-One installation solution. Although it’s an IDE specialized in scientific applications it’s also perfect for Python practice. You can read more about effortless Python Installation here.