Reprint: http://www.cnblogs.com/dsky/archive/2012/06/04/2535397.html
1, Idle is a python package comes with an integrated development environment, beginners can use it to easily create, run, test and debug Python programs.
First, idle installation in fact, idle is installed with Python, but to ensure that the "TCL/TK" component is selected during installation, it should be accurate to not cancel the component because the component is selected by default. Second, idle startup after the installation of Python, we can start from the Start menu → "All Programs" → "python 2.7" → "Idle (python GUI)" to activate the IDLE. The initial window after the idle startup is shown: As shown, the Python shell, which starts idle and first looks into our eyes, allows us to execute the python command inside the idle. In addition, Idle also comes with an editor for editing a Python program (or script), an interactive interpreter to interpret the execution of a Python statement, and a debugger to debug a Python script. Let's start with the idle editor.
third, the use of idle to create a python program idle provides developers with many useful features, such as automatic indentation, syntax highlighting , Word AutoComplete and command history, and so on, with the help of these functions, can effectively improve our development efficiency. Here we introduce each of these features in an example. The source code for our sample program is as follows:
#提示用户进行输入 integer1 = Raw_input (' Please enter an integer: ') integer1 = Int (integer1) integer2 = Raw_input (' Please enter an integer again: ') integer2 = Int (integer2) if integer1>integer2: print '%d >%d '% (integer1,integer2) else : print '%d <=%d '% (integer1,integer2) We now show you how to use the idle editor to create a Python program. To create a new file, first select the "New Window" menu item from the "File" menu so that you can enter the program's code in the window that appears. Now let's enter the code above to experience the various conveniences offered by idle in person.
we first introduce automatic indentation. In fact, very few languages are as important as python, and in other languages such as C, Indentation is "better" for code writing, rather than "No No," which is at best a matter of style for a personal writing code; But here in the Python language, The indentation is raised to the height of a grammar. A compound statement is not represented by a symbol such as curly braces {}, but by indentation. The advantage of this is that it reduces the programmer's freedom and facilitates a unified style, making it easier for people to read code. To do this, idle provides an auto-indent function that positions the cursor at the specified margin on the next line. When we type the key corresponding to the control structure, such as if, or enter the keyword corresponding to the function definition such as DEF, the Auto Indent function will start when the ENTER key is pressed. As shown in the following:
When we press ENTER after the colon in the line where the IF keyword is located, idle automatically indents. In general, idle indents the code one level, or 4 spaces. If you want to change this default indent, you can modify it by selecting the "New indent width" item from the "Format" menu. For beginners, it is important to note that although the Auto Indent function is very convenient, we cannot rely entirely on it, because sometimes automatic indentation does not necessarily fit our mind, so we need to examine it carefully.
The so-called syntax highlighting, is to give different elements of the code to use a different color to display, about this, we have seen from Figure 3. By default, the keyword is displayed as orange, the note is red, the string is green, the output of the definition and interpreter is blue, and the console output is shown in brown. These color highlights are automatically applied when you type your code. The advantage of syntax highlighting is that it makes it easier to distinguish between different grammatical elements, which improves readability, while syntax highlighting also reduces the likelihood of errors. For example, if the variable name you enter is displayed as orange, you need to be aware that the name conflicts with the reserved keyword, so you must change the name for the variable.
Word autocomplete means that when a user enters a part of a word, select the Expand Word item from the Edit menu, or press alt+/to automatically complete the word.
Also, sometimes we just remember the first few letters of the function, what do we do? For example, I want to use the Raw_input function in the program to type something from the standard input device, originally this function name I remember, but because the belly is too hungry input raw these three letters, the following few letters but how can not remember, do not matter, from the "Edit" menu select "Show Completetions "menu item, idle will give some hints.
Now just press ENTER and idle will automatically complete this function name. If it is not appropriate, you can also find the up and DOWN ARROW keys. after the program is created, choose Save from the File menu to save the program. In the case of a new file, the "Save as" dialog box pops up, where we can specify the file name and save location. When saved, the file name is automatically displayed in the blue title bar at the top of the screen. If there is content in the file that has not been saved, an asterisk appears before and after the file name of the title bar.
Iv. General editing functions we will now introduce the idle options that are commonly used when writing Python programs, listed below in different menus for beginners ' reference. For the "Edit" menu, in addition to the several options described above, common options and explanations are as follows:
undo: Undo the last modification
Redo: Repeat the last modification
Cut: Cuts the selected text to the Clipboard
Copy: Copy the selected text to the Clipboard
Paste: Pasting the text of the Clipboard to the cursor location
Find: Finding words or patterns in a window
Find in Files: finding words or patterns in a specified file
Replace: replacing words or patterns
Go to line: positions the cursor at the beginning of the specified line.
For the Format menu, the common options and explanations are as follows
Indent Region: Increases indent by moving the selection to the right level
Dedent region: Reduces indent by moving the selected content group to the left-hand level
Comment out region: Changes the selection to a comment
Uncomment region: Remove the comment from the front of each line of the selection
New Indent width: Re-set tab indent width, range 2~16, width 2 equivalent to 1 spaces
Expand Word: Automatic word completion
Toggle tabs: Turns tab stops on or off.
v. Run the Python program in idle to use the idle execution program, you can select the Run Module menu item from the Run menu, which is the function of the current file. For our sample program, the execution is as follows:
Vi. Using the Idle debuggerIn the process of software development, there is always a mistake, which is grammatical and logical. For syntax errors, the Python interpreter can be easily detected, which stops the program from running and gives you an error message. For a logic error, the interpreter is beyond, and the program executes all the time, but the resulting run is wrong. Therefore, we often need to debug the program.
The simplest way to debug is to display program data directly, for example, you can display the value of a variable in some key locations with a print statement to determine if there is an error. But this approach is cumbersome because developers have to insert print statements in all suspicious places. It's too wordy to wait until the program has been debugged and all these print statements must be cleared.
Besides, we can also use the debugger to debug. With the debugger, we can analyze the data of the program being debugged and monitor the program's execution process. Debugger features include suspending program execution, checking and modifying variables, calling methods without changing program code, and so on. Idle also provides a debugger to help developers find logic errors.
Below is a brief introduction to the use of the idle debugger. You can launch the idle Interactive debugger by clicking the "Debugger" menu item in the "Debug" menu in the Python Shell window. At this point, Idle opens the Debug Control window and Prints "[Debug on]" in the Python Shell window followed by a ">>>" prompt. This way, we can use the Python Shell window as usual, except that any commands you enter now are allowed under the debugger. We can view the contents of local variables and global variables in the Debug Control window. If you want to exit the debugger, you can click the Debugger menu item in the Debug menu again, and idle closes the Debug Control window and outputs [Debug OFF] in the Python Shell window.
Seven, idle command history function command history can record all commands that were executed at the command line during a session. At the prompt, you can press Alt+p to retrieve these commands, and each time you press idle, the command history is retrieved from the most recent command, displayed in the order used by the command. By pressing Alt+n, you can traverse individual commands in the opposite direction, starting with the initial command.
VIII, summary idle is a python package comes with an integrated development environment, Ideal for beginners in Python programming. This article introduces in detail the use of idle in the program development process through an example program, hoping to help you learn Python programming.
2. python idle execution py file
Import
You can also use import to run files under idle. such as running test.py file:improt test
However, for a file,Improt can only run the file the first time it is imported. After the first import, no other import will work anymore, even changing and saving the module's source code file in another window. Experiment, found after restarting Idel still not. This is the result of intentional design. Importing is an expensive operation so that each program cannot repeat more than 1 times.
Reload
However, if you want python to run the file again in the same session, you need to use the built-in reload (overloaded) function.
The invocation format is:reload (test)
Using reload, the output will contain <module ' test ' from ' C:/python27/practice code/test.pyc ' >.
This is because reload is a function, and import is a statement. The call requires passing the module name to the reload function as a parameter in parentheses, so an extra line of output is obtained when overloading. The last line of output is the print display of the return value after the reload call, andthe return value of the reload function is a Python module object. But for a program that has not been modified, calling the reload function will only get a printed display of a line similar to the return value above.
It is necessary to note that if import is not used , thereload function will make an error. This means that the reload function needs to be used with import already in use . You can use alt-p,alt-n Roll back to the previous command, and there is no clear screen option in idle.
ExecFile
Another way to run a file through interactive hint mode is built-in function execfile (' test.py '). By default, a new file is run every time execfile is called, but technically it does not import the module.
Module import is a method module that runs code files and is the largest program structure in a Python program. python programs often have multiple modules that are connected by import statements. Each module file is a self-contained variable wrapper, which is a namespace. A module file cannot see the variable name defined by other files unless he explicitly imports that file, so the module file plays a role in minimizing the naming conflict in the code file.
If a link error occurs, try starting IDLE through the mode of a single process . idle requires communication between its independent user and GUI processes, and sometimes a startup error occurs on a particular platform. If you encounter such a link error, it is often possible to use the system command to exercise the idle run in a single mode to start, thus avoiding the problem of communication:-n command line flag can be forced into this mode. For example, in the Windows Lie feature, you can run System command test.py–n.
3. Python ebook
Concise Python Tutorials (get started with a look)
Http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html
Python Learning Manual Third edition Chinese (introductory book, Easy to understand)
Http://www.jb51.net/books/22761.html
Python Core Programming second Edition Chinese (translation was robbed, OK, we don't care about it)
Http://www.jb51.net/books/20775.html
Dive into Python Chinese version (good book)
http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/pythonhtml/html/
Dive into Python 3 (latest English version)
Http://www.e-bookz.cn/book/dive-into-python-3.html
Python Cookbook Chinese version (good book, translation not finished, ZQ continuous update, can be concerned about)
Http://wiki.woodpecker.org.cn/moin/PyCookbook
Python Cookbook English CHM
Http://www.infoxa.com/asp/book_file/xxnr_book_2300.htm
Cute python (python excellent extracurricular reading, community work)
Http://wiki.woodpecker.org.cn/moin/ObpLovelyPython
Python Chinese Manual (Some references)
Http://www.myebook.cn/ebook/dnwl/cxsj/qt/2008/304343818785.htm
Python source Profiling (learn more about dynamic languages)
Http://www.e-bookz.cn/book/python-yuanma-pouxi.html
Expert python Programming (Python advanced programming)
Http://www.ebookee.net/Expert-Python-Programming_217684.html
Python Standard library Manual
Http://wiki.woodpecker.org.cn/moin/PythonStandardLib
The Django Book
http://djangobook.py3k.cn/
Python Concise Regular Expression tutorial
http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D%97
Python advanced Programming (Expert Python programming)
Http://www.ebookee.net/Expert-Python-Programming_217684.html
Html&css
http://zh.html.net/
Python Book Summary
Http://club.topsage.com/thread-361615-1-1.html
http://www.verycd.com/topics/2762837/
2. Python Idle Entry