Python Interactive Shell

In this section of the Python tutorial, we will discuss about Python interactive shell which is a built-in tool provided by the Python.

This is a console (CLI) based Python tool which allows us to interact with Python interpreter. This console is known as terminal in Linux and Mac, command prompt in Windows. We can write code here and test its output instantly without giving further run commands. It works as an interpreter which read and executes code line by line and produce output instantly.

This is a quickest way to test our program logic or an expression without saving code into a file and storing into memory. It executes each instruction efficiently. We can write any valid python statement to get its output instantly.

Lets move on to practical side and start our Python shell to program over there. Before starting, first check the Python is installed in the computer by using the below command in the console. Since we are using Python3.8, so the command would be like below.


$ python

Python 3.6.8 (default, Dec 2 2019, 12:59:55)

[GCC 8.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.


If the Python is installed, it shows the available python version. Otherwise we can install it from the official site of Python Click to Download. For installation process of different Operating Systems, See the detailed description.

The primary prompt command for Python is three greater-than signs (>>>) which denote that present console is Python shell.


We can simply declare variable, print its value and type by simple statements. See the below example.

>>> a = 10
>>>a = "hello python"
‘hello python’
< class’str’>

We created a variable a with integer value then reassign it by string value and get its datatype as well. Here, we used Python’s built-in method type() which returns type of assigned variable. We will discuss it further in our tutorial.

Control Flow(if-else,loop)

Lets move on to the next and see how control flow can be created at terminal.

>>> a = 10
>>> b = 20
>>> if a < b:
...        print(b,"is greater")
...    else:
...        print(a, "is greater")
20 is greater

To test the conditional code, you can use if-else blocks too in the shell terminal and get instant output. We can also use loops to iterate elements of a sequence. Lets see how?

>>> for element in range(1,5):
...        print(element)


Creating function in shell console is very easy and same as to writing code into file. It helps developers to test function instantly and later on copy it into the file to create application.

>>> def add(a,b):
...      return a+b
>>> add(10,20)

Python Shell as a Calculator

Python shell console also works as a calculator i.e. we can apply operators directly into the values rather than variables and get instant output. Although we can use variables too but if we know values, can apply operators directly on them.

>>> 10+2 # Adding values
>>> 10-2 # Subtracting value
>>> 10*2 # Multiply values
>>> 10/2 # Dividing
>>> 10**2 # Getting square

Import Modules

We can import any module of Python and use its functions easily. Here, we are importing math module and using its functions.

>>> import math
>>> math.sqrt(100)
>>> math.sqrt(9)
>>> math.floor(5.55)
>>> math.log10(10)