Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 Interpreter

Python3 Interpreter

On Linux/Unix systems the default python version is generally 2.x; we can install python3.x in the /usr/local/python3 directory.

After installation, we can add the path /usr/local/python3/bin to the environment variables of your Linux/Unix operating system, so that you can start Python3 by entering the following command in a shell terminal.


$ PATH=$PATH:/usr/local/python3/bin/python3    # 设置环境变量
$ python3 --version
Python 3.4.0

On Window systems you can set the Python environment variables with the following command, assuming Python is installed in C:\Python34:


set path=%path%;C:\python34

Interactive programming

We can enter the "Python" command at the command prompt to start the Python interpreter:


$ python3

After running the above command, the following window information appears:


$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Enter the following statement at the python prompt and press Enter to see the result:


print ("Hello, Python!");

The result of the above command is as follows:


Hello, Python!

When typing a multi-line structure, continuation lines are required. Consider the following if statement:


>>> flag = True
>>> if flag :
...     print("flag 条件为 True!")
... 
flag 条件为 True!

Script programming

Copy the following code into the hello.pyfile:


print ("Hello, Python!");

Run the script with the following command:


python3 hello.py

The output is:


Hello, Python!

On Linux/Unix systems you can add the following command at the top of a script so that a Python script can be executed directly like a SHELL script:


#! /usr/bin/env python3

Then change the script permissions to make it executable with the following command:


$ chmod +x hello.py

Execute the following command:


./hello.py

The output is:


Hello, Python!