Python 3 interpreter

Python 3 Interpreter
On Linux/Unix systems, the default Python version is typically 2.x. We can install Python 3.x in the /usr/local/python3 directory.

After installation, add the path /usr/local/python3/bin to your Linux/Unix operating system’s environment variables. Then, you can start Python 3 by entering the following command in a shell terminal:

$ PATH=$PATH:/usr/local/python3/bin/python3 # Set the environment variable
$ python3 --version
Python 3.4.0

On Windows, you can set the Python environment variable using the following command. Assuming your Python installation is in C:Python34:

set path=%path%;C:python34

Interactive Programming

We can start the Python interpreter by typing “Python” in the command prompt:

$ python3

After executing the above command, the following window will appear:

$ 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 in the Python prompt and press Enter to see the result:

print ("Hello, Python!");

The above command produces the following results:

Hello, Python!

When typing a multi-line construct, continuation lines are required. Let’s take a look at the following if statement:

>>> flag = True
>>> if flag :
... print("flag condition is True!")
... 
flag condition is True!

Scripting

Copy the following code into the hello.py file:

print ("Hello, Python!");

Execute the script with the following command:

python3 hello.py

The output is:

Hello, Python!

On Linux/Unix systems, you can add the following command to the top of your script to make it directly executable like a shell script:

#! /usr/bin/env python3

Then modify the script permissions to allow execution with the following command:

$ chmod +x hello.py

Execute the following command:

./hello.py

The output is:

Hello, Python!

Leave a Reply

Your email address will not be published. Required fields are marked *