Writing, Saving and Running Python Programs with IDLE

Writing, Saving and Running Python Programs with IDLE

Entering commands at the prompt is just the beginning. Let’s use IDLE to save and run files. With this skill you’ll be able to write and build complex and powerful Python programs. IDLE has two modes: interactive and script. We wrote our first program, “Hello, World!” in interactive mode. Interactive mode immediately returns the results of commands you enter into the shell. In script mode, you will write a script and then run it.

Let’s create a module. We’ll cover modules in depth later. For now, all you need to know is that a module is a file containing Python code ending in the suffix:

.py

If it’s not already running, start IDLE. Using IDLE is very similar to using a text editor. Under File, select New Window or press Ctrl + N. That will bring up a new window titled, “Untitled”.

IDLE

Enter this command in the Untitled window:

print(“What's brown and sounds like a bell?”)

We need to save our file before we can run it. So under File, select Save. At the prompt, name it something clever like:

badjoke.py

And save it somewhere you can find it later.

Now the fun part. Under Run, select Run Module.

Running a Python program in IDLE

Now go back to the Shell. What’s this RESTART business? The Shell is restarted every time you run a module in IDLE. That’s to clear out any old settings that might interfere with your program. Below the RESTART line you should see:

Running a Python program in IDLE

It’s not much of a joke without a punchline. Let’s modify our module. Change the following:

input(“What's brown and sound like a bell?”)
print(“Dung!”)

Let’s run that again, this time with the keyboard shortcut: F5. Try that.

In the Shell you’ll see our joke printed, but where’s the punchline? Notice that there isn’t a prompt? That’s because the Shell is waiting for your input. Press any key.

Running a Python program in IDLE

I love that joke. It’s so good and so bad. Know what I mean? Know what I mean? Nudge nudge, say no more.

Perform mathemagical feats in our next lesson on Python operators.

Hands-on Computer Science

Learn computer science, programming, and web development with us, your educational and entertaining super friends.

Subscribe to our YouTube Channel

Writing, Saving and Running Python Programs with IDLE was posted by Jared on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.