×

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

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Writing to a CSV file 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...

6 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute 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 round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Shallow Copy and Deep Copy in Python

Shallow Copy and Deep Copy in Python In this section, we will learn about the Shallow Copy and Deep Copy in the Python program. But before going through the topic, we...

6 minutes read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

What does base case mean in recursion

Base cases and recursive stages are used to define recursive functions. In a primary subject, we compute the outcome right away, given the function call's arguments. In a recursive step, we...

4 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Find key from value in dictionary python

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

5 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.