0% found this document useful (0 votes)
186 views

Topic4 - Python Interpreter Modes

The document discusses two modes for running Python code: interactive mode and script mode. Interactive mode allows running code line-by-line and is good for testing small pieces of code. Script mode involves writing code in a .py file and running the file, which is better for longer programs or application development.

Uploaded by

vsnpradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Topic4 - Python Interpreter Modes

The document discusses two modes for running Python code: interactive mode and script mode. Interactive mode allows running code line-by-line and is good for testing small pieces of code. Script mode involves writing code in a .py file and running the file, which is better for longer programs or application development.

Uploaded by

vsnpradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

TOPIC 4: Python Interpreter and Python Interpreter Modes

Python Interpreter is a program that reads and executes Python code.


Python has two basic modes of execution

In the Python programming language, there are two ways in which we can run our cod e

1. Interactive mode and

2. Script mode

Interactive Mode:

Interactive mode, also known as the REPL (Read, Evaluate, Print, Loop) or Python shell mode
provides us with a quick way of running blocks or a single line of Python code. Interactive
mode is very convenient for writing very short lines of code.

So the Interactive mode provides programmers a quick way to execute commands without
creating a python file (without .py).

The code executes via the Python shell, which comes with Python installation. Interactive
mode is handy when you just want to execute basic Python commands and if you want to
test a particular function and want to see how it works

In interactive mode whatever you type is immediately run. It will provide immediately with
the result or the feedback of the Python commands you have typed

In interactive mode, the Python waits for the user to enter the Python commands through
the keyboard

Once the user types in the Python command and presses the Enter (Return) key, the Python
interpreter goes ahead and executes the Python command and it will instantly provide you
with feedback or results of the command issued by the user

Once the interpreter provides you with the output result of the previous issued command, it
will once againwaits for the next command to be entered by the user and it loops the same
continuously until the user exits or quits from the interactive session mode

While the Python interpreter is in interactive mode it is commonly known as Python Shell.
REPL:
Read: The read function accepts an input from the user and stores it into the memory.
Eval: The eval function evaluates this ‘input’ read from the memory.

Print: The print function prints the outcome from the eval function.
Loop: The loop function creates a loop and terminates itself when the program ends.

Advantages:

1. It is good enough to learn, experiment or explore. It is good for beginners who want to
understand the basics of Python

2. Working in interactive mode is convenient for beginners and for testing small pieces of
code.
3. It is helpful when the script is small and when you want to see the results of your code
immediately
4. It is faster as you just need to type the python command and press enter key to view the
results

5. It is easy to check Python basic commands and some python built-in functions like how
the function works, what arguments are needed to pass and see the result of function

Drawback:

1. We cannot save the statements and have to retype all the statements once again to re-run
them.

2. It cannot remember the data over multiple interactive session modes. The data which the
user has entered in one session will be lost once you come out of the session
All the data will be lost or deleted once we exist from the interactive session. It will not able
to remember the previous session data in the next session.
So it is not suited for Application Development or Projects Development

3. It is very complex to enter and execute large pieces of code


4. It is hard and difficult to edit the code in interactive mode as you have to move backward
(by pressing up key) to the previous commands or else you need to type the entire code
once again
To access the Python shell, open the command prompt in the Windows operating system
and then type "python". Press the enter key and the Python shell will appear.

D:\Python310>python

Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>>

This mode provides with the command prompt >>> (chevron) to write and run the python
commands and code
The >>> indicates that the Python shell is ready to execute and send your commands to the
Python interpreter. The result is immediately displayed on the Python shell as soon as the
Python interpreter interprets the command.
To run your Python statements, just type them and hit the enter key. You will get the results
immediately, unlike in script mode. For example, to print the text "Hello World", we can type
the following:
>>> print("Hello World")

Hello World

>>>

Here are other examples:

>>> 10

10

>>> print(5 * 20)

100

>>> "hi" * 5

'hihihihihi'

We can also run multiple statements on the Python shell. A good example of this is when we
need to declare many variables and access them later.

>>> name = "Goutham"


>>> age = 26

>>> course = "Computer Science"

>>> print("My name is " + name + ", aged " + str(age) + ", taking " + course)

My name is Goutham, aged 26, taking Computer Science


Another example

>>> x=10

>>> y=20

>>> z=x+y

>>> print(z)
30

To exit or quit from the interactive mode, you can type quit() or exit() or Ctrl + Z (^Z)
2. Script Mode

It is also known as Batch mode.

This is the normal mode in which you work most of the times in Python
In the script mode, a python program can be written in a file. This file can then be saved and
executed using the command prompt. We can view the code at any time by opening the file
and editing becomes quite easy as we can open and view the entire code as many times as
we want. Script mode is very suitable for writing long pieces of code. It is much preferred
over interactive mode by experts in the program. The file made in the script made is by
default saved in the Python installation folder and the extension to save a python file is
“.py”.

If you need to write and execute long pieces of code or a lengthy program which spans
across multiple files, then the script mode is most recommended

How to run python code in script mode?

In order to run a code in script mode follow the following steps.


Step 1: Make a file using a text editor. You can use any text editor of your choice( We are
using notepad).

Step 2: After writing the code save the file using “.py” extension.
Step 3: Now open the command prompt and command directory to the one where your file
is stored.
Step 4: Type python “filename.py” and press enter.
Step 5: You will see the output on your command prompt.
Open Notepad and write the below code

Save the file with a filename with a “.py” extension


How to execute Python file in script mode

Open Command Prompt

Execute the file

1. Change the drive letter to D drive by typing d:


2. Change to the folder by typing cd jun2022
or you can also directly execute by typing python d:\jun2022\test.py

Advantages of Script mode:

1. Script mode of execution is very much suitable for us to write long Python programs
(programs having more than a single code line).
2. We can easily do the editing of our Python program inside the script mode.

3. We can open our Python program as many times as we want inside the script mode.

4. In script mode, we can even save the code we have written to use in the future.

5. Script mode is execution is mostly preferred by all the experts and Python programmers.

Protect pdf from copying with Online-PDF-No-Copy.com

You might also like