Python 101 – An Intro to IDLE

Python comes with its own code editor: IDLE (Integreted Development and Learning Environment). There is some lore that the name for IDLE comes from Eric Idle, an actor in Monty Python. An IDE is an editor for programmers that provides color highlighting of key words in the language, auto-complete, an “experimental” debugger and lots of other fun things. You can find an IDE for most popular languages and a number of IDEs will work with multiple languages. IDLE is kind of a lite IDE, but it does have all those items mentioned. It allows the programmer to write Python and debug their code quite easily. The reason I call it “lite” is the debugger is very basic and it’s missing other features that programmers who have a background using products like Visual Studio will miss. You might also like to know that IDLE was created using Tkinter, a Python GUI toolkit that comes with Python.

To open up IDLE, you will need to find it and you’ll see something like this:

Yes, it’s a Python shell where you can type short scripts and see their output immediately and even interact with code in real time. There is no compiling of the code as Python is an interpretive language and runs in the Python interpreter. Let’s write your first program now. Type the following after the command prompt (>>>) in IDLE:

print("Hello from Python!")

You have just written your first program! All your program does is write a string to the screen, but you’ll find that very helpful later on. Please note that the **print** statement has changed in Python 3.x. In Python 2.x, you would have written the above like this:

print "Hello from Python!"

In Python 3, the print statement was turned into a print function, which is why parentheses are required. You will learn what functions are in chapter 10.

If you want to save your code into a file, go to the File menu and choose New Window (or press CTRL+N). Now you can type in your program and save it here. The primary benefit of using the Python shell is that you can experiment with small snippets to see how your code will behave before you put the code into a real program. The code editor screen looks a little different than the IDLE screenshot above:

Now we’ll spend a little time looking at IDLE’s other useful features.

Python comes with lots of modules and packages that you can import to add new features. For example, you can import the math module for all kinds of good math functions, like square roots, cosines, etcetera. In the File menu, you’ll find a Path Browser which is useful for figuring out where Python looks for module imports. You see, Python first looks in the same directory as the script that is running to see if the file it needs to import is there. Then it checks a predefined list of other locations. You can actually add and remove locations as well. The Path Browser will show you where these files are located on your hard drive, if you have imported anything. My Path Browser looks like this:

Next there’s a Class Browser that will help you navigate your code. Frankly it would make more sense if this menu option was called “Module Browser” as that is much closer to what you’ll actually be doing. This is actually something that won’t be very useful to you right now, but will be in the future. You’ll find it helpful when you have lots of lines of code in a single file as it will give you a “tree-like” interface for your code. Note that you won’t be able to load the Class Browser unless you have actually saved your program.

The Edit menu has your typical features, such as Copy, Cut, Paste, Undo, Redo and Select All. It also contains various ways to search your code and do a search and replace. Finally, the Edit menu has some menu items that will show you various things, such as highlighting parentheses or displaying the auto-complete list.

The Format menu has lots of useful functionality. It has some helpful items for indenting and dedenting your code, as well as commenting out your code. I find that pretty helpful when I’m testing my code. Commenting out your code can be very helpful. One way it can be helpful is when you have a lot of code and you need to find out why it’s not working correctly. Commenting out portions of it and re-running the script can help you figure out where you went wrong. You just go along slowly uncommenting out stuff until you hit your bug. Which reminds me; you may have noticed that the main IDLE screen has a Debugger menu.

That is nice for debugging, but only in the Shell window. Sadly you cannot use the debugger in your main editing menu. However you can run a module with debugging turned on such that you are able to interact with your program’s objects. This can be useful in loops where you are trying to determine the current value of an item inside the loop, for example. If you happen to be using tkinter to create a user interface (UI), you can actually leave the mainloop() call off (which can block the UI) so you can debug your user interface. Finally, when an exception is raised with your debugger running, you can double-click the exception to jump directly to the code where the exception happened.

If you need a more versatile debugger, you should either find a different IDE or try Python’s debugger found in the pdb library.

The Run menu has a couple of handy options. You can use it to bring up the Python Shell, check your code for errors, or run your code. The Options menu doesn’t have very many items. It does have a Configure option that allows you to change the code highlighting colors, fonts and key shortcuts. Other than that, you get a Code Context option that is helpful in that it puts an overlay in the editing window which will show you which class or function you’re currently in. You will find this feature is useful whenever you have a lot of code in a function and the name has scrolled off the top of the screen. With this option enabled, that doesn’t happen. Of course, if the function is too large to fit on one screen, then it may be getting too long and it could be time to break that function down into multiple functions. The other neat item in the Settings dialog is under the General tab where you can add other documentation. What this means is that you can add URLs to 3rd Party documentation, such as SQLAlchemy or pillow, and have it pulled into IDLE. To access the new documentation, just jump to the Help menu.

The Windows menu shows you a list of currently open Windows and allows you to switch between them.

Last but not least is the Help menu where you can learn about IDLE, get help with IDLE itself or load up a local copy of the Python documentation. The documentation will explain how each piece of Python works and is pretty exhaustive in its coverage. The Help menu is probably the most helpful in that you can get access to the docs even when you’re not connected to the internet. You can search the documentation, find HOWTOs, read about any of the builtin libraries, and learn so much your head will probably start spinning.


Wrapping Up

In this article we learned how to use Python’s integrated development environment, IDLE. At this point, you should be familiar enough with IDLE to use it on your own. There are many other integrated development environments (IDEs) for Python. There are free ones like PyDev and Editra, and there are some others that you have to pay for, such as WingWare and PyCharm, although they both have free versions too. There are also plug-ins for regular text editors that allow you to code in Python too. I think IDLE is a good place to start, but if you already have a favorite editor, feel free to continue using that.

If you happen to be a visual learner, I also created a screencast version of this tutorial:

This is from my Python 101 Screencast

Print Friendly