Chapter 3 The First Python Program

3.4 A Simple Complete Python Program

Text IO allows a stand-alone Python program to interact directly with the user. For a simple Python program that does not rely on IDLE, consider the problem of interest calculation. Suppose that a user has an initial sum of money in a savings account, and the interest rate is fixed for a number of years. The problem is to find out the total sum of savings after a certain number of years. A program can be written as follows.

The program above consists of 6 lines of code. It can be typed in using any text editor, and saved into a specific folder in the file system. For the convenience of illustration, suppose that it is saved under the Desktop folder.

The IDLE editor can also be used to edit the program. To start editing a new program, go to the menu item [File→New Window], and a new text editor window will be popped up. Enter the code above into the editor window, and go to the menu item [File→Save…]. A dialog box will be opp up for entering the destination folder and file name. Choose Desktop as the destination folder and savings.py as the file name. A new Python code file savings.py will be created in the Desktop folder. It also appears on the Desktop of the graphic user interface of the computer.

In order to open and edit an existing file using IDLE, go to the menu item [File→Open…]. A dialog box will pop up for the selection of a specific file. In the case of this example, select the file savings.py from the Desktop folder. A new text editor window will pop up, which contains the current content of the file. Editing can be performed in the editor window.

In order to execute the current program in the editor, go to the menu item [Run→Run Module], or press the function key F5. The current window will be switched to the interactive IDLE window, and the following result can be shown according to the execution of the program.

>>> ============================== RESTART ==============================
[Savings Calculator]
Please enter the interest rate: 0.036
Please enter the number of years: 5
Please enter the initial sum of money: $10000
After 5 years, the savings will be $11934.35
>>>

The program displays the title ‘[Savings Calculator]’, and then asks the user to input the interest rate, the number of years after which the total savings should be calculated, and the initial sum of money. In this example, 0.036, 5 and 10000 are entered as the three values, respectively. The program then displays the result in the text console.

The Python program can also be executed directly using Python, under a text console.

On Windows in Desktop folder, the Command Line tool is the default text console.

>python saving.py [Savings Calculator]
Please enter the interest rate: 0.036
Please enter the number of years: 5
Please enter the initial sum of money: $10000
After 5 years, the savings will be $11934.35

Now back to the program itself. It consists of 6 statements. The first and last statements are two print statements, while the rest of the statements are assignment statements. With respect to functionality, the second to fourth statements inquire the user for relevant inputs. The fifth statement calculates the answer based on user input, and the last statement shows the result to the user.

The Python program is executed line by line, from top to bottom. It can be treated as a list of statements, executed sequentially in a batch. The order is important, because the execution of one statement can depend on the result of another. For example, in the code above, final is calculated based on the user input of init. By default, Python always executes a program sequentially.

© Copyright 2024 GS Ng.

Next Section - 3.5 The Structure of Python Programs