Course 1.2Lesson 2 of 5
A new value of a variable can be worked out from its old value: score = score + 10. The short forms +=, -=, *= and /= do the same. The order of the lines matters, so we follow a program with a trace table, one row for each line. To swap two variables we keep the first value in a third variable, or we write a, b = b, a. A swap written as a = b and then b = a does not work, because the first value is lost.
Try the code and the simulations beside the lesson.
After this lesson you can
In a game, the score does not stay at one value. It goes up and down. Look at how a score can change.
The program then uses four short forms. They do exactly the same job as the longer form.
| Short | Same as | Meaning |
|---|---|---|
score += 5 | score = score + 5 | add 5 |
score -= 4 | score = score - 4 | take away 4 |
score *= 2 | score = score * 2 | multiply by 2 |
score /= 2 | score = score / 2 | divide by 2 |
A counter is a variable that goes up by one each time something happens. Almost every program has one: the number of visitors, the number of wrong answers, the number of rounds.
Python does the lines in order, from the top to the bottom, and each line sees the values that the lines above it have made. Compare these two programs in your head.
score = 0, then score += 10, then score *= 2 gives 20.score = 0, then score *= 2, then score += 10 gives 10.The same two changes, in a different order, give a different answer. This is why a programmer must be able to follow a program in order, and not guess.
Here is a short program. What does it show? Guess first, and then follow it with the table.
Do not run it yet. Write the table on paper, with one row for each line, and one column for x and one for y. Then check your table against this one.
| Line | x | y |
|---|---|---|
x = 3 | 3 | not made yet |
y = 5 | 3 | 5 |
x = x + y | 8 | 5 |
y = x - y | 8 | 3 |
x = x - y | 5 | 3 |
print(x, y) | shows 5 3 |
Now run the program. Was the answer the same as your table? A trace table is the best tool you have for finding a mistake in your own program. It makes you do the job of the computer, slowly.
Two cups hold two drinks. You want to put the tea in the other cup, and the milk in this cup. You cannot pour both at once. You need a third, empty cup. Variables work in the same way.
First, look at a swap that does not work.
Now the two correct ways.
Python programmers use Way 2. Way 1 is how almost every other language does it, and it shows the idea clearly. Learn both.
Without temp, the line a = b would throw away the first value of a. That is why the first swap program did not work.
score + 10 on a line by itself works out a number and throws it away. You must write score = score + 10.visits = 0 must come before the lines that change it. If it comes after, the counter starts again.a = b first, the old a is gone. Save it first.b to follow a. A variable keeps the value that it got. It does not change when another variable changes.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
score = score + 10 works out the right side first, and then stores the answer in the name on the left.+=, -=, *= and /= are short forms of the same thing.a, b = b, a.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
n = 4 and n *= 3, what is n?count += 1 runs five times. What is count?a and b hold after a = 2, b = 9, a = b and b = a?x = x + 1 and x + 1 on its own line?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.