2 Sequence

The simplest Python program is a sequence of instructions, written one per line, and executed one by one from top to bottom. Our first program only has two instructions. Here is the program again, already run for you. (Remember to click the pencil icon if you can’t see the code.)

The first instruction is an assignment: the computer evaluates the expression 3 + 7 to the right of the assignment (=) and stores the result in the variable result on the left of the assignment. Each piece of data we need has to be stored in a variable. Translating Python to English, the assignment states ‘let result be the sum of 3 and 7’.

The second instruction, print, prints some text on the screen, followed by the computed result. Note the following:

2.1 The art of failure

The following details are important once you start writing code. Computers are not as smart and accommodating as human readers: at the slightest spelling mistake (like pint instead of print) or missing punctuation (forgetting the comma or double-quotes, for example), the computer will give up on understanding and running your code and will display an error message.

On top of that, most error messages are rather cryptic. Hence it’s best to get used to them, by doing errors on purpose, so that you have a clue what might be wrong when you are writing your own code.

Activity 2.1

Put double-quotes around the 7 on the first line and run the program. Make sure you type "7" and not ''7'': a double-quote character is not the same as two single-quote characters.

You will get an error message saying that there is a type error. This basically means the code is mixing data of different types, it’s mixing apples and oranges if you like. In this case, we are trying to add a number (3) to a string ("7") which of course doesn’t make sense.

Click on the X at the right end of the message to make it go away and then reset the program to undo the change.

Sometimes, code has more subtle errors, that lead to unexpected results. This is akin to miscommunication, when other people understand something different from what you intended to say. Here is an example.

Activity 2.2

Put double-quotes around the variable name result in the second line. Run the program.

Try to explain what happened before reading further.

When putting double-quotes around the variable name, it becomes a string. Hence, the computer will print the name literally, instead of printing the value stored in the variable.

To sum up, "result", "7", result and 7 are different things: the first two are strings (literal text), the third is a variable, and the last is a number.

Learning from mistakes is always helpful, so for this and the remaining programs in this tutorial, I encourage you to introduce errors and observe what message or output you get. Don’t forget to reset the program after each error.

In a sequence, all instructions are executed. Next you will learn how to selectively execute some instructions but not others.

Introduction Start Selection