Python is a dynamic language with powerful functions to explain and execute automatic compilation. It also supports process-oriented programming while providing flexible and convenient data structures and a large number of excellent modules, the syntax is clear and concise, and can be run on many platforms. It is very suitable for completing various high-level tasks. At present, the related technologies based on this language are rapidly developing, and the number of users is rapidly expanding. In view of this, the author will introduce the knowledge of Python programming to readers from the beginning. Although Python supports multiple platforms, we use Windows as our work platform. Now, let's start with preparation!
1. Install Python
When installing python, you can install python from both the source code and the compiled and packaged binary version. Apparently, the latter is relatively simple, and its installer can be downloaded free of charge from the Web site http://www.python.org, for Windows systems, the current latest installation package is a Python-2.5.2.msi. After downloading the SDK, double-click it to install it as prompted.
Ii. Use the python Interpreter
For beginners, you can simply regard the python interpreter as an "agent": let us tell what we want to do with this "agent, then it will complete the work according to our instructions. Note that the method to tell Python what to do is to use the python command, that is, the statement. We can tell it step by step that the python interpreter will explain and execute it in one sentence; you can also put the statements to be executed into a file (that is, a python program file. the Python interpreter executes all the statements in the file in sequence. The aforementioned "agent" can understand "means to conform to the python syntax, which will be explained in later articles.
Now we use the "Hello, world !" Let's start our Python journey. After installing python, you can start the python interpreter from the "Start" menu> "All Programs"> "Python 2.5"> "Python (command line. However, this operation is troublesome. Therefore, it is recommended that you right-click the python icon under the python installation directory and select "create shortcut" from the pop-up menu (see figure 1 ), then copy the created shortcut icon to the desktop, which will be much more convenient to use later.
Figure 1 create a shortcut for the python Interpreter
After the interpreter is started, enter the following sentence at its prompt (Python prompt >>>:
Print 'hello, world! '
As you may have guessed, this is to make Python display "Hello, world!" on the screen !". After you press the Enter key, the output on the screen is as follows:
Figure 2 "Hello, world!" Interactive execution in Python !"
I believe that many people who use C programming will feel awkward after reading it, because it does not seem to be programming at all, and it is simply using a calculator. In fact, the python interpreter is a bit like a "Calculator". For example, enter the following sentence at the prompt:
Print 8 + 8
The interpreter output is as follows:
Figure 3 use the interpreter as a calculator
Well, in this case, we will change to a programming method to complete this simplest programming task.
3. Program "Hello, world !"
First, open notepad and enter the following statement:
# Printing a line of text
Print 'hello, world! '
Then, save the file as hello. py. Note the suffix here. Generally, the extension of the python program file is set to. py. This program is very simple with only three lines. Let's first look at the first line. It starts with #, which indicates that this line is a comment. We know that adding necessary comments to a program is very important. One can help others understand our program, and the other can help us maintain it in the future, because over time, the meaning of the code written at the beginning may be forgotten in the memory, and the original comments will be of great help.
A comment starting with # is called a single line comment, which ends at the end of the line. However, a single line comment can start not only at the beginning of a line, but also at the middle of a line, for example, to annotate the code of the line following the code. But in any case, python will not convert it into actual operations.
It is an empty line after the comment line. We can separate code segments of different functions with blank lines. Here we use blank lines to separate the comments and code sections, which makes the program more readable.
The fifth line is the main character of this program. A print statement is used to display strings between quotation marks on a computer. A string, as its name implies, is a string of characters, but these characters are sequential. Note that the end of the sentence here is not as ending with a semicolon ";" as the C language, and there is no additional at the end of the python statement. Unlike the comment line, this line is converted to the actual operation by python, that is, a line of text is displayed on the screen: "Hello, world", as shown in, you will find that the quotation marks in the print statement are not in the output column.
Figure 4 Execution of program Hello. py
4. Run the program
To run a python program, you can switch to the directory where the python program file is located at the DOS prompt. For example, the example program is located in the C: \ python25 \ test directory, run the following command:
Cd c: \ python25 \ test
Then, enter python in the command prompt, followed by the name of the program to be executed. To run Hello. py, run the following command:
Python hello. py
See Figure 4.
So far, we have used two different methods to execute Python statements. The first method is to directly input a statement in the interpreter for execution. In this mode, only one statement can be entered at a time, as shown in figure 2. The second method is to put the statement to be executed into a file with the suffix. py, and then let the python interpreter execute the code in the file, as shown in figure 4. For the second method, when the python interpreter executes the code stored in the file, it will explain and execute from the first line of the file, one line after another, until the end of the file.
5. Explain and execute
As mentioned above, the two execution methods of Python statements are essentially the same. They are all interpreted by the interpreter to execute the python statements we provide.
The explain execution mentioned here is relative to the compilation and execution. We know that a program written in a compilation language such as C or C ++ can be converted from the source file to the machine language used by the computer, and a binary executable file is formed after the connector connection. When we run the binary executable program, because it has been compiled, the loader software loads the binary program from the hard disk into the memory and runs it.
In contrast, a program written in Python does not need to be compiled into binary code. It can run programs directly from the source code. When we run the python file program, the python interpreter converts the source code to the intermediate form: bytecode, which is then executed by the python virtual machine, as shown in Figure 5. In this way, we don't need to worry about program compilation, library connection loading, and other issues. All these work is done by the python virtual machine.
Figure 5 explain and execute Python
Let's take a look at Python's Interpretation Language Features in two ways. On the one hand, each runtime must be converted to bytecode, and then the virtual machine will convert the bytecode to the machine language before running on the hardware. Compared with the compiled programming language, each operation has two more processes, so its performance will be affected. On the other hand, because you don't need to worry about program compilation and Database Connection issues, the development work will become easier. At the same time, the virtual machine is farther away from the physical machine, so the python program is easier to transplant, in fact, it can run on multiple platforms without modification.
Vi. Summary
This article introduces Python installation and interpreter usage, and uses the most common "Hello, world !" The sample program explains how to write and run a python program. Finally, I will give a brief introduction to the implementation features of Python. Readers may have discovered that python programming is simple and easy to use. As for more content, we will introduce it in subsequent articles.