Master Python

First steps

3 / 253
Contents

Contents

Master Python

0 of 253 complete

6 min read

One line at a time, top to bottom

How a program with several instructions in it is actually read.

A program with several instructions is read the way you read a list. Start at the top, do the first thing, then the next, and carry on until there is nothing left. Nothing runs early, nothing runs twice, nothing is quietly skipped.

That sounds obvious, and it is still worth seeing, because a good deal of confusion later comes from imagining that the computer looked ahead or worked something out in advance. It did not. It was on line one, and then it was on line two.

Try CodePython

Swap the first and third lines and run it again. The output follows the order of the lines, not the meaning of the words.

What the computer is doing, line by lineThe arrow marks the instruction being carried out. The output builds up underneath.

Step 1 of 4. Line 1

->  print("Fill the kettle.")
    print("Boil the water.")
    print("Pour the tea.")

Output so far:
Fill the kettle.

The first instruction runs and writes one line of output.

Each print finishes its own line, so three instructions give three lines of output. When you want a blank line, ask for a print with nothing at all in the brackets.

Try CodePython

Add a second print() between the two items and run it again. Then take every blank line out and compare how the two look.

One line of code does not have to mean one short line of output, either. A long message is still a single instruction, however much text is inside the quotation marks.

Try CodePython

Make the first message twice as long and run it again. Two instructions still give two lines of output.

What does this write out? print("A") then print() then print("B"), on three lines.

A line reading A, then an empty line, then a line reading B. The middle instruction has nothing to write, so it writes nothing and then finishes its line, which is exactly what a blank line is.

All lessons in Master Python