If you have been exposed to Python, you know that there are many ways to run Python scripts. Currently, the main ways are: interactive environment, command line window, development tools, etc., which are different on different operating platforms. Today, I will cover the basics of Python, using the Windows interactive environment as the basis to demonstrate the running of Python programs.
In general, after a successful Python installation, there are two ways to enter a Python interactive environment. One is to open the interactive window directly in IDLE, which comes with Python, as shown below:
The other option is to open the Start menu, enter CMD, enter the command line window, enter Python, and press Enter to enter the interactive environment, as shown below:
Enter a programming statement after the >>> prompt, then press Enter to execute, and you will get the result of the run. In a nutshell, the interactive environment is a bit like a calculator, executing one statement at a time and saving the results. Here are some examples:
Note that the special variable “_”, which in Python’s interactive environment means the result of a previous operation, is usually used in programs to construct new expressions from which further calculations can be made. In an interactive environment, we can use constants directly or define variables, but we must define variables first, otherwise we will have a naming error. For example, in the last line of code, a is not defined, so the error is reported, but length and width are not reported, because they are defined.
In the Python interactive environment, if there is a syntax error, the program will report an error and get an error message. This will help us easily find the number of lines and the content of the error
In an interactive environment, typed code is not saved, and when closing Python to get the run window, previously typed code is not saved. However, pressing up and down keys on the keyboard in an interactive environment can look for historical commands, but it is a temporary memory, when exiting the program, these commands will not exist.
If you need to write long programs, using the command line is not very convenient. At this point you can edit the script using a text editor. In IDLE, click file in the upper left and select The New Window menu to create a New window, as shown below:
You can write your code here, but be careful about indentation, or you’ll often get indentation errors. Indentation is very important in Python, because different indentation reflects the relationship between different levels of a block of statements. The prepared file needs to be saved (Ctrl+ S) and then saved as an extension of.py. After saving, press the shortcut key F5 to run the program, and then you can see the result of running in IDLE window. Of course, you can also right click and hit Run to run the program. Learn more about Crawlers and data mining in Python at pdcfighting.com/