Home Backend Development Python Tutorial Installation and use of Python

Installation and use of Python

Apr 05, 2017 pm 01:09 PM

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!"
Copy after login

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!"
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles