Topic4 - Python Interpreter Modes
Topic4 - Python Interpreter Modes
In the Python programming language, there are two ways in which we can run our cod e
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
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
>>>
>>> 10
10
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.
>>> print("My name is " + name + ", aged " + str(age) + ", taking " + course)
>>> 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
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
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
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.