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.
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:
print(37 + 45 + 41)
print((37 + 45 + 41) / 3)
print((37 + 45 + 41) - 100)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.
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:
total = 37 + 45 + 41
print(total)
print(total / 3)
print(total - 100)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.
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.
print(). Save it in a variable first, and print the variable.5Recap
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.What does print(2 + 3) do with the value 5?
Why is writing print(37 + 45 + 41) three times a bad idea?
What do we actually need, that print() cannot give us?