×

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the most basic information we should know before learning any programming language. Let’s start with the most basic question- What is an interpreter?

Interpreter

We have often heard or read that python is an interpreted language. Now, what does this mean? Python is one program that runs another program. In simple words, python runs its code line by line. Since we all should be aware that computer cannot understand our language, it only understands the machine language, which is the binary language. So whenever we write any code in python, the interpreter changes it to the language which the computer will understand. Whenever we run a python script, the interpreter does the same.

So, we can write the python code in any text editor and then save it with an extension of “.py” in our system. To run this code, we must have some program preinstalled in our system. This program can be “python” or “python3”. Now, these programs will run our code. These programs are called interpreters.

Python also provides a shell to run a python code. A python interpreter first reads the command, executes it, and then again loops back to read the next command. Because of this order, python is known as REPL (Read, Evaluate, Print, Loop).

Ways an interpreter works

There are three ways in which the interpreter works, which are:

  • It can execute the source code directly into the target code.
  • It can convert the source code into an intermediate code, and then the intermediate code will get converted into the target code.
  • It can use an internal compiler to convert the code into precompiled code and then executes this precompiled code.

Types of interpreters

  1. Bytecode Interpreter: In this type of interpreter, the source code is converted into byte code. Now, a byte code is an optimized version of a code, but it is not the target code.
  2. Threaded Code Interpreters: It is similar to a bytecode interpreter, but the only difference is that it uses a pointer. In this, each word acts as a pointer.
  3. Abstract Syntax Tree Interpreters: In this type of interpreter, the source code is converted into an abstract syntax tree. Then, the program is executed based on the tree sequence.
  4. Self-Interpreters: It is a special type of interpreter, which converts only those programs which are written in the same language as the interpreter.

Advantages of an interpreter

  • It executes the code line-by-line. Because of that, debugging the code is very easy. It will instantly tell us the line which is causing the error.
  • It is very memory efficient.

Disadvantages of an interpreter

  • Execution time is higher than a compiler.

How can we open Power Shell?

  • First, we will open the power shell or command prompt on windows, and the terminal window on mac.
  • After opening the terminal, we will write ‘python’ and press enter.
  • After that, we will see a python prompt consisting of three greater than symbols i.e., “>>>”.
  • Now, we can write our python code in it.

Example:

>>> 'Welcome' + '!'
'Welcome!'
>>> [0, 2] + [1, 3]
[0, 2, 1, 3]
>>> 5 + 2
7
>>> max(1, 5, -2)
5
>>> 1 + 2 * 3
7
>>>

Indentation in Interpreter

When we write a single-line code, then we can simply enter to execute the code, but when we are writing a multi-line code, then it is important to use indentation in the code., When the code ends with “:”, we can understand the code is multiline. We use two spaces we can use any number of spaces.

Example

>>> for i in range(11):
      x = i * 10
      print(i, x)

Output

0 0
1 10
2 20
3 30
4 40
5 50
6 60
7 70
8 80
9 90
10 100

If we don’t want to use a local interpreter, we simply use online interpreters. There are a lot of online interpreters available on the web. We can go and search on the web and find these interpreters. They can be time efficient, but one of the drawbacks of using an online interpreter is that we cannot save our files on the local device. The code is available till the compiler is open. After that, we won’t be able to recheck our code.


Related Topics

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python String isalpha() method

Python String isalpha() method The string.isalpha () method in Python returns a boolean value true if all characters in the  string are alphabetic else for any other value it returns false. Syntax isalpha()  Parameter NA Return This...

1 minute read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.