Python tutorials (Python learning route): Teaches you how to execute Python program in an interactive environment

Python tutorials (Python learning route): Teaches you how to execute Python program in an interactive environment

I believe contact with Python pals know that there are a variety of ways to run Python scripts, the main methods are: interactive environment to run the command line window, run, and other development tools, which operate on different platforms but also mutual Not the same. Today, small talk about some Python-based content to the Windows interactive environment as the basis, the demo run Python programs.

In general, after the successful installation of Python, there are two ways to enter the Python interactive environment. One is open IDLE Python comes directly interactive window, as shown below:

Python tutorial: teach you how to execute Python program in an interactive environment

 

Another is to open the Start menu, then input cmd, enter the command line window, then the input Python, after a carriage return will be able to enter the interactive environment, as shown below:

 

Python tutorial: teach you how to execute Python program in an interactive environment

 

Behind >>> prompt, enter the programming statements and press enter execution, you will get results run. Simple point of view, this interactive environment is somewhat similar to a calculator, one statement, but also can save the results. The figure is part of the operation examples:

 

Python tutorial: teach you how to execute Python program in an interactive environment

 

Note that the special variable "_", which means in the Python interactive environment is the result of the preceding operation, which is generally used in the expression configured new program, on the basis of further calculations. In an interactive environment, we can directly use constants, variables can also be defined, but must be pre-define the variables are, or naming error occurs, as in the example of the last line of code in a not defined, so the error , but the length and width are not being given, because he has defined.

In Python interactive environment, if there is a syntax error, program error after running, and will get an error message, can help us easily find the content of error rows and error, as long as we can prescribe the right medicine to cure the patient friends , bug every minute to get rid ~~

In an interactive environment, the input code is not preserved when the window closing operation obtained Python code previously entered will not be saved. But in an interactive environment by pressing the arrow keys in the keyboard, you can find the history command, but it is short of memory, when exiting the program, these commands will cease to exist.

If you need a longer time to write a program, use the command line is less convenient. At this point you can use a text editor to edit the script. IDLE upper left click File, New window and select the menu, you can create a window of, as shown below:

 

Python tutorial: teach you how to execute Python program in an interactive environment

 

Here, you can write your code, but be sure to pay attention to indent, otherwise it will often appear indentation (indentation) error in Python indentation statement is important because different indentation statements reflect the different blocks the relationship between the levels. Writing good file needs to be saved (Ctrl + s), then to expand the .py will be saved. After the save, press the shortcut key F5 to run the program, then you can see the results running in IDLE window. Of course, you can also right-click run, you can run the program.

The latter will then share with you more Python tutorials and articles summarize, we must keep learning, insist on doing exercises!

Guess you like

Origin www.cnblogs.com/cherry-tang/p/10944463.html