python1cups = 2 + 32cost = cups * 1.53word = "tea" * 24print(word, cost)5print("Cups: " + str(cups))print("Cups: " + cups)TypeError2 + 3 * 47 / 2tea.py57.5'teatea'teatea 7.5'5'
Code card: tea.py; the highlighted line is the one runningPython interpreter: works out each expressionTerminal: the >>> prompt and what print() writesMemory boxes: a name, a type (int, float or str) and a valueAt the >>> prompt Python is a calculator: * goes before +, / always gives a float, // drops the fraction and % gives the remainder. Then tea.py runs line by line. Each value it makes lands in a memory box that shows its name, its type and its value: the int 5, the float 7.5 and the str 'teatea'. print() writes values of any type; str() turns the int 5 into the text '5' so + can join it to "Cups: ". In the last step line 5 loses its str(): Python will not join text and a number, and stops with a TypeError after lines 1 to 4 have run.

Numbers at the prompt

  1. At the >>> prompt Python is a calculator. + adds, - subtracts and * multiplies. As in maths, * goes before +, so 2 + 3 * 4 is 14. Brackets change the order: (2 + 3) * 4 is 20.

Types, line by line

Mixing types

Try it yourself

;; step 1/9
>>> 2 + 3 * 4
14
>>> (2 + 3) * 4
20
>>> 10 - 4
6
>>> 2 ** 10        # ** is "to the power of"
1024

The $ and >>> at the start of a line are prompts printed for you: you do not type them. On macOS and Linux the command may be python3. Python 3.11 and later also print a line of ~ and ^ under the failing code, pointing at the +; the figure leaves it out. In the real message, the ... is the full path to the file.