Loading, please wait...

A to Z Full Forms and Acronyms

What is Python Interpreter?

May 25, 2020 Python Interpreter, Python, 3167 Views
In this article will discuss briefly python interpreter briefly.

Python Interpreter:

The word “interpreter” can be used in various ways when using Python. Sometimes interpreter refers to the Python REPL(Read-Evaluate-Print-Loop) fashion, the interactive prompt you get by typing python at the command line. Sometimes people use the “Python Interpreter” as “Python” to talk about executing Python code from start to finish.

Python Interpreter and its Environment (Source Code Encoding)

The default encoding of a Python source file is UTF-8 which is a Unicode Standard variable-width character encoding. It can encode 1,112,064 valid points in Unicode using up to four 8 bit bytes. Using this encoding, we can use characters of most languages in string literals, comments, and identifiers. The standard library uses ASCII characters only so you must declare the use of this encoding to your editor to ensure that all such similar characters display without a problem and the font should be such that it supports all characters in the file.

To declare an encoding other than the default, a special line should be added as the first line of the file.

Syntax: #  -*-  coding: encoding  -*-

Where encoding is one of the valid codecs supported by Python.

For example, to declare that Windows- 1252 encoding is going to be used, the first line, in that case, should be:

Syntax:#  -*-  coding: cp1252  -*-

One exception to the first line rule is when the source code starts with a UNIX “shebang” line. In this case, the encoding declaration should be added as the second line of the file. For example:

Syntax:#! /usr/bin/env python3

#  -*-  coding: encoding  -*

To install Python Interpreter:

The python interpreter is usually installed by default in /usr/local/bin/python3.8 when installing Python in the machine whereas, in Unix if the shell’s search path is set to /usr/local/bin it can be easily started by typing the python3.8 command.

Now, as the choice of directory is an installation option to where the interpreter exists, other places are also possible. You can check on it with your System Administrator. (e.g., /usr/local/python).

Add the path using this command:

Set path=%path%; C:\python

To start or invoke the Python Interpreter:

On windows, when you want to run the Python interpreter in the shell, you can simply type python and press Enter.

Now to get out of the interpreter by disassembling the Bytecode shell, you can type quit() or exit().

Similarly, you can save your python code as a script by writing it in a text editor:

and execute it using the interpreter:

You can also enter interactive mode by passing -i before the script.

A second way of starting the interpreter is “python-c command [arg] ...”, which executes the statement(s) in command, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety with single quotes.

Some Python modules are also useful as scripts. These can be invoked using the “python -m module [arg] ...”, which executes the source file for module as if you had spelled out its full name on the command line.

Argument Passing

When we pass a script name and its additional arguments to the shell while invoking the Python interpreter, it converts these into a list of strings. Then, it assigns them to the variable argv in the sys module. The command “import sys” will give us a list of this.

Without a script or argument, sys.argv[0] denotes an empty string. A script name of ‘-‘ means that it sets sys.argv[0] to ‘-‘, and with -c’, it set to ‘-c’. A value of ‘-m’ sets sys.argv[0] to the module’s full name. the command/module handles the options after ‘-c’ or ‘-m’.

Interactive Mode

When commands are read from a tty(teletype), the interpreter is said to be in an interactive mode.

In this mode, it prompts for the next command showing three greater-than signs (>>>) as shown in the above figure. And for continuation lines it prompts with the three dots () as shown in the figure below. Continuation lines are needed when entering a multi-line construct. Take a look at the figure below for the if statement example:

You can also use the Python interpreter as a calculator:

A to Z Full Forms and Acronyms

Related Article