Step-by-Step learning of Python: Run, edit, and debug idle

Source: Internet
Author: User

Idle is an integrated development environment that comes with the python software package. It allows beginners to easily create, run, test, and debug Python programs. This article describes the idle GUI, Python shell, editor, and debugger in detail, helping beginners get started quickly.

1. Install idleIn fact, idle is installed with python, but make sure that the "Tcl/tk" component is selected when the installation screen shown in Figure 1 appears. To be accurate, do not cancel this component because it is selected by default.

Figure 1 idle Installation

Ii. startup of idleAfter installing python, we can start idle from the "Start" menu → "All Programs" → "Python 2.5" → "idle (Python GUI. The initial window after the idle is started is shown below:

Figure 2 Python shell of idle

As shown in, after the idle is started, the python shell is first visible to us. We can use it to execute Python commands within the idle. In addition, idle also comes with an editor for editing Python programs (or scripts), an interactive interpreter for interpreting and executing Python statements, and a debugger for debugging Python scripts. Next we will start with the idle editor.

 

3. Use idle to create a python ProgramIdle provides developers with many useful features, such as automatic indentation, syntax highlighting, Automatic completion of words, and command history. With the help of these features, it can effectively improve our development efficiency. Next we will introduce these features through an instance. The source code of our sample program is as follows:

# Prompt the user to enter integer1 = raw_input ('enter an integer: ') integer1 = int (integer1) integer2 = raw_input ('enter an integer again :') integer2 = int (integer2)

If integer1 <integer2: print '% d <% d' % (integer1, integer2) else: print' % d ≥ % d' % (integer1, integer2)

We now demonstrate how to use the idle editor to create a python program. To create a new file, select the "new window" menu item from the "file" menu, so that you can enter the program code in the window that appears. Now let's enter the above Code to experience the various conveniences provided by idle.

We will first introduce auto indent. In fact, there are very few languages that focus on Indentation like python. In other languages such as C language, indentation is "better" for code writing, rather than "no ", at best, it is a matter of writing code style by individual. But in Python, indentation is elevated to a syntax level. Composite statements are represented by indentation instead of braces such. The advantage of this is that it reduces the programmer's freedom and facilitates unified style, making it easier for people to read code. To this end, idle provides the automatic indent function, which can position the cursor to the specified blank distance in the next row. When we type the key that corresponds to the control structure, such as if, or enter a keyword that corresponds to the Function Definition, press the Enter key and the idle starts the automatic indent function. As shown in:

Figure 3 Automatic indent function of idle

 

As shown in figure 3, when we press the Enter key after the colon in the row where the if keyword is located, the idle is automatically indented. Generally, idle indent the code to a level, that is, four spaces. If you want to change the default indent, you can select the new indent width option from the "format" menu. For beginners, it should be noted that although the automatic indent function is very convenient, we cannot rely on it completely, because sometimes automatic indentation may not be exactly what we want, so we need to carefully check it.

The so-called syntax highlighting is to display different elements of the Code in different colors. We can see this in figure 3. By default, keywords are displayed in orange, comments are displayed in red, strings are green, definitions and interpreters are output in blue, and console outputs are displayed in brown. These colors are automatically highlighted when you type the code. The advantage of syntax highlighting is that it is easier to differentiate different syntax elements to improve readability. At the same time, syntax highlighting reduces the possibility of errors. For example, if the input variable name is orange, you need to note that the name conflicts with the reserved keyword, so you must change the variable name.

When you enter a part of a word, select "Expand word" from the "edit" menu, or press Alt +/to automatically complete the word. For example:

Figure 4 enter a part of a word

Now we enter the character "I" and press the combination key Alt +/. The idle will automatically complete the word, as shown in the result:

 

Figure 5 automatically completed words by idle

 

In addition, sometimes we only remember the first few letters of the function. What should we do? For example, I want to use the raw_input function to input some content from the standard input device when the program is running. I remember this function name, but because I am too hungry to input the raw letters, the following letters cannot be remembered. It doesn't matter. Select the "show completetions" menu item from the "edit" menu, and the idle will give some tips ,:

Figure 6 use the idle prompt to complete a word

 

Now, you only need to press the Enter key, and the idle will automatically complete this function name. If not, you can go up or down to search for them. After creating the program, select "save" from the "file" menu to save the program. If a new file is created, the "Save as" dialog box is displayed. You can specify the file name and storage location in the dialog box. After saving, the file name is automatically displayed in the blue title bar at the top of the screen. If the file contains content that has not been stored in the disk, the asterisks appear before and after the file name in the title bar.

Iv. Explanation of common editing functionsNow we will introduce the idle options frequently used in writing Python programs. The following lists the options based on different menus for beginners. For the "edit" menu, in addition to the options described above, the common options and explanations are as follows:

Undo: undo the last modification. Redo: Repeat the previous modification. Cut: Cut the selected text to the clipboard. Copy: copy the selected text to the clipboard. Paste: paste the text of the clipboard to the position of the cursor. Find: Find the word or mode in the window. Find in files: searches for words or modes in a specified file. Replace: Replace the word or mode. Go to line: positions the cursor at the beginning of the specified row. For the "format" menu, common options and explanations are as follows: Indent region: shift the selected content to the right level, that is, increase the indentation. Dedent region: shifts the selected content group to the left level to reduce the indentation. Comment out region: changes the selected content into comments. Uncomment region: removes the annotator before each row of the selected content. New indent width: reset the indention width of the indent, range: 2 ~ 16. The width is 2, which is equivalent to 1 space. Expand word: the word is automatically completed. Toggle tabs: Enables or disables a tab.

5. Run the python program in idleTo run the program using idle, you can select the "run module" menu item from the "run" menu. The function of this menu item is to execute the current file. The execution of our sample program is shown in:

Figure 7 running status of the sample program

 

6. Use the idle DebuggerDuring software development, there is always one or more errors, including syntax and logic. For syntax errors, the python interpreter can easily detect them. In this case, it will stop running the program and give an error prompt. For logical errors, the interpreter will be overwhelmed, and the program will continue to run, but the running result will be wrong. Therefore, we often need to debug the program.

The simplest debugging method is to directly display program data. For example, you can use the print statement to display the variable value in some key locations to determine whether an error has occurred. However, this method is troublesome because developers must insert print statements in all suspicious places. After debugging, you must clear all the print statements.

In addition, you can use the debugger for debugging. With the debugger, we can analyze the data of the program to be debugged and monitor the execution process of the program. Debugger functions include pausing program execution, checking and modifying variables, and calling methods without changing program code. Idle also provides a debugger to help developers find logical errors.

The following describes how to use the idle debugger. In the "Python shell" window, click the "Debugger" menu item in the "debug" menu to start the idle interactive debugger. In this case, idle opens the "Debug control" window and outputs "[Debug on]" in the "Python shell" window followed by a ">>>" prompt. In this way, we can use this "Python shell" window as we usually do, except that any input command is allowed under the debugger. You can view local variables, global variables, and other related content in the "Debug control" window. If you want to exit the debugger, click the "Debugger" menu item in the "debug" menu again. The idle closes the "Debug control" window, in the "Python shell" window, output "[debug Off]".

VII. Command history functions of idleCommand history records all commands executed in the command line during a session. At the prompt, you can press Alt + P to retrieve these commands. Each time you press the command, the idle will retrieve the command history from the latest command and display them one by one in the order of command usage. Press the Alt + n key combination to traverse each command in the opposite direction, that is, traverse from the initial command.

VIII. SummaryIdle is an integrated development environment that comes with the python software package. It is very suitable for beginners of Python programming. This article describes how to use idle in the program development process through an example program, hoping to help you learn Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.