Python3 Interpreter
On Linux/Unix systems, the default python version is , and we can install it in the /usr/local/python3 directory.
Once 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 start Python3 by typing the following command in a shell terminal.
$ PATH=$PATH:/usr/local/python3/bin/python3 # Setting environment variables $ python3 --version Python 3.4.0
On Window systems you can set the environment variables for Python with the following command, assuming your Python installation is under C:\Python34.
set path=%path%;C:\python34
interactive programming
We can start the Python interpreter by typing the command "Python" at the command prompt:
$ python3
After executing the above command, the following window message 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. >>>
Type the following statement at the python prompt and press Enter to see how it runs:
print ("Hello, Python!");
The result of the above command is as follows:
Hello, Python!
When typing a multi-line structure, line continuation is mandatory. We can look at the following if statement:
>>> flag = True >>> if flag : ... print("flag condition is True!") ... flag conditioned on True!
scripted programming
Copy the following code into the file:
print ("Hello, Python!");
Execute the script with the following command:
python3
The output result is:
Hello, Python!
On Linux/Unix systems, you can add the following command to the top of your scripts to make Python scripts directly executable like SHELL scripts:
#! /usr/bin/env python3
Then modify the script permissions so that it has execute permission with the following command:
$ chmod +x
Execute the following command:
./
The output result is:
Hello, Python!
There is more than one Python interpreter: CPython, IPython, Jython, PyPy, and so on.
As the name suggests, CPython is developed in C. It is the official standard implementation and has a good ecosystem, so it is the most widely used.
IPython, on the other hand, is an interpreter (/) based on CPython with enhanced interactivity.
Jython is a Python interpreter (/) designed for the Java platform that compiles Python code into Java bytecode for execution.
PyPy is a fast, compatible alternative implementation (/) of the Python language (2.7.13 and 3.5.3), known for its speed.
Type $ python in the cmd window and get a runtime error:
Python often works on Unix or Linux, and the $ at the beginning of the code indicates a UNIX or Mac OS command prompt. The $ means "prompt the user for a command line," and the $ itself is not part of the command statement being typed. $ is not required to be typed.
Python has two programming modes: interactive, and scripted.
For interactive programming, we need to open a cmd window (command prompt window) and type python, enter, and we are in interactive programming. At this point, we can just type in the python statement to get the result of the run:
Scripted programming is where we write python statements, save them in a file with a .py extension, and then call that file from outside. It can also be called using the cmd window, but unlike interactive programming, you don't enter python in the cmd window with a carriage return to enter interactive mode.
If we want to call the file in the cmd window, we just need to transfer the cmd path directory to the folder where it is located, and then enter the command
Let's say we have a file on drive D with the path D:\Python27\Mytest\ Then to call this file in the cmd window, we need to switch the directory path to D:\Python27\Mytest. this can be done by using the cd command.
The following are brief additions:
The cmd window opens by: right-clicking the start menu and selecting 'Command Prompt (Administrator)'. Or from the start menu ->Run-> type cmd, enter.
About the cd Command: Used to change the current directory path. Usage: cd[space][path]
For example, cd d:/Python27/Mytest goes to this path
Note: If the current drive letter is not D, you need to go to D first and type d: to enter. Then you can use cd d:/Python27/Mytest.