0% found this document useful (0 votes)
11 views6 pages

UNIT-3 Python Interpreter: Interpreted Languages: Compiled Languages Interpreters

Python is an interpreted language that executes code line-by-line, developed by Guido van Rossum in 1991. It utilizes a Python Virtual Machine (PVM) to convert bytecode into machine language, allowing for various arithmetic and trigonometric calculations. The document also discusses the differences between compilers and interpreters, the advantages and disadvantages of using interpreters, and the functionality of the Python Shell for executing commands.

Uploaded by

opbabul2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

UNIT-3 Python Interpreter: Interpreted Languages: Compiled Languages Interpreters

Python is an interpreted language that executes code line-by-line, developed by Guido van Rossum in 1991. It utilizes a Python Virtual Machine (PVM) to convert bytecode into machine language, allowing for various arithmetic and trigonometric calculations. The document also discusses the differences between compilers and interpreters, the advantages and disadvantages of using interpreters, and the functionality of the Python Shell for executing commands.

Uploaded by

opbabul2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT-3

Python Interpreter
Python is an interpreted language developed by Guido van Rossum in the year of 1991.
Python is one of the most high-level languages used today because of its massive versatility
and portable library & framework features. It is an interpreted language because it executes
line-by-line instructions. There are actually two ways to execute python code one is in
Interactive mode and another thing is having Python prompts which is also called script
mode. Python does not convert high level code into low level code as many other
programming languages do rather it will scan the entire code into something called
bytecode. every time when Python developer runs the code and starts to execute the
compilation part execute first and then it generates a byte code which get converted by PVM
Python Virtual machine that understand the analogy and give the desired output.

Interpreted Languages: Perl, BASIC, Python, JavaScript, Ruby, PHP.


Compiled Languages: C, C++, C#, COBOL and CLEO.

Interpreters

Interpreters are the computer program that will convert the source code or an high level
language into intermediate code (machine level language). It is also called translator in
programming terminology. Interpreters execute each line of statements slowly. This process
is called Interpretation.

For example, Python is an interpreted language, PHP, Ruby, and JavaScript.

[ Working of Interpreter ]

How interpreters function in Python?

Python interpreter is written in C programming language as we all know that C is considered


as mother of all programming languages, Python interpreter called "CPython".

Initially in source code analysis, Python get source and check for some Indentation rule and
check for errors. if there are any errors Python stops its execution and make to modify to
prevent errors. This process is called Lexical analysis in python which states that dividing the
source code into list of individual tokens.

In Byte code generation, once the python interpreter receives the tokens, it generates AST
(Abstract Structure tree) which converts to byte code and then byte code can be saved in
(.py) extension.

At last, by using Python Virtual Machine, interpreter loads the machine language and PVM
converts in into 0s and 1 s which prints the results.

Difference between Compilers and Interpreters

Compiler Interpreter

It translates a program on a single run It executes a program line by line.

It is an Fast Process It is an Slow Process

It generates output in the form of .(exe) It does not generate any form of outputs.

It utilizes CPU more. It takes less utilization of CPU

It is maximum used in Programming and


It is used in Production environment
development environment.

Python, Ruby, PHP are the interpreted


C, C++, C# are the compiled languages
languages.

It takes lot of time to analyze the code


It takes less time compared to compilers.
structure

Advantages of using Interpreter

• It is flexible and error localization is easier.

• It is smaller in size.

• It executes line by line execution.


Disadvantages of using Interpreters

• It takes lot of time to translate.

• It is slower.

• It uses lot of storage.

USING PYTHON AS A CALCULATOR


Python can be used as a calculator to compute arithmetic operations like addition,
subtraction, multiplication and division. Python can also be used for trigonometric
calculations and statistical calculations.

Simple arithmetic calculations can be completed at the Python Prompt, also called the Python
REPL. REPL stands for Read Evaluate Print Loop. The Python REPL shows three arrow
symbols >>> followed by a blinking cursor. Programmers type commands at the >>> prompt
then hit to see the results. Commands typed into the Python REPL are read by the interpreter,
results of running the commands are evaluated, then printed to the command window. After
the output is printed, the >>> prompt appears on a new line. This process repeats over and
over again in a continuous loop.

Operator Description Example Result

+ addition 2+3 5

- subtraction 8-6 2

- negative number -4 -4

* multiplication 5*2 10

/ division 6/3 2
** raises a number to a power 10**2 100

_ returns last saved value _+7 107

Trigonometry: sine, cosine, and tangent

Trigonometry functions such as sine, cosine, and tangent can also be calculated using the
Python REPL.

To use Python's trig functions, we need to introduce a new concept: importing modules.

In Python, there are many operations built into the language when the REPL starts. These
include + , -, *, / .

Example-

>>> sin(60)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'sin' is not defined

This error results because we have not told Python to include the sin function.
The sin function is part of the Python Standard Library. The Python Standard Library comes
with every Python installation and includes many functions, but not all of these functions are
available to us when we start a new Python REPL session. To use Python's sin function, first
import the sin function from the math module which is part of the Python Standard Library.

Importing modules and functions is easy. Use the following syntax:

from module import function

To import the sin() function from the math module try:

>>> from math import sin >>> sin(60) -0.3048106211022167

Example

import math

print(math.sqrt(25)) # Square root

print(math.pow(2, 5)) # Power

print(math.factorial(5)) # Factorial
print(math.sin(math.pi/2)) # Trigonometry

Example

num1 = float(input("Enter first number: "))

op = input("Enter operator (+, -, *, /, %, **): ")

num2 = float(input("Enter second number: "))

if op == "+":

print("Result:", num1 + num2)

elif op == "-":

print("Result:", num1 - num2)

elif op == "*":

print("Result:", num1 * num2)

elif op == "/":

if num2 != 0:

print("Result:", num1 / num2)

else:

print("Error: Cannot divide by zero.")

elif op == "%":

if num2 != 0:

print("Result:", num1 % num2)

else:

print("Error: Cannot take modulus by zero.")

elif op == "**":

print("Result:", num1 ** num2)

else:

print("Invalid operator.")
PYTHON SHELL
Python is an interpreter language. It means it executes the code line by line. Python provides
a Python Shell (also known as Python Interactive Shell) which is used to execute a single
Python command and get the result. Python Shell waits for the input command from the
user. As soon as the user enters the command, it executes it and displays the result.

A shell, also called a console or terminal, is a program that allows direct interaction with an
interpreter. The interpreter usually runs an entire program all at once. But the interpreter
can run one line of code at a time within a Python shell.

The acronym REPL is often used when referring to a shell. REPL stands for "read-eval-print
loop," which describes the repetitive nature of a shell:

1. Read/input some code

2. Evaluate/run the code

3. Print any results

4. Loop back to step 1

Example

C:\Users\Suchandra Datta>python

Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on
win32

Type "help", "copyright", "credits" or "license" for more information.

>>>print("hello world!")

You might also like