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 the installation is complete, we can add the path /usr/local/python3/bin to the environment variable of your Linux/Unix operating system, so that you can enter it through the shell terminal Use the following command to start Python3.

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

Under Windows system, you can set the Python environment variables through the following command. Assume that your Python is installed under C:\Python34:

set path=%path%;C:\python34

Interactive programming

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

$ python3

After executing 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.
>>>

In the python prompt Enter the following statement in , and then press the Enter key to see the running effect:

print ("Hello, Python!");

The execution result of the above command is as follows:

Hello, Python!

When typing a multi-line structure, line continuation is necessary. We can look at the following if statement:

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

Script programming

Copy the following code to the hello.py file:

print ("Hello, Python!");

Execute the script through the following command:

python3 hello.py

The output result is:

Hello, Python!

In Linux/Unix systems, you can add the following command at the top of the script to make the Python script as accessible as the SHELL script Execute directly:

#! /usr/bin/env python3

Then modify the script permissions so that it has execution permissions. The command is as follows:

$ chmod +x hello.py

Execute the following command:

./hello.py

The output result is:

Hello, Python!