Skip to content

Variables

Deprecated Lab — Trinket is shutting down

This lab was written for Trinket.io, which is shutting down in August 2026. The embedded trinket.io links on this page will stop working after that date.

These pages are kept for reference only. The current version of this course now runs every lab as an inline Skulpt editor right in the page — no account or install needed. Start at Chapter 1: Welcome to Python.

Using Variables

In the simple square program we repeated the numbers for the distance and turning angle four times in four different places. If we wanted to change the size of our box we would have to change the code in four different places. By using variables we can make our program easier to change.

In this example program we will make the turtle go forward 40 steps and then make a right turn of 90 degrees. We will repeat this four times to complete a square.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import turtle
dan = turtle.Turtle()
dan.shape('turtle')

# let's just put these in one place to make our program easier to modify
distance = 50
angle = 90

dan.forward(distance)
dan.right(angle)

dan.forward(distance)
dan.right(angle)

dan.forward(distance)
dan.right(angle)

dan.forward(distance)
dan.right(angle)

dan.write('done with square')

Run Square With Variables

Experiments

Can you make the turtle draw a larger square? Hint: change the forward(40) to be forward(100)