×

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 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.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

6 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python open() function

Python open() function The open() function in Python opens a file and returns a corresponding file object. If the file cannot be opened, an OSError is raised. Syntax open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameter file It represents the path and name of the file mode...

2 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

Write Dictionary to CSV in Python

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...

4 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Copying a file from one folder to Another with Python

In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer....

3 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.