LambdaLabTM
Computer Science · Class 11 · Variables & User Input
VariablesWhy⏱️ 7 min read

Why print() Is Not Enough

So far, every answer you have asked Python for has gone straight into a print(). It has worked. But it only works for tiny programs, and this lesson shows you exactly where it breaks.

1print() shows the answer, then forgets it

Think about what print(2 + 3) actually does. Python solves 2 + 3, gets 5, throws the 5 on the screen — and then lets it go. The answer is displayed, not kept.

It is like a friend who does a sum in their head, tells you the answer, and then instantly forgets it. If you ask again a minute later, they have to work the whole thing out from the beginning.

Key Takeaway
print() is a display tool, not a storage tool. It shows a value. It does not save it anywhere for the rest of the program to use.

2Where this becomes a real problem

A tiny program can get away with it. A real program cannot. Suppose you work out the total marks of a student near the top of a long program, and further down you need that total three more times — once to find the percentage, once to print the report, once to decide pass or fail.

If the total only ever went into a print(), it is gone. Your only choice is to write the whole calculation out again, everywhere you need it:

repeated.py
print(37 + 45 + 41)
print((37 + 45 + 41) / 3)
print((37 + 45 + 41) - 100)
Output
123
41.0
23

This program works. But three things about it are bad:

  • The computer does the same work again and again. Those three numbers are added three separate times. Here that is nothing. In a program that repeats a heavy calculation a thousand times, it is very slow.
  • You have to repeat yourself. The same long sum is typed out three times, and every copy is a chance to make a typing mistake.
  • One change means many changes. If a mark is corrected from 41 to 44, you must remember to fix it in all three places. Miss one, and your program quietly prints a wrong answer.
And some values cannot be worked out twice
A long program uses the same result in many places, so this problem grows with the program. Worse, some values can never be worked out a second time at all — a number the user types in, for example. Python cannot guess it again. If you do not save it when it arrives, it is lost.

3The fix: work it out once, and keep it

What we really want is a way to say to Python: “solve this, and hold on to the answer for me — I will need it later.” Python gives us exactly that. You give the answer a name, and from then on you use the name:

saved.py
total = 37 + 45 + 41
print(total)
print(total / 3)
print(total - 100)
Output
123
41.0
23

Same three lines of output. But now the sum is worked out once, the answer is stored under the name total, and every line after that simply asks for total. Correct a mark and you change it in one place only.

4Watch both programs run

Press Run both. The two programs print exactly the same thing — so watch the memory strip in the middle instead. On the left the answer puffs away each time. On the right it is worked out once and stays in a box.

Two programs · same output · very different work
Everything inside print()
print(2 + 3)
print((2 + 3) * 10)
print((2 + 3) - 1)
Memory — what is kept
nothing is saved here
Screen
Python solved 2 + 3— times
Saved in a variable first
c = 2 + 3
print(c)
print(c * 10)
print(c - 1)
Memory — what is kept
the box appears when line 1 runs
Screen
Python solved 2 + 3— times

That box is called a variable, and it is the whole idea of this chapter. It is a place in the computer's memory where your program parks a value, gives it a name, and picks it up again whenever it likes.

The rule of thumb
If your program needs a value more than once — or needs it later — do not put the calculation inside print(). Save it in a variable first, and print the variable.

5Recap

Key Takeaway
print() only displays a value. It does not retain it. To keep a result so it can be used again and again — without making the computer solve the same problem over and over — we save it inside a variable. The next lesson opens one up and shows you what a variable really is.
Quick Check

What does print(2 + 3) do with the value 5?

Quick Check

Why is writing print(37 + 45 + 41) three times a bad idea?

Quick Check

What do we actually need, that print() cannot give us?