Home  >  Article  >  Backend Development  >  Installation and use of Python

Installation and use of Python

巴扎黑
巴扎黑Original
2017-04-05 13:09:361632browse

Python is a powerful dynamic language that interprets, executes and automatically compiles. It is object-oriented and also supports process-oriented programming. It has flexible and convenient data structures, a large number of excellent modules, clear and concise syntax, and can be used on many platforms. Running on it, it is very suitable for completing various high-level tasks. Currently, related technologies based on this language are developing rapidly, and the number of users is expanding rapidly.

This article uses Windows system as the working platform, introduces the installation of Python and the use of the interpreter, and uses the most common "Hello, world!" sample program to explain the writing and running of Python programs to readers. Finally, a brief introduction to Python's interpretation and execution features is given.

1. Install Python

When installing Python, you can either install it from source code or use a compiled and packaged binary version. Obviously, the latter is simpler.

The Python installation program can be downloaded for free from the official site http://www.python.org. The installation package we chose here is Python-2.7.2.msi.

After downloading, just double-click and follow the prompts to install. It is very simple. This article assumes that your Python installation directory is D:\Python.

2. Using the Python interpreter

The Python interpreter is used to execute Python statements. It reads one statement at a time and performs specific operations based on this statement. You can also put all the statements to be executed into a file (that is, Python program file). These files usually have a .py extension. In this case, the Python interpreter will execute all the statements in the .py file in order.

After installing Python, you can start the Python interpreter from the "Start" menu => "Programs" => "Python 2.7" => "Python (command line)".

Now let's take the customary "Hello, world!" as an example. After starting the interpreter, we can enter the following sentence directly after its prompt (Python's prompt is >>>):

	print "Hello,world!"

This sentence tells Python to display "Hello, world!" on the screen. After pressing the Enter key, the output hello world on the screen.

I believe that many people who use C language programming must feel very awkward after watching this, because it doesn’t look like programming at all. In this case, let’s complete this simplest programming in a more programming way. Task.

3. Program "Hello, world!"

First, open Notepad and type the following statement in it:

	#Printing a line of text

	print "Hello,world!"

Then, save this file as hello.py. Pay attention to the suffix here. Generally, the suffix of Python program files is set to .py. This program is very simple, only three lines. Let's look at the first line first. It starts with #, which means this line is a comment. We know that it is important to add necessary comments to the program. The second line is a blank line. A blank line is used here to separate the comment part and the code part, which will make the program more readable. The third line is the protagonist of this program, a print statement. Its function is to let the computer display the string between the quotation marks. Note that the end of the sentence here does not end with a semicolon ";" like in C language. Python adds nothing at the end of the statement.

4. Run the program

First modify the Windows system Path environment variable. Assume that your python installation directory is in D:\python. The steps are as follows:

Right-click "My Computer" => "Properties" => "Advanced" => "Environment Variables" => "There is a Path in the system variables", double-click to open, and add ";D:" at the end: \Python".

To run a Python program, you can switch to the directory where the Python program file is located at the dos prompt. Assuming that our sample program hello.py is located in the D:\test directory, you can use the following command:

	cd D:\test

Then, enter python at the command prompt, followed by the name of the program to be executed. If you want to run hello.py, you can use the following command:

        python hello.py

On the Windows platform, if you set up an association with a .py file, you don’t even need to enter the python command, you can directly enter the file name to run the program.

	hello.py

So far, we have used two different ways to execute Python statements. The first way is to directly enter the statement into the interpreter to execute it. This interactive mode can only use one statement at a time. The second way is to put the statements to be executed into a file with a .py suffix, and then let the Python interpreter execute the code in the file. For the second method, when the Python interpreter executes the code stored in the file, it will start from the first line of the file and interpret and execute it line by line until the end of the file.

5. Explanation and execution

The two execution methods of Python statements are discussed above. In fact, these two execution methods are essentially the same. They are both interpreted and executed by the interpreter to execute the Python statements we provide.
The interpretation execution mentioned here is relative to the compilation execution. We know that a program written in a compiled language such as C or C++ can be converted from source files into machine language used by the computer, and then connected by a connector to form a binary executable file. When we run a binary executable program, because it has been compiled, the loader software loads the binary program from the hard disk into memory and runs it.

In contrast, programs written in the Python language do not need to be compiled into binary code. It can run the program directly from the source code. When we run a Python file program, the Python interpreter converts the source code into an intermediate form: bytecode, and then the Python virtual machine executes these bytecodes (as shown in the figure). In this case, we don't have to worry about program compilation, library connection and loading, etc. All these tasks will be taken care of by the Python virtual machine.

For the interpreted language features of Python, we have to look at it in two. On the one hand, it must be converted into bytecode every time it is run, and then the bytecode is converted into machine language by the virtual machine, and finally it can be run on the hardware. Compared with compiled programming languages, each run will require two more processes, so its performance will be affected. On the other hand, since you don’t have to worry about program compilation and library connection issues, development work will become easier; at the same time, the virtual machine is further away from the physical machine, so Python programs are easier to transplant and can actually be used without modification. Runs on multiple platforms.

Related documents: Installation steps for Python and Django

The above is the detailed content of Installation and use of Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn