Python For Kids For Dummies
Book image
Explore Book Buy On Amazon

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.

    <b>Figure 1: Press the Tab key to open this drop-down menu.</b>
    Figure 1: Press the Tab key to open this drop-down menu.
  3. Press the arrow key to get to print in the drop-down window.

    The p that you typed changes to the word print.

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

    <b>Figure 2: Python's hissing about an error.</b>
    Figure 2: Python's hissing about an error.
  3. Press the up arrow key until the cursor is back on the previous line.

    You should have to press it three times.

  4. Press Enter.

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

    <b>Figure 3: Command line history copies lines so you can fix errors.</b>
    Figure 3: Command line history copies lines so you can fix errors.
  5. Use the arrow keys to go to the end of the line.

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

About This Article

This article is from the book:

About the book author:

Brendan Scott is a dad who loves Python and wants kids to get some of its magic too. He started to help teach his oldest child to code. He maintains it to help other young people learn Python.

This article can be found in the category: