Build with Python

Values and Variables

On this page

Values and Variables

In the last module every number in your program was typed where it was used. To move the robot's head you changed one line, and then had to change every eye by hand. This module gives a value a name, so that one change moves everything that depends on it.

Before this, you can: write a call with its arguments in the right order; find a point from its x and y; read which line an error message points at.

By the end you will be able to work out an expression by hand, say what value every name holds after each line of a short program, and draw a picture that is controlled by a few names at the top.

Python as a calculator

Python works out arithmetic wherever it finds it. The canvas shows shapes; to see a number you need a second window. print(...) shows a value in the output panel, so you can ask Python what it got:

print(3 + 4)
print(10 - 2 * 3)
print((10 - 2) * 3)
print(10 / 4)
7
4
24
2.5

* means multiply and / means divide. Look at the second line: the answer is 4, not 24. Python multiplies and divides before it adds and subtracts, as you were taught at school. Parentheses go first of all, which is why the third line gives 24. And 10 / 4 gives 2.5, not 2: dividing with / does not throw the fraction away.

An argument can be arithmetic too. Python works it out and then makes the call:

from scimigo import canvas, rectangle

canvas(600, 400)
rectangle(100, 150, 2 * 80, 80, color="teal")

The width is 2 * 80, so the box is 160 wide: twice as wide as it is tall.

A name for a value

width = 160
print(width)
print(width / 2)
160
80.0

width = 160 makes a variable: a name with a value. Read the line from right to left: take the value 160, and give it the name width. From then on, wherever Python meets width, it uses 160.

The = sign here does not mean what it means in maths. It is an instruction: store this value under this name. The name always goes on the left.

Names are for you. Python would be equally happy with w or thing7, but width tells the next reader, who is usually you a week later, what the number is for. A name has no spaces; use an underscore, as in head_x.

Here is the point of it. One name, used in three places:

from scimigo import canvas, rectangle, circle

canvas(600, 400)
head_x = 200
rectangle(head_x, 100, 200, 180, color="orange")
circle(head_x + 60, 170, 15, color="green")
circle(head_x + 140, 170, 15, color="green")

Change head_x = 200 to head_x = 50 and the head and both eyes move together, because every position is worked out from that one name.

Names do not follow each other

This is the idea in this module that surprises people most.

a = 5
b = a + 2
a = 10
print(b)
7

It prints 7, not 12. When Python reached b = a + 2, it looked up a, found 5, worked out 7, and stored the number 7 under the name b. It did not store "whatever a is, plus 2". Later a got a new value, and b knew nothing about it.

An assignment stores the value the right-hand side has at that moment. It does not store the formula, and it is not worked out again later. For the numbers and text in this module, that means changing one name never changes another.

Read, work out, store

A name can be given a new value, and the new value can be worked out from the old one:

score = 4
score = score + 1
print(score)
5

As maths, score = score + 1 is nonsense: nothing equals itself plus one. As an instruction it is three small moves, always in this order:

  1. Read the value of score. It is 4.
  2. Work out the right-hand side: 4 + 1 is 5.
  3. Store 5 under the name score. The 4 is gone.

When you step through this program in the lab, you see score as 4 before the line runs and as 5 after it. The three moves all happen inside that one step.

The same three moves let a picture change as the program goes on:

from scimigo import canvas, circle

canvas(600, 400)
size = 20
circle(120, 200, size, color="purple")
size = size + 20
circle(280, 200, size, color="purple")
size = size + 20
circle(470, 200, size, color="purple")

The three circle lines are the same apart from x. They draw different sizes because size has a different value each time Python reaches one.

Number or text?

+ adds numbers. Between two pieces of text it does something else: it joins them.

print(3 + 4)
print("3" + "4")
7
34

"3" in quotes is text, a single character, so "3" + "4" is the text 34, the two characters side by side. This catches people when a number arrives as text: the program runs, there is no error, and the answer is wrong.

Mixing the two kinds is an error, and the message names both:

print("3" + 4)
  File "your_program.py", line 1
    print("3" + 4)
TypeError: can only concatenate str (not "int") to str

Python will not guess whether you meant 7 or 34. The message says which two kinds of value it was asked to combine; the fix is to make them the same kind.

Use print to look inside

You have used print since the first section to see a value. Give it several things separated by commas and it shows them on one line with spaces between, which lets you label what you print:

coins = 5
print("coins", coins)
coins = coins + 3
print("coins", coins)
coins 5
coins 8

When a picture comes out wrong, printing the values that went into it is the fastest way to find out why. Put a print just before the line you doubt, with a label so you know which line spoke.

In the lab

You will predict what short programs leave in their variables before running them, move a robot by changing one number, draw a sunset that follows three variables, and work out a receipt with no picture at all.

Your turn

Now try it yourself

Predict what a program will do, run it, and step through it line by line. The checks tell you when your own code works.

Loading the lab…

In this lab

  1. What does it work out?

    Python works out each of these before it prints. Decide the answer first.

  2. What is shown?

    Three print lines. One of them has quotes round the arithmetic. Decide what appears in the output panel.

  3. What is b now?

    Read this carefully, one line at a time. Decide what the last line prints.

  4. One number moves the robot

    Every position in this robot is worked out from head_x and head_y.

    1. Change head_x to 50 and run it. The head and both eyes move together.
    2. One eye uses the variable eye_color. The other has its colour typed in. Change the program so that both eyes use eye_color. Then changing that one line recolours both.
  5. Read, work out, store

    Each line after the first uses x to work out a new value for x. Choose the table that shows x after each line.

  6. Bigger every time

    Three circles, all drawn with size, so all the same. Make each circle 20 larger than the one before by changing size between the calls. Do not type new numbers into the circle lines.

    The check also tries a different starting size, so the second and third sizes have to be worked out from the first.

  7. Number or text?

    The + sign does different things to numbers and to text. Decide what each program does.

  8. What is printed?

    Two print lines with an update between them.

  9. Sunset

    Draw a sunset, controlled by the three variables at the top.

    • The sky is a rectangle that fills the whole canvas. Draw it first.
    • The sun is a "gold" circle with its centre at (sun_x, sun_y) and radius sun_size.
    • The sea is a rectangle covering everything below y = 260, from the left edge to the right edge. It is in front of the sun, so a low sun is partly hidden.

    The checks try other values of the three variables. The sun should follow them; the sky and the sea should not move.

  10. Receipt

    Three prices and a tax percentage are given. Work out:

    • the subtotal: the three prices added together;
    • the tax: that percentage of the subtotal;
    • the total: the subtotal plus the tax.

    Print exactly three lines, in this order. The first starts with the word subtotal, the second with tax, the third with total, and each has its amount after the word. Then write the total on the canvas with text.

    With the prices given, the subtotal is 40, the tax is 4 and the total is 44. The checks try other prices, and they also change the percentage on its own.

  11. Recipe scaler

    A pancake recipe for 4 people uses 300 grams of flour, 2 eggs and 500 millilitres of milk.

    Print the amounts needed for people people. Print three lines: the first starts with the word flour, the second with eggs, the third with milk, each followed by its amount. Half an egg is fine.

    The checks try other numbers of people. No picture in this one.

  12. Stack of boxes

    Draw three square boxes stacked on top of each other, standing on the ground line y = 380.

    • Every box is size wide and size tall, with its left edge at x = 250.
    • The first box stands on the ground. The second stands on the first, and the third on the second.

    Keep a variable for the top of the box you are about to draw, and update it between the calls. The checks try other sizes.

  13. The swap that loses a value

    This program is meant to swap the values of a and b. Run it: both end up the same.

    Fix it, so that afterwards a holds what b started with and b holds what a started with. You will need a third name. Before you fix it, work out which value is lost, and on which line.

  14. Twelve plus three

    You live at number 12. Your friend lives three doors further along, and this program is meant to print their house number, 15. It prints 123.

    Fix it. There is no error message to help: work out why Python was happy to print 123.

  15. A picture with three dials

    Make a drawing of your own that is controlled by at least three variables at the top of the program: for example a position, a size and a colour. A face, a car, a tree, a rocket: your choice.

    There are no checks. When it works, go through the list below.