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?
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
- 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.
- 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.
- 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.
- 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.
>>> '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.
>>> for i in range(11): x = i * 10 print(i, x)
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.