Installation and Use of Python

Source: Internet
Author: User
Python installation and use is a powerful dynamic language for interpreting and executing automatic compilation. It also supports process-oriented programming and flexible and convenient data structures, with 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, technologies based on this language are rapidly developing, and the number of users is rapidly expanding.

This article uses Windows as the working platform, introduces Python installation and interpreter usage, and uses the most common "Hello, world! "The sample program explains how to write and run Python programs. Finally, I will give a brief introduction to the implementation features of Python.

1. install Python

When installing Python, you can install Python from both the source code and the compiled and packaged binary version. Obviously, the latter is relatively simple.

Python installer can be downloaded from official site point http://www.python.org for free, here we choose the installation package for Python-2.7.2.msi.

After downloading the SDK, double-click it to install it as prompted. This document assumes that your Python installation directory is D: \ Python.

II. use the Python interpreter

The interpreter of Python is used to execute a Python statement. each time a statement is read, a specific operation is executed based on the statement. You can also put all the statements to be executed in a file (that is, a Python program file. py is used as the extension. in this case, the interpreter of Python is executed in order. all the statements in the py file.

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

Now we use the "Hello, world !" For example. After the interpreter is started, enter the following sentence at its prompt (Python prompt >>>:

print "Hello,world!"

This sentence enables Python to display "Hello, world!" on the screen !". Press the Enter key to output hello world on the screen.

I believe that many people who use C programming will feel awkward after reading it, because it does not look like programming at all, 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 textprint "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 it is important to add necessary comments to the program. The second line is a blank line. here, empty lines are used to separate the comments and code sections, which makes the program more readable. The third line is the main character of the program. a print statement is used to display strings between quotation marks on a computer. 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.

4. run the program

First, modify the Path environment variable in windows. assume that your python installation directory is in D: \ python. The steps are as follows:

Right-click "My Computer" => "properties" => "advanced" => "environment variable" => "a Path exists in the system variable" and double-click it to open it, add "; D: \ Python" at the end ".

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

cd D:\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

On Windows, if you set the Association of the. py file, you can run the program directly by entering the file name without entering the python command.

hello.py

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. 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. 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 (). 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.

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.

Related Documents: install Python and Django

The above describes how to install and use Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.