IDLE comes with python installation and it is used for writing python codes. The first thing anyone wants to do after installing Python on your machine is to write the first python code. To write a piece of code (sometimes also called script), you need IDE (Integrated Development Environment) or an editor. An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build tools (optional), compiler/Interpreter and a debugger. Source code editor (or simply an editor) is a place where you can write code as well customize the code’s appearance (colour, font, highlights etc..). Once the script is written, it goes for a conversion from the source language (more often called High-level language) to machine code (low-level language).
Table of Contents
- What is Python IDLE
- IDLE features
- IDLE components
- IDLE to write code and execute it
- Improving work-flow
- Debugging Python code in IDLE
- Customize or style IDLE
What is Python IDLE
Python default IDE is known as IDLE (Integrated Development and Learning Environment). There is no need to install this IDE separately (via Python PIP) as it comes as default with Python installation. Although there are plenty of IDE which you can download separately on your system, still it is considered as a super choice for a newbie. IDLE comes by default on Windows and Mac but Linux user has to download it using the package manager. You have learned to write a Python code in Interactive environment, where you get the instant result of an expression. Now it’s time to write a few lines of code to solve a problem. You can write multiple lines of code in the Interactive environment as well, but it is not favoured because of the debugging reasons.
- coded in 100% pure Python, using the tkinter GUI toolkit
- cross-platform: works mostly the same on Windows, Unix, and macOS
- Python shell window (interactive interpreter) with colourizing of code input, output, and error messages
- multi-window text editor with multiple undo, Python colourizing, smart indent, call tips, auto-completion, and other features
- search within any window, replace within editor windows and search through multiple files (grep)
- debugger with persistent breakpoints, stepping, and viewing of global and local namespaces
- configuration, browsers, and other dialogs
On Windows, IDLE can be started by two methods
- Press Window button, a search box will appear, write idle and hit enter.
- Go to program list by pressing windows button and then look for Python in the program list. Expand the Python list by clicking on the down arrow and then click on IDLE (Python 3.x.y x64 bit) from the drop-down menu as shown in the image.
After opening, the interface looks similar to the image shown below. The menu fields have been marked with red square boxes (in real the red square boxes are not present, this is done here for demonstration purpose only).
Now we see the various menu options present. We have File, Edit, Shell, Debug, Options, window and help. When you click on any of the menus, you will see certain items in the drop-down list. I have tried to capture all the drop-down items of IDLE in the two images shown below.
Python IDLE to write code and execute it
Till now, you have seen various components of a IDE window. Don’t worry about knowing all the components, as we progress further through this article, you will get to know about them in details. At first, this IDLE will act as Interactive Python Interpreter, meaning as soon as you write certain expression/code after >>> (Tripple forward arrow), you will get the result in the next line itself. You can evaluate multiple expression one by one or you could also write multiple lines of script and evaluate them. However, writing multiple lines is not advisable here. You will see how to write multiple lines of code and execute them in one go, in a while. A sample screenshot is provided below to clear your doubts if any.
At the bottom right portion of the window (both IDLE window and Editor window), you will observe two symbols Ln and Col. This represents current line number (Ln) and column number (Col) of the cursor.
a) Create a new file
To write multiple lines of code, we need an editor. You can open an editor by navigating to File menu and select the new file from the drop-down list. Alternatively, you can use the shortcut command (Crtl+N). This is Python default editor and we will write our programs in this editor. Image shown below illustrate the method to create a new file for writing python program.
You have noticed that the name of the editor is untitled, which means the name has not been assigned to this file. We will first give a name and save the file. To do so, click on the file option of this editor (not the IDLE) and select Save from the drop-down list. It will ask for the location to save the file, browse the path where you want to save the file and provide a name with the extension .py with it and click on the Save button. You will notice the untiled is replaced with the name of your file along with the location. Now, we are good to go and write the program.
Write a short program (image is attached for the sample code) and click on the Save button from the file menu, alternatively press Ctrl+S to save the file. I have created two variables, added them and printed the results.
Note: You will observe an asterisk symbol(*) until you save the code. This symbol will disappear after saving the code.
You can execute the program by navigating to Run menu of this editor and select Run Module from the drop-down list. The keyboard shortcut to run the program is F5. After you hit F5 or Click on Run module, the editor window will be minimised and results will be displayed back in the IDLE window (main window). Find the image below to understand it.
b) Open an existing file
To open an existing file, navigate to File menu of any window (either the IDLE window or editor window) and select Open from the drop-down list. It will open a new window and ask you to choose a file. Browse the file location, select the file and click on Open button, the new file will be opened.
Once the file is opened, you can execute the program or make some changes to it. To execute the code use the same method as described earlier i.e. press F5. You can also modify/add the existing code and run again.
Till now you have understood how to use IDLE to write and execute python code. Now it’s time to increase your efficiency while writing code by utilizing IDLE tool features. Python IDLE provides you with three amazing features, which you can use, to implement your code faster and with ease. You are going to learn about
a) Automatic Indentation
Python is a kind of program which enforces code readability and code structuring through Indentation. It is the blank spaces that are applied at the starting of the next immediate line which ends with a colon (:). Understand it in little easy way, when you create function, loops, or classes, it ends with a colon and when you press Enter key the cursor goes to a new line. Here, instead of the cursor being starting at extreme left, it starts after 4 blank space. This is done automatically by Python IDLE to save time in the overall development process. An example of automatic indentation for function, loop and class is shown in the image below.
b) Call Tips
There are thousands of function and methods present in Python language and it is hard to remember each of them, especially the arguments that can be passed during a method call. Call tips help the programmer by providing a hint as what elements are needed next. When you place a left parenthesis, the call tip will pop up automatically, within a few seconds of inactivity, to provide a hint to the user about that particular element. Call tip may not appear for a method which falls under some specific library, for call tip to appear you need to import that library. On Windows OS, you can see a call tip by pressing Ctrl+’\’ button.
c) Code Completion
Python IDLE has basic code completion functionality. This feature helps the user to save coding time by bringing automatic class and function names, that are declared previously in the program. This helps the user to save their time significantly in the overall development process for a comparatively larger program. To use autocompletion in the editor, just press the tab key after a sequence of text. Here, in the below image, I have created a function called my_user_dashboard(). Whenever I want to use this function, I can type in my and then press the Tab button on the keyword, it will automatically complete the full-text my_user_dashboard.
Debugging a Python code in IDLE
We debug our code to make it free from any existing Bugs (mistakes) as well monitor how the changes are happening. For example, we can monitor a variable from its value assignment to the final result. Python IDLE provides basic debugging features to debug your code. A bug could appear in many from and one is different from others. Sometimes it’s very difficult to find the reason for its existence. IDLE provides two types of Debug method,
a) Interpreter Debug Mode
Interpreter debug mode is switched OFF by default and it can be invoked (activated) by navigating to Debug menu in the IDLE window and selecting Debugger option from the drop-down list. Once the debugger is activated you will notice [Debug ON] text before >>> arrow mark in the IDLE window as well debug control window (shown in the image below)will pop up.
After the debugger is ON, you can select your program (File->Open->Select your file) and hit F5 to run. This will load the program location in IDLE and debugger is now active to execute the script.
You’ll also see four checkboxes in the debug window:
- Globals: your program’s global information
- Locals: your program’s local information during execution
- Stack: the functions that run during execution
- Source: your file in the IDLE editor
Click on the Go button. Image shown below shows the process flow.
You will observe various global and local variables along with their values. To deactivate Debugger just close the Debug Control window, after that you will also see [DEBUG OFF] before >>> symbol in the main window.
Editor windows also have breakpoint functions. Lines with a breakpoint set are specially marked. Breakpoints only affect when running under the debugger. To apply breakpoint or breakpoints(s) in the editor window, Right-click on the line and select set breakpoint. Two option appears in the Right-click pop up, which are
Set Breakpoint: Set a breakpoint on the current line.
Clear Breakpoint: Clear the breakpoint on that line.
Python Interpreter will pause at the line where the breakpoint is set.
Customizing or Styling Python IDLE
Customizing options provide a lot of choices to design IDLE components such as font, highlight, keyword, theme. To access the customization window, select Options → Configure IDLE from the menu bar. To preview the result of a change you want to make, press Apply. When you’re done customizing, press OK button to save all of your changes.
There are 5 tabs inside Configure IDLE windows and mostly people uses four tabs for the customization purpose. Tab list are,
Font/Tab have the option to customize font that you can apply for your code. Apply colour, size and font style. You can also customize the number of spaces when the Tab button is pressed on the keyboard.
Highlight enables users to visually distinguish between the different Python constructs and the data used in the code. You can set a specific colour to the code components such as variable, text, function, class, keywords etc.. You can choose themes as well as customize foreground and background colours. Find the image below to see options available in Font and Highlight tab.
Key tabs show a list of keyboard shortcuts that are pre-defined for a particular OS. You can either come up with your keyboard shortcuts, or you can use the ones that come with IDLE.
General tab allows users to customize things like the window size and whether the shell or the file editor opens first when you start Python IDLE. Most of the things in this window are not required to change. Find the image below which shows various options under Key and General tab.
In this tutorial, you have learned about IDLE, it features, components. Now, I am sure that you can use IDLE to write new python code or open an existing script and execute them. You have also learned how to improve your programming efficiency by leveraging inbuilt tool features such as automatic indentation, call tips and automatic code completion. Debugging code also made easy with Interpreter debugger as well as breakpoints. Finally, you have also learned to customize IDLE for the giving it a better visual look with the help of fonts, colour, size as well highlights. So, its time to practice your code on Python IDLE.
Keep Learning and keep growing.