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

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. We see 6 repetitive patterns in the image, let’s see what involves creating one single piece of the pattern.

AJ Gentile has a very impressive and informative video suggesting we are in a simulation and the it’s no coincidence some ratios such as Fibonacci sequence appears in so many places in nature because it is how life was coded by the creator of the simulation.

The video mentions how researchers from University of Washington were able to discover methods to embed computer codes in DNA.

The video below is embedded in this page from the time it mentioned the code in life.

Computer code writ in the fabric of the cosmos...

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)

Recommended Posts