Python Interactive and Script Mode programming

There are two python programming modes you can use to submit your python programs, Python Interactive and Script Mode Programming.

How to run Python Code?

An interpreter works in a same way for both the programming methods so ultimately, we get the same result. But there are few exceptions and limitations to use these methods.

If you still do not have python installed on your machine then what are you waiting for! Download Now!!

Python Programming modes:

1. Interactive Mode Python Programming

If you are using Command Prompt to write and execute python commands, then you must invoke the interpreter without any parameters.

All you need to do is – write “python” and python prompt will start automatically.

Microsoft Windows [Version 10.0.17763.195]

(c) 2018 Microsoft Corporation. Med enerett.

H:>python

Python 3.7.2rc1 (tags/v3.7.2rc1:75a402a217, Dec 11 2018, 23:05:39) [MSC v.1916 64 bit (AMD64)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

>>> 

Type following command in your python prompt and press enter to see the result.

The result will appear in next line automatically when you press ENTER.

Output,

This is a sample Python program

2. Script mode Python Programming

If you are using command prompt to execute python script, then you must invoke the interpreter with file name as parameter. You may write your python code in any editors – notepad, notepad++ but the file extension should be .py to execute the program by interpreter.

Let’s take same example and try to execute it in script mode. Your script name is blog.py and it has the same code,

print(“This is a sample Python Program”)

We assume that you have Python interpreter set in PATH variable and your file present in the same directory where you are currently at. Now, try to run this program as follows –

$ python blog.py

Your program must be present in current directory otherwise you will get this error – “python: can’t open file ‘blog.py’: [Errno 2] No such file or directory”

This script will produce same output,

This is a sample Python Program

A Longer Python Program

Now we’ll try to run program which has more than one-line code. When we have more than one line of code and try to execute in interpreter mode and see the result. 

Let’s try to print this star using interpreter mode.

print(”   *    “)

print(”  ***   “)

print(” *****  “)

print(”  ***   “)

print(”   *    “)

If we try to enter each line one at a time into the IDLE interactive shell, the program’s output will be intermingled with the statements you type.

For example,

>>> print("   *    ")

   *

>>> print("  ***   ")

  ***

>>> print(" *****  ")

 *****

>>> print("  ***   ")

  ***

>>> print("   *    ")

   *
>>>

 

In such scenario the best approach is to type all code into editor, save the program in file (star.py) and run the program. 

Most of the time we use an editor to enter and run our Python programs. The interactive interpreter is most useful for experimenting with small snippets of Python code

In this example if we run this program star.py in script mode then each print statement “draws” a horizontal slice of star.

$ python star.py
python print star pattern

In Python programming, it is more important that no whitespace (spaces or tabs) come before the beginning of each statement.

In Python the indentation of statements is significant and must be done properly. If we try to put a single space before a statement in the interactive shell, we get this ERROR.

python white space error unexpected indent

An interpreter reports a similar error when we attempt to run a saved Python program if the code contains such extraneous indentation.

For further read,

Leave a Comment