Two Modes of Python Program

Table of Contents

Two Modes of Python Program

Python Program can be executed in two different modes:

  • Interactive mode programming
  • Script mode programming

Interactive Mode Programming

It is a command line shell which gives immediate output for each statement, while keeping previously fed statements in active memory. This mode is used when a user wishes to run one single line or small block of code. It runs very quickly and gives instant output. A sample code is executed using interactive mode as below.

Interactive mode can also be opened using the following ways:

i) From command prompt c :> users\\…>python

The symbol “>>>” in the above screen indicates that the Python environment is in interactive mode.

ii) From the start menu select Python (As shown below)

Python interpreter in interactive mode is commonly known as Python Shell. >>> is the prompt for Python shell. It shows that shell is ready to accept your commands. Python shell allows you to type Python code and see the result immediately. It is also known as REPL which stands Read-Eval-Print-Loop. REPL allows you to quickly test code snippets and see the output immediately. To quit the Python shell in Python Command line, hit Ctrl+Z followed by the Enter Key. To quit the Python shell in Idle, press Ctrl+Q.

 

Script Mode Programming

When the programmer wishes to use more than one line of code or a block of code, script mode is preferred. The Script mode works the following way:

  1. Open the Script mode
  2. Type the complete program. Comment, edit if required.
  3. Save the program with a valid name.
  4. Run
  5. Correct errors, if any, Save and Run until proper output

The above steps are described in detail below:

i) To open script mode, select the menu “IDLE (Python 3.9 64-bit)” from start menu

ii) After clicking on the menu “IDLE (Python 3.9 64-bit)” , a new window with the text Python 3.9 Shell will be opened as shown below:

iii) Select File -> New, to open editor. Type the complete program.

iv) Select File again; Choose Save. This will automatically save the file with an extension “.py”.

v) Select Run -> Run Module or Short Cut Key F5 ( As shown in the screen below)

 

The output of the program will be displayed as below:

Script mode is used to create, modify and execute Python programs. Script mode is used for executing a set of statements at any time and any number of times. The set of statements can be saved in a file with extension .py which can be executed at later time. The Python Interpreter in Script mode is used to execute the python code from a file.

Add Your Heading Text Here

You May Like to Browers More