What you are looking at
A program is a set of instructions. The editor is where you type the instructions. The output panel is where you see anything the program prints. Clicking Run Python starts the program from the top. Editing the code does not change the previous output until you run it again. You can repeat a run to check that the same code produces the same result.
Start with three ideas. A value is a piece of data, such as the number 4 or the text "four". A variable is a name you use to refer to a value. An expression is something Python evaluates to produce a value, such as 4 + 2. On the line points = 4 + 2, Python first works out the expression, then gives the result the name points. This is assignment. It reads from right to left conceptually, although the line is written with the name on the left.
Read a small program before running it
points = 4
points = points + 2
print("Points:", points)
print("points")
| After line | Value of points | New output |
|---|---|---|
| 1 | 4 | Nothing yet |
| 2 | 6 | Nothing yet |
| 3 | 6 | Points: 6 |
| 4 | 6 | points |
Read line 2 as “take the current points, add two and store the new result”. It would make no sense as an equation in maths, but here it updates the variable. On line 3 Python evaluates the unquoted name and prints its value. On line 4 it sees the literal word between quotes. The comma in print separates arguments. Python inserts a space between them by default.
Get comfortable in the sandbox
- Read the starter and say what you expect it to print. Run it once without editing.
- Change only one value. Predict which output line will change. Run it and compare.
- Delete a closing quotation mark and run it. The final line of the message will identify a syntax error. Fix the quote and run again. Later, try spelling a variable differently in the print call. That is a different problem because Python can read the line but cannot find that name.
- Use Restore starter if you want to begin again. Copy your code before leaving the lesson. The introduction exercise checks for its own exact target, so a different program can run correctly without passing that particular check.
Practise a Paper 2 habit
Read before you edit. Suppose you are given the code above and asked to show Points: 9 on the first output line while keeping the starting value at 4. Identify the one line that needs changing. Then predict the two output lines before running your version. Record what you changed and why.