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 variables of your Linux/Unix operating system, so that you can enter the following command through the shell terminal to start Python3.

$ PATH=$PATH:/usr/local/python3/bin/python3 # Set environment variables
$ python3 --version
Python 3.7.9

Under Window system, you can use the following commands to set Python environment variables, assuming that your Python is installed under C:\Python37:

set path=%path%;C:\python37

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 message appears:

$ python3
Python 3.7.9 (default, Apr 31 2020, 17:10: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 then press Enter 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("The flag condition is True!")
...
The flag condition is True!

Scripted Programming

Copy the following code to the hello.py file:

print("Hello, Python!"); 

Execute the script with the following command:

python3 hello.py

The output is:

Hello, Python!

In Linux/Unix systems, you can add the following command at the top of the script to make Python scripts executable directly like SHELL scripts:

#! /usr/bin/env python3

Then modify the script permissions to have execute permissions, the command is as follows:

$ chmod +x hello.py

Execute the following commands:

./hello.py

The output is:

Hello, Python!