Some IDLE Tricks to Remember in Python

By Brendan Scott

In Python, the IDLE Shell window has a couple of tricks that make coding a little easier. Tab completion and command history are two good tricks.

Tab completion

Tab completion is using the Tab key to finish your typing for you. Try it out with these steps:

  1. Start a new line in the Shell window.

  2. Type p and press the Tab key. This is tab completion at work!

    A drop-down window appears, giving you different options. In Figure 1 you can see the window.

  • Press the arrow key to get to print in the drop-down window.

    The p that you typed changes to the word print.

  • Type (Hello World!) and then press Enter.

    Keep typing to close the drop-down window; don’t press Enter. If you press Enter while you’re there, you’ll muck up the completion. If typing is just too weird for you, press the Tab key twice to accept the highlighted selection.

  • Tab completion also works with the names of variables that you’ve already created. Try this example:

    1. Start a new line in the Shell window.

    2. Type this_is_a_long_variable_name = 0.

    3. Press Enter.

    4. Type thi and press the Tab key.

      Yes, type only t, h, and i.

      The whole variable name should be typed out for you now. Only one variable matches the completion for thi, so you don’t have to choose from a list.

    Command history

    IDLE lets you go back and edit your mistakes using command history. To use command history, follow these steps:

    1. Use the up arrow key to go back to the line you want to run again.

    2. Press Enter.

      The code at the current command prompt is copied. If there’s a code block (such as one associated with a conditional statement like if), then the whole code block is also copied.

    3. Use the arrow, Backspace, and Delete keys to edit the line.

    4. Press Enter to run the code.

    Try it yourself:

    1. Type print(‘Hello World!) at the prompt.

      Yes, just the one quote. The line is intentionally wrong.

    2. Press Enter.

      Python complains about a syntax error, like you see in Figure 2.

  • Press the up arrow key until the cursor is back on the previous line.

    You should have to press it three times.

  • Press Enter.

    A copy of the code appears at the interpreter prompt. See the copied line in Figure 3.

  • Use the arrow keys to go to the end of the line.

  • Insert the closing quote and then press Enter.

    Using command line history is often easier than retyping the whole line again. Missing quote marks, misspellings, and bad syntax are cases where it’s good to use.