Programming in Immediate/interactive mode

In Python, there are two options/methods for running code:

  • Immediate / interactive mode
  • Script mode

Here, we will see
* The difference between the interactive modes and script mode and
* The pros and cons of running scripts in both of these modes.

Immediate / Interactive Mode

Interactive mode, provides us with a quick way of running blocks or a single line of Python code. The code executes via the Python shell, which comes with Python installation.
Interactive mode is handy when we want to execute basic Python commands or when we are new to Python programming and we just want to get familiar with this wonderful language.

To access the Python shell, we have to open the terminal of your operating system and then type “python”. On Pressing the enter key and the Python shell will appear.

“>>>” is the prompt of python language. It 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 our Python statements, we have to just type them and hit the enter key. and we will get the results immediately.
Example: To print the text “Welcome to Python programming”, we can type the following:

>>> print(“Welcome to Python programming”)
Welcome to Python programming

Here are other examples:

>>> 100
>>> print(2*10)
>>> print(“hello”*5)

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.

>>> a=10
>>> b=20
>>> c=30
>>> print(“a”,a,”b”,b,”c”,c)
a 10 b 20 c 30

>>> name=”Amit”
>>> salary=45000
>>> print(“name “,name,” salary “,salary)
name Amit salary 45000

More examples:

In immediate mode as we type the command and press enter we get the result.

Example: To display a welcome message in immediate mode

>>> print(“welcome to python”)
Welcome to python

Example: To display the value of a variable in immediate mode


Writing the above statement will create a variable named “a” and assign value “10” to it. (in python we are not required to declare a variable first and then use it as in some other languages like c, c++, java etc).

Now to display the value of variable “a” we can write as:


>>> print(a)

>>>print(“a = “,a)

Example: to calculate and print sum of two numbers in immediate mode
1st method

>>> a=10
>>> b=20
>>> c=a+b
>>> print(a)
>>> print(b)
>>> print(c)

Example: to calculate and print sum of two numbers in immediate mode
2nd method

>>> a=10
>>> b=20
>>> c=a+b
>>> print("1st no = ",a)
1st no =  10
>>> print("2nd no = ",b)
2nd no =  20
>>> print("sum = ",c)
sum =  30

Example: to calculate and print square of a number in immediate mode

>>> a=5
>>> b=a*a
>>> print("no = ",a)
no =  5
>>> print("square = ",b)
square =  25

Example: to calculate and print cube of a number in immediate mode

>>> a=5
>>> b=a*a*a
>>> print("no = ",a)
no =  5
>>> print("cube = ",b)
square =  125

Getting Help in python

You can also get help with regards to a particular command in interactive mode. Just type the help() command on the shell and then hit the enter key. You will see the following:

>>> help()

>>> help()
Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at

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 contains a given string such as "spam", type "modules spam".


Now to find the help for a particular command, a simple type that command, for instance, to find help for the print command, simply type print and hit the enter key. The result will look like this:

help> print
Help on built-in function print in module builtins:

    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.


To exit help, type q for “quit” and then hit the enter key. You will be taken back to the Python shell.

Pros and Cons of Interactive Mode

The following are the advantages of running your code in an interactive mode:

* Helpful when your script is extremely short and you want immediate results.
* Faster as you only have to type a command and then press the enter key to get the results.
* Good for beginners who need to understand Python basics.

The following are the disadvantages of running your code in the interactive mode:

* Editing the code in interactive mode is hard as you have to move back to the previous commands or else you have to rewrite the whole command again.
* It’s very tedious to run long pieces of code.

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling