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 ...

Get Practical Programming, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.