Skip to content

IDLE

IDLE consists of two components:

  • IDLE Shell: A Read-Eval-Print Loop (REPL) shell

    • The shell reads user input which should consists of a single statement which can be multiple lines.
      • Begins with >>>.
      • Next line begins with ....
    • The shell evaluates the single statement.
    • The last expression in the statement will printed unless the result of the expression is None.
    • The shell then loop to read the next statement.
    • Note that you can NOT evaluate multiple statements.
  • IDLE Editor: A text editor.

    • Can be used to execute the entire code.
    • Nothing will be printed unless an explicit print(...) is given.
    • Run code by pressing F5.
      • This will restart the shell.
      • Useful to avoid state pollution (i.e., you do not know what changes has been made).

Shell

Shell

Editor

Editor

Terminology

For simplicity, we may use IDLE to mean IDLE shell and we may use Editor to mean IDLE editor.

Shell

An important distinction is that IDLE shell will show result. Otherwise, we --as user-- may be confused. This can also be used to test the code you are writing on editor too. The example on the left is a standard usage, as you can see, there may be multiple lines. The example on the right is a wrong usage where we try to evaluate two statements.

Shell01

Shell02

Note that the value 3 and 5 are the evaluation results printed by the shell. We will see the difference between shell and editor when running files containing these code.

Editor

There multiple ways to open the editor. If you have set up the double click to open Python files (i.e., files with extension .py), then you can simply double click on the Python files. Note that you may not see the file extension. The image on the left does not show the file extension but you can see that it is a "PY File" from the column "Type". The image on the right shows the file extension.

Options01

Options02

So how can we show the file extension? If you are on Windows, you can activate the folder options. Click "Options*"" (you may need to click on the ...) as seen on the left. Then click on the "**View" tab and uncheck "Hide extensions for known file types" as seen on the right. Once you do this, you should be able to see the file extension.

Options03

Options04

Another way is to open a new file. Start with IDLE shell, then click on "File > New File". Alternatively press Ctrl+N on Windows or Cmd+N on Mac. This should open a new file called "untitled" as seen on the right.

NewFile01

NewFile02

Then we can edit the file by typing the code that we want. Notice how if there are changes on the file, you will see star (i.e., *) surrounding the filename which is currently called untitled. So you should see *untitled* as seen on the left. Now we can save this by clicking "File > Save" as seen on the right (shortcut: Ctrl+S on Windows or Cmd+S on Mac). Let us save this file as Example.py.

Edit01

Edit02

Now we can run this file by clicking "Run > Run Module (shortcut: F5). If no IDLE shell opened, this will open a new IDLE shell. It may also still open a new IDLE shell as it depends on whether the editor is attached to a particular shell or not. But notice that there is nothing printed.

Run01

Run02

But how do we know that it is run? Notice that we have assigned the value 2 on variable x in the statement x = 2. We can ask what is the value of x on the IDLE shell. This should print the value 2. Let us try to initialize the variable y to 3 and check the value of x + y. Notice that there is nothing printed on assignment. But something is printed on evaluation of the addition.

Run03

Run04

Why is nothing printed? In the case of assignment, there is literally no result. In particular, it cannot be put inside a print(...) function. This causes an error as we can see on the left. Another value that is not going to be printed is the value None. This is a value that represents nothingness. But we can still force Python to print this by using print(None) as seen on the right.

Run05

Run06

Printing from Editor

So that gives us some clue on how we can print values from the editor. We need to add an explicit print(...) function. Let us modify our example code to include explicit print. First notice that if we have not saved the file after we made any changes, the * surrounding filename appears again. If we try to run (shortcut: F5), we will get the following warning on the right. Just click "OK".

Print01

Print02

Now we will see something printed before the initial prompt >>>. Notice also there is the following line.

========== RESTART: D:/Downloads/Example.py =========

This means that the shell has been restarted. Any changes we made earlier will no longer be applicable. Only the state changes made by the code from the editor is going to be present. We can check this by asking the value of variable y. We will get an error as seen on the right. So that is the meaning of restart.

Restart01

Restart02