Python Turtle Shell

Are you ready to learn how to use Python’s Turtle Shell? This tool lets you create digital art using simple commands in Python. Let’s get started! First, import the turtle module:

# Import the turtle module
import turtle

# Create a new turtle object
t = turtle.Turtle()

# Set the speed of the turtle to 0 (fastest)
t.speed(0)

# Set the pen color to red
t.color("red")

# Move the turtle forward by 100 units
t.forward(100)

# Rotate the turtle by 90 degrees to the right
t.right(90)

# Move the turtle forward by 100 units
t.forward(100)

# Rotate the turtle by 90 degrees to the right
t.right(90)

# Move the turtle forward by 100 units
t.forward(100)

# Rotate the turtle by 90 degrees to the right
t.right(90)

# Move the turtle forward by 100 units
t.forward(100)

# Hide the turtle cursor
t.hideturtle()

# Exit the turtle program
turtle.done()

# The turtle module allows us to create digital art using simple commands in Python
# We create a new turtle object and assign it to the variable 't'
# The speed(0) function sets the speed of the turtle to the fastest possible
# The color() function sets the pen color to red
# The forward() function moves the turtle forward by the specified number of units
# The right() function rotates the turtle by the specified number of degrees to the right
# We use a combination of forward() and right() functions to create a square shape
# The hideturtle() function hides the turtle cursor from the screen
# The done() function ensures the turtle program stays open until we close it manually

Next, we need a window for our little robot (the “turtle”) to draw on. Create one with this command:

# Create a window for the turtle to draw on
window = turtle.Screen() # Creates a window object using the turtle module

# The window object has various methods that can be used to customize the window
# For example, we can set the background color of the window to white
window.bgcolor("white") # Sets the background color of the window to white

Now let’s bring out the Turtle! Use this line of code:

# Import the turtle module
import turtle

# Create a turtle object and assign it to the variable "tess"
tess = turtle.Turtle()

# The above line creates a turtle object and assigns it to the variable "tess"

# The turtle object can now be used to draw on the screen
# For example, we can use the forward() method to move the turtle forward by a certain distance
tess.forward(100)

# The above line moves the turtle forward by 100 units

# We can also use the right() method to turn the turtle to the right by a certain angle
tess.right(90)

# The above line turns the turtle to the right by 90 degrees

# Finally, we can use the done() method to keep the turtle window open until we close it
turtle.done()

# The above line keeps the turtle window open until we close it

We named our Turtle “tesst” because we’re not very creative, but you can name yours whatever you want. To make your Turtle move forward by 100 pixels, use this command:

# Create a Turtle object and name it "tess"
tess = Turtle()

# Move the turtle forward by 100 pixels
tess.forward(100)

# The "forward" method moves the turtle in the direction it is facing by the specified number of pixels. 
# In this case, the turtle named "tess" will move forward by 100 pixels.

To turn the turtle left or right instead of just moving forward, try these commands:

# This script uses the turtle module to turn the turtle left or right by a specified angle

# Import the turtle module
import turtle

# Create a turtle object named tess
tess = turtle.Turtle()

# Turn the turtle left by 90 degrees
tess.left(90)

# Turn the turtle right by 45 degrees
tess.right(45)

You can combine movements and turns to create more complex shapes! For example:

# This script uses a for loop to repeat a set of movements and turns three times, creating a triangle shape.

# Import the turtle module
import turtle

# Create a turtle object named tess
tess = turtle.Turtle()

# Use a for loop to repeat the following code three times
for i in range(3):
    # Move tess forward 100 units
    tess.forward(100)
    # Turn tess right 120 degrees
    tess.right(120)

This will make the Turtle move forward by 100 pixels, turn right by 120 degrees, and repeat that sequence two more times! The possibilities are endless with Python’s Turtle Shell you can create intricate designs or just have fun doodling. So go ahead, let your creativity run wild and unleash the artist within!

If you want to learn more about using Python for scientific computing, check out this article by Scott Shell: “An introduction to Python for scientific computing” (PDF). And if you’re interested in why Python is a great choice for research, read this article by Hoyt Koepke.

SICORPS