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 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.
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.
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.
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:
score. It is 4.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.
+ 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.
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.
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.