Python Interpreter

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

Do you know the place where you write the Python code and run it? It is an interpreter!

In this article, we will learn about the Python interpreter, how it works. And also get introduced to its environment by coding. So first, let us start by discussing what an interpreter is.

What is an Interpreter?

An Interpreter is a program that converts the code a developer writes into an intermediate language, called the byte code. It converts the code line by line, one at a time. It translates till the end and stops at that line where an error occurs, if any, making the debugging process easy.

Python and Java are two examples of the interpreted programming languages.

What happens when you run a Python Code?

On running a Python code, say a simple program that prints “Hello, World!”, then the following steps happen:

1. The program gets converted into an intermediate code, called bytecode. This is also binary but is not understood by the processor.

2. Python Interpreter, stored in the memory as a collection of instructions in binary form, does the above conversion. These instructions can be thought of as a combination of a compiler and a Python Virtual Machine (PVM).

3. The compiler converts the source code, the “Hello, World!” program, into the byte code, which is platform-independent and understood by the PVM.

4. PVM reads the byte code and executes it on the hardware, the job of a processor.

This is how a Python interpreter works. The following chart summarizes the above process.

Working of Python Interpreter

Features of Python Interpreter

The Python Interpreter is user friendly and its features include:

  • Interactive editing
  • Use of variables initialized in the previous prompts
  • Writing the complete code in it with a readline facility.
  • To get command-line editing, one can press Ctrl+P, which gives a beep indicating the mode is activated.

Getting started with the Python Interpreter

To open the Python interpreter, installed in the system, search in the Start menu. Then click on Python 3.9 or other, depending on the installed version.

  • In Windows, it looks like Command Prompt.
  • And on Mac, it looks like a terminal.

The interpreter environment works using REPL:

  • Read the lines of the code
  • Evaluate and execute the code
  • Print the output of the code (if any) to the console
  • Looping to repeat the above process

The picture below shows the Python interpreter once opened. Small description of the Python version, storage, etc. Along with this, you can see three arrows (>>>), called prompt. This is the place where you have to write the code.

Python Interpreter

Coding in the Python Interpreter Environment

The interpreter allows interactive editing. Once you write the code, press the “Enter” button to see the results. The below example shows a simple code of printing “Hello, World!”

“Hello, World!” program in Interpreter:

print("Hello, World!")

Output

Hello, World!

After the result, the interpreter gives another prompt for the next program/code. You can see this in the below picture.

REPL

To write multiple line codes, the interpreter allows for the continuation. For example, see the below if-else statement.

Example of multiline if-else code in Python interpreter:

n=4
if(n>0):
...     print("Number is positive")
else:
...     print("Number is negative")

Output:

Number is positive

You can also use Python IDLE, which downloads along with the Python interpreter to write and run the programs. To open the IDLE, you can search in the “Start” menu.

Start Python Interpreter

You can also use it as a calculator!

Examples of using interpreter as calculator:

3+4

Output:

7

Examples of using interpreter as calculator:

144/24

Output:

6.0

Finally, to exit from the shell, you can

  • type quit() and press enter
  • Press Ctrl-Z plus Return in Windows
  • In Linux or macOS, type Ctrl+D.

Passing Arguments in Shell in Python

Python interpreter also allows the programmer to pass the arguments as a list of Strings. This property is inherited from the C language.

To implement this, sys has to be imported first:

Importing library for command-line arguments:

import sys

In this, the variable argv holds all the parameters passed to the interpreter. If the size of argv is 1 and if argv[0] is empty, then it means no arguments are passed.

On passing arguments, these get stored as list elements in argv, starting from the zero index. The first element will be the python file name, followed by the arguments.

Python Interview Questions on Interpreter

Q1. Does a Python interpreter allow you to write multiline code? If yes, write one.

Ans 1. Yes. Python interpreters do allow writing multiline codes like if-else blocks, for loops, etc. An example of a multiline code written in interpreter is given below.

Example of multiline code in Python Interpreter:

age=15
if(age<18):
... print("You are not eligible to vote")
else:
... print("You are eligible to vote")
...

Output:

You are not eligible to vote

Q2. Write a program to count the number of arguments passed in the command line.

Ans 2. The code to count the number of arguments:

import sys
no_of_args=len(sys.argv)
print("Number of arguments passed:",no_of_args)

On saving this file with the .py extension, say len.py. Then running it in the command prompt/ shell by passing the arguments will give the count.

len.py "Python" "PythonGeeks"

Output:

Number of arguments passed: 3

Q3. What is the first element of the command line arguments? Show it using the code.

Ans. The first element of the command line arguments is the python filename or its directory path. Since the elements are stored in argv, we can print the first element using the zero index.

Print the first argument:

import sys
first_arg=sys.argv[0]
print("The first argument is:",first_arg)

On saving this file with the .py extension, say firstArg.py. Then running it in the command prompt/ shell by passing the arguments will give the count.

firstArg.py "Python" "PythonGeeks"

Output:

The first argument is:firstArg.py

Q4. Print all the elements other than the file name in the command line.

Ans. The argv that stores all the arguments, has the first element as the filename. We can print all the elements other than the first element, that is skipping the zero index.

Print all arguments other than the filename:

import sys
n=len(sys.argv)
print("The arguments other than filename are:")
for i in range(1,n):
    print(sys.argv[i],end=" ")

Writing this code in a document and saving this file with the .py extension, say Args.py. On running it in the command prompt/ shell by passing the arguments will the list of the arguments.

Args.py "Python" "PythonGeeks" 1 2 3

Output:

The arguments other than filename are:
Python PythonGeeks 1 2 3

Q5. Write a program to use an interpreter as a calculator to do addition, subtraction, multiplication and division on the numbers 19,4.

Ans 5. Example of using interpreter as calculator:

19+4

19-4

19*4

19/4

Output:

23

15

76

4.75

Quiz on Python Interpreter

Conclusion

Hence, we learned that Python is an Interpreter language and how the code is executed internally. We also got our hands dirty with some coding in an interpreter and got familiar with its environment.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google | Facebook


1 Response

  1. John Hofman says:

    how do I get this python intertpretor on my laptop 64 bit windows 8

Leave a Reply

Your email address will not be published. Required fields are marked *