Getting started with Python 3.2 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.Program.

1. Install idle
In fact, idle is installed with python, but make sure that the "Tcl/tk" component is selected during installation. To be precise, do not cancel this component, this component is selected by default.
Ii. startup of idle
After installing python, we can start idle from the "Start" menu → "All Programs" → "Python 2.7" → "idle (Python GUI. The initial window after the idle is started is shown below:
 
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. Create a python program using idle
idle 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 example program is as follows:

# prompt the user for input
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, in this way, you can enter the Code of the program in the displayed window. 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:


After we press the Enter key next to 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.

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.

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 functions
Now 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 last 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 words or modes in the window

Find in files: searches for words or modes in a specified file.

Replace: replace a word or pattern

Go to line: positions the cursor at the beginning of the specified row.

Common options and explanations for the "format" menu are as follows:

Indent region: shifts the selected content one level to the right, that is, increasing the indentation.

Dedent region: shifts the selected content group to a level left, that is, reducing indentation.

Comment out region: changes the selected content into comments.

Uncomment region: removes the annotator before each line 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: Automatic completion of words

Toggle tabs: Enables or disables a tab.

5. Run the python program in idle
To 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:

6. Use the idle Debugger
During 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, you can click the "Debugger" menu item in the "debug" menu again. Idle will disable the "debug"
Control window, and output "[debug Off]" in the "Python shell" window.

VII. Command history functions of idle
Command 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. Summary
Idle 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 Process of program development through an example program, hoping to help you learn Python programming.

Related Article

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.