Writing and Running a Program
So far, we have used the shell to investigate Python. As you have seen, the shell will show you the result of evaluating an expression:
| | >>> 3 + 5 / abs(-2) |
| | 5.5 |
In a program that is supposed to interact with a human, showing the result of every expression is probably not desirable behavior. (Imagine if your web browser showed you the result of every calculation it performed.)
How Does a Computer Run a Python Program?, explained that in order to save code for later use, you can put it in a file with a .py extension. You can then tell Python to run the code in that file rather than type commands in at the interactive prompt.
Here is a program that we wrote using IDLE and saved in a file ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access