Skip to content

Variables for Your Drawing

By the end of this lesson you'll be able to:

  • Store a number in a variable and use it in your code
  • Change the size of a square by editing just one line
  • Explain why variables make programs easier to modify

In the simple square lesson, the numbers 100 and 90 appeared eight times each. If you wanted a bigger square you had to change eight lines. Variables let you write the number once and use it everywhere — change the variable and the whole drawing changes.

Welcome to This Lesson!

Monty waving welcome Ready to make your code smarter? Today I'll show you how variables work — and why every programmer uses them. Let's code it together!

What Is a Variable?

A variable is a named box that holds a value. You create one by writing a name, an equals sign, and the value:

1
distance = 100

After this line, Python remembers that distance equals 100. Whenever you write distance later in your code, Python uses 100 in its place.

Give variables descriptive names — distance is much clearer than d or x.

Scratch Bridge

Monty with a tip In Scratch you used the "Set [distance] to 100" block in the Variables drawer. In Python, distance = 100 does exactly the same thing: it creates a variable called distance and stores the value 100 in it.

Sample Code

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

distance = 100
angle = 90

monty.forward(distance)
monty.right(angle)

monty.forward(distance)
monty.right(angle)

monty.forward(distance)
monty.right(angle)

monty.forward(distance)
monty.right(angle)

What Do You Think Will Happen?

Monty thinking The turtle uses distance for every forward() call and angle for every right() call. What shape will it draw? Make your guess — then click Run to find out!

Try It Now

Edit the code below and click Run to see Monty draw right on this page. No account needed — everything runs in your browser.


  

Were you right? With angle = 90, each turn is a quarter-circle, so the turtle draws a perfect square.

If you see a red error message: Check that your variable name is spelled exactly the same everywhere — distance and Distance are two different names in Python.

How It Works

Python reads the file from top to bottom. When it reaches distance = 100, it stores the value 100 in memory under the name distance. Every time it later sees forward(distance), it looks up that stored value and uses it — just like reading a note you left yourself.

Explanation Table

Line What it does
distance = 100 Creates a variable named distance and stores 100 in it
angle = 90 Creates a variable named angle and stores 90 in it
monty.forward(distance) Moves forward 100 steps (reads the variable's value)
monty.right(angle) Turns 90 degrees right (reads the variable's value)

Common Mistake: Typo in Variable Name

Monty warning If you write distanse on one line and distance on another, Python treats them as two different variables — and the second one has no value yet. You'll see a NameError. Python is case-sensitive too: Distancedistance.

Learning Check

Spot the Bug!

Monty warning The program below should draw a square, but it crashes with a NameError. There's a typo in one variable name. Can you find it and fix it?


  

Experiments

Try these changes to the code above. Predict what will happen first, then run it to check!

  1. Change distance = 100 to distance = 150. You'll know it worked when the square is noticeably larger than before.

  2. Change distance = 100 to distance = 40. You'll know it worked when the square is about half as big.

  3. Change angle = 90 to angle = 120 and reduce the four pairs to three pairs of forward/right. You'll know it worked when you see a triangle instead of a square.

  4. Add monty.color('purple') just before the first forward line. You'll know it worked when all four sides of the square are purple.

  5. Add a third variable: monty.pensize(thickness) and set thickness = 5 at the top. You'll know it worked when the lines are noticeably thicker, and you can change thickness in one place to affect all four sides.

Great Work!

Monty celebrating You've unlocked variables — one of the most powerful ideas in all of programming! In the next lab you'll combine variables with a for loop so you don't have to repeat those four lines at all. Let's code it together!