Skip to content

IDLE

IDLE (Integrated Development and Learning Environment) is the editor of choice in IT5001 at NUS. While primitive, this is an IDE that's built-in and presently available when installing Python using the official installer. Essentially, you do not need to download a more sophisticated code editor or IDE to program something pretty simple in Python. Speaking of sophisticated, it's also to streamline the tool to use during practical exams, free from additional plugins (or now with AI capabilities) that can tip the scale when it comes to maintaining a controlled environment.. which would then complicate how students' capabilities are tested and graded.

Running IDLE

If you installed Python using the downloaded official installer from python.org, you should see IDLE as an available app to execute. Just run it to start IDLE and you should view the IDLE Shell first by default.

For the fiesty or finnicky ones who installed it using other means like via Homebrew or pyenv, IDLE does not come installed by default. To install IDLE, we need to install the required Tkinter library.

brew install python-tk

Enter the following command in the Terminal to execute IDLE:

Terminal Command to Execute IDLE
python -m idlelib

Without installing the Tkinter library, the following error message will appear:

** IDLE can't import Tkinter.
Your Python may not be configured for Tk. **

Using IDLE

Right here, you are able to run Python commands and see results instantaneously. In fact, it is possible to treat it as a simple calculator interface.

IDLE Shell Window

Try entering some simple arithmetic expressions into the shell:

  • 2 + 2
  • 19 + 20
  • 69 / 3 - 20
  • (-45 / 4)**2 (which means \((45\div 4)^2\))
  • ((2+0+2+5) * (2-0-2-5))^2 (which means \([(2+0+2+5)\times (2-0-2-5)]^2\))

Notice that you will obtain the simplified result of the typed-in arithmetic expression. The way how this Python shell works follows the "REPL" cycle (i.e., read, execute, print, loop).

  • Read the entered command/statement/code
  • Execute the entered command/statement/code
  • Print the obtained result from executing the command/statement/code
  • Loop - repeat R.E.P. all over again with the next entered command/statement/code

Python Shell outside of IDLE

You can also access the Python shell interface outside of IDLE, either by:

  • typing in python or python3 in the Terminal (enter exit() or quit() to exit the interface); or..
  • opening the program from the given shortcut in the Start Menu, titled "Python version here (64-bit)"

Creating and Using a Program File

Now say you require running each of the statements you typed in the same sequence as you have done, perhaps with minor tweaks to each statement. You may find that it's not efficient to work solely on the shell command line, as you would need to re-enter each statement or line manually, among several other disadvantages. Here, it is better to create a new file to contain each of your statements.

To create a new file, select File from the top menu bar, then New File. You may also do so using the key combo Ctrl+N for Windows or Cmd+N for macOS.

Create a new file in IDLE

You should then be presented with a new blank window like as follows:

New program File in IDLE

Right here, you can proceed include your statements to be executed in succession from the get-go. Save the following file as test.py.

test.py
1
2
3
4
5
2 + 2
19 + 20
69 / 3 - 20
(-45 / 4)**2
((2+0+2+5) * (2-0-2-5))^2

Now, let's run this program file. In your program file window you just populated (i.e., test.py), select Run, then Run Module. You can also do this by pressing F5.

You may notice that it switches you back to the IDLE shell window, but no result is printed. Why is that?

When you type out an expression or line of Python code in the IDLE shell, the result is printed immediately following the R.E.P.L. cycle. However, when running code from a program file, the printing is not done automatically. You will need to surround each of your expressions using the print() function.

With this amendment, the results should then be printed as soon as you try running the module (i.e., Run \(\rightarrow\) Run Module or F5).

test.py
1
2
3
4
5
print(2 + 2)
print(19 + 20)
print(69 / 3 - 20)
print((-45 / 4)**2)
print(((2+0+2+5) * (2-0-2-5))^2)

Switching gears from here, let's print out a message - the classic "Hello World!" example.

test.py
print("Hello World!")

And with that, you are pretty much set to start learning how to program in Python! Here are another bunch of code snippets you can try running by yourself.

test.py
name = "Henry"
print(f"Hello {name}")
test.py
1
2
3
4
5
6
from datetime import datetime


birth_year = int(input("Enter a birth year >> "))
age = datetime.now().year - birth_year
print(f"You are {age} years old.")
test.py
for _ in range(12):
    print("The quick brown fox jumps over the lazy dog.")

Showing Line Numbers in New Windows

I like having line numbers in code editors. Often when errors arise, the text in red in the console (what we call the stack trace) will often mention where/which line the error was raised from. Using this information, one can cross-check where the problem sources are easily when they arise. Even without stack traces, having line numbers easily allows one to roughly remember where about specific parts of the program are located at.

You can configure the program file window to include line numbers when they appear from the settings.

New program File in IDLE

To activate this setting manually each time, select Options from the menu bar, and choose Show Line Numbers.

To activate this for good in your IDLE editor window,

  1. Go into Settings from the menu bar.
  2. Under the Shell/Ed tab, ensure that Show line numbers in new windows is checked.

Activate Line Numbers in IDLE

Reference