Interactive Mode and Script Mode in Python

Here we are going to learn about “INTERACTIVE MODE” and “SCRIPT MODE” in Python.

Let’s dig into the topics one by one to have a clear understanding and later have a fair comparison amongst them on every basis possible.

Interactive Mode

The interactive mode of Python is also called REPL.

REPL stands for ‘Read-Eval-Print-Loop’. It is a simple, interactive command-line shell that provides us with the result when provided with a single line Python commands.

  1. Read: The read function accepts an input from the user and stores it into the memory.
  2. Eval: The eval function evaluates this ‘input’ read from the memory.
  3. Print: The print function prints the outcome from the eval function.
  4. Loop: The loop function creates a loop and terminates itself when the program ends.

This was a brief explanation of the interactive mode: REPL.

To keep it simple and concise for a better understanding, the interactive mode is a command-line shell that gives immediate feedback for each statement, while running previously fed statements inside the active memory. So once new lines are fed into the interpreter, the fed program is evaluated each partly and as a full program. Interactive mode is beneficial after you simply wish to execute basic Python commands or just wish to undertake numerous syntaxes and easy functionalities of python.

Thus, open a command prompt in WINDOWS to call a python shell to run the interactive mode. On Linux or macOS open up the terminal and thereafter type “Python” and press enter.

This results in the opening of the Python shell in interactive mode.

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> indicates that the Python shell is ready for execution in the interactive mode.

We can perform a number of simple tasks or run a few simple programs or test some Python functions, using the interactive mode.

Let us see a few examples on the interpreter below:

The print() function:

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("This statement is printed by the print() function in the interpreted mode")

OUTPUT:

This statement is printed by the print() function in the interpreted mode

Performing mathematical operations:

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10*10
100
>>>
>>> 10+10
20
>>>
>>> 100-30
70

Storing values into variables and later performing arithmetic or string operations:

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a="repeat_me|"
>>> b=5
>>> a*b

OUTPUT:

'repeat_me|repeat_me|repeat_me|repeat_me|repeat_me|'

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=" was stored in first variable 'a'"
>>> b=" hey there i was stored in variable 'b'"
>>> print("I" +a+ "and" +b)

 

OUTPUT:

I was stored in first variable 'a'and hey there i was stored in variable 'b'

Acquiring help from the interactive shell:

Another important trick of the trade is getting help from the Python interpreter. simply input ‘help()‘ in the interactive shell and hit the enter key.

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help()

Welcome to Python 3.8's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.8/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

You can enter any command/keyword/module name to get help regarding it. Eg: print, str, etc.

Exit the help() by giving ‘q’ as input and pressing the enter key.

Running python scripts in an interactive mode in Python:

You need to be very careful while using the interactive mode. Since the code you may feel is right in appearance can be slightly incorrect in formatting for the interpreter.

EXAMPLE: 

if 1:
  print("Good MORNING")
print("SEE YA! have a NICE day")

The above is a valid Python Script. But suppose you enter the script into the interactive shell it will result in the following.

IndentationError: expected an indented block
>>> if 1:
...     print("Good MORNING")
... print("SEE YA LATER! have a NICE day)
  File "<stdin>", line 3
    print("SEE YA LATER! have a NICE day")
    ^
SyntaxError: invalid syntax

Since the interpreter expects a line-gap between the two print statements to indicate the end of the  ‘if’ condition. This resulted in the error due to the missing line the compiler misinterpreted it as an indentation error.

It must be written as follows:

if 1:
    print("Good MORNING")

print("SEE YA! have a NICE day")

It will consequently result in the following output:

C:\Users\Tirthesh Pawar>py
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> if 1:
... print("True")
...
True
>>> print("Done")
Done
>>> if 1:
... print("Good MORNING")
...
Good MORNING
>>> print("SEE YA! have a NICE day")
SEE YA! have a NICE day

You can play with your code, test syntax, write some test code here.

Let us move on to our next topic which is the ‘SCRIPT MODE IN PYTHON‘.

Script Mode

To write long Python codes or programs having multiple files, the interactive mode is not what I would recommend using on the first hand. Script mode would be a better choice in such scenarios.

Using Script mode is quite easy, you have to write your code in a text file and save the particular file with a .py’ extension. .py stands for PYTHON. You can use any available text editor.

In a standard Python shell, you can simply click “FILE” then choose “NEW” or press “ctrl+N” to open a blank script where you can input your code. Saving the code is quite is by pressing “Ctrl+S”.  Similarly running a script in it is as easy as pressing the key “F5”.

The syntax of the programs remains the same. “IDLE”  is the popular GUI based Python shell to write programs and perform all the functions mentioned above.

We can execute programs or scripts written in a GUI directly in a Python shell by traversing to the exact directory (location) of the saved file and write the command “python file_name.py” to get the output.

Key differences between Interactive and Script Mode:

  1. Interactive mode is used for running a single line or a single block of code.
  2. Whereas, Script mode is used to work with lengthy codes or multiples blocks of code.
  3. Interactive mode runs very quickly and gives the output instantly. On the other hand, Script mode takes more time to compile and run.
  4. Editing the written code is a tedious task in interactive mode and one has to be very careful and accurate while writing the code, whereas script mode gives flexible editing options.
  5. One can use IDLE or other text editors, Python shells for simplicity in coding and editing and later execute those programs on a Python shell.

 

Conclusion

So Pycoders, as I conclude this post I would like to share my insight that depending on the use case appropriately select between the two modes. The interactive mode is suitable for executing simple one-liner codes or single block programs. Whereas, use script mode to write long programs for easier editing and working with the program. I hope this explanation would be helpful in your better understanding of which approach to take while coding. Try hands-on methods demonstrated above for a clear understanding and keep coding my pycoders and have a bug free day!!!

Also read:

Basic in-built functions in Python

Leave a Reply

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