Javaexercise.com

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.


Example

$ python
Output

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.

Variable

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

>>> a = 10
>>>a
10
>>>a = "hello python"
>>>a
‘hello python’
>>>type(a)
< 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)
...
1
2
3
4
>>>

Functions

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)
30
>>>

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
12
>>> 10-2 # Subtracting value
8
>>> 10*2 # Multiply values
20
>>> 10/2 # Dividing
5.0
>>> 10**2 # Getting square
100

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)
10.0
>>> math.sqrt(9)
3.0
>>> math.floor(5.55)
5
>>> math.log10(10)
1.0
>>>