Anth's Computer Cave Tutorials

Python basics: Using IDLE

20th May 2015

Today I'll show you some basic usage examples in IDLE, the Python interpreter and code editor.

There are other ways to use Python, but IDLE is the most common.

This article assumes you have Python installed on your Windows computer. If not, see the first article, How to install Python on Windows

The IDLE shell and the IDLE code-editor.
The IDLE shell on the left and the code editor on the right.

IDLE has two separate components, the shell and the code-editor.

The IDLE shell

In the shell you can type simple code snippets to try out code ideas on the fly. It is also where you can watch your programs run, and read their output.

You can launch an IDLE shell from the Start Menu by selecting IDLE(Python GUI).

For instance, let's say you want to try out a Python library or module to see how it works. This is a good use for the shell.

In this example I'll import the Time module.

Import time from the IDLE shell.

At the prompt, type import time and press Enter.

Let's get the Time module to tell us the time using the strftime() function.

Type time.strftime("%H:%M") and press Enter.

There is our time in 24-hour format.

Here is another time function I use regularly, the time() function

Type time.time() and press Enter.

There you can see that 1432097078 seconds have passed since the Unix epoch (1st of January 1970).

Now I am not going to go into that because I'm not trying to show you the Time module. I am trying to show you the kind of things you would use the shell for rather than the code editor.

The IDLE code-editor

The code-editor is where you write and edit your main scripts and programs

We'll continue with our Time module scenario. Now you have tried the module out in the shell, you may want to build and save some functions using Time.

This is where you will use the code-editor.

From the IDLE shell, click File and select New File.

You'll see an empty code-editing window.

Let's write a simple script to create a countdown clock using the Time module.

Type a comment at the top of the file to tell people it is a countdown clock. (The "#" character makes that line a comment that Python won't try to run.

Once again we'll import the Time module.

Next we'll declare a global variable called count. The script will use this variable to keep track of the countdown.

Now we create a while loop to run the countdown.

Each loop will print the current count then subtract one.

We then use the time.sleep() function to tell the script to wait for one second.

Lastly, we'll tell the script what to do when the countdown finishes.

Naturally we want it to say Blast Off!

Notice the print statement is outside the while loop so it waits for the entire countdown to finish.

You'll need to save your script now.

Click File and select Save As.

Select a destination and give your script a name.

IDLE will automatically add a Python (.py) extension to the file.

Finally, we can run the script.

Click Run and select Run Module.

A new Python shell will open and start printing the count every second.

Once it gets below one the while loop ends and you should see "Blast Off" printed to the shell.


I hope this has given you an idea about the relationship and difference between the shell and the code-editor.

Basically, you can create and run small scripts and test Python features and modules in the shell.

Any larger scripts, or scripts you want to save and use later, you will create in the code-editor.

You then run and interact with these scripts and read their output in the shell.

Next article

In the next article I'll discuss Python version compatability.

You will most-likely do a lot of your programming using Python 3 or later, but it pays to understand some compatibility quirks between Python 3 and Python 2. There are several syntax differences, but you can also strike issues when working with modules and libraries that were written for a newer or older version of Python.



PreviousInstall Python

Next: Python version compatability



Leave a comment.

There are currently no comments for this article.

Leave a comment on this article