How To Work with the Python Interactive Console?


In this tutorial we will discuss how to work with th Python interactive console and control it as programming tool

Entering the Interactive Console

The Python interactive console can be easily obtained from any local PC or web server with Python installed.

The command you mostly will wish to utilize to go into the Python interactive console for your default form of Python is:

  • $ python


On the off chance that you have set up a programming scenario, you can dispatch the scenario and access the form of Python and modules you have installed in that condition by first going into that condition:

  • $ cd environments

  • $ . my_env/bin/activate


Then typing the python command:

  • Python

(my_env) admin@ubuntu:~/environments$ python


For this situation, the default adaptation of Python will be Python 3.6.4, which is shown in the output once we type the command, alongside the important copyright notice and a few commands you can type for additional data:

Result

Python 3.6.4 (default, Apr 25 2018, 17:55:23)
[GCC 15.5.0 20180806] on linux
Enter help, copyright, credits or license for more details.

>>>.


The essential prompt for the following command is three greater-than symbols (>>>):

>>>

You can easily target particular forms of Python by affixing the version number to your command, without any empty spaces:

$ python 3.6.4

Result

Python 3.6.4 (default, Apr 25 2018, 18:55:23)
[GCC 5.5.0 20180806] on linux2
Enter help, copyright, credits or license for more details.
>>>

Here, we got the output that Python 3.6.4 will be utilized. On the off chance that this is our default form of Python 3, we could likewise have gone into this intuitive console with the command python3.

Working with the Python Interactive Console

The Python interactive translator acknowledges Python syntax structure, which you put following the >>> prefix.

To give an instance, set values to factors

  • birth_year = 1998

 

When we have allocated out the number value of 1998 to the variable birth_year, we will squeeze return and get another line with the three more greater-than signs as a prefix:

>>>birth_year = 1988

>>>

 

We can keep on assigning variables and after that perform math with administrators to get estimations returned:

>>> birth_year = 1988
>>> death_year = 2018
>>> age_at_death = death_year - birth_year
>>> print(age_at_death)
30
>>>

Since we would with a script in a record

As we would with a content in a record, we allocated variables, subtracted one variable from the another, and requested that the console print the variable that speaks to the distinction.

Much the same as in any type of Python, you can likewise utilize the interactive console as a calculator:

>>> 111/ 15
7.4
>>>

In the above calculation, we divided the number 111 by 15 and delivered the result of 7.4.

Several Lines

When we are composing Python code the will cover several lines, the interpreter will utilize the secondary provoke for continuation lines, 3 dots (...).

To burst out of these continuation lines, you should press ENTER twice.

We can observe what this looks like in the accompanying code that relegates two factors and after that uses a contingent statement to figure out what to print out to the console:

>>> admin= admin
>>> Bee= Bee
>>> if len(admin) > len(Bee):
...     print(admin codes in Java.')
... else:
...     print(admin codes in Python.')
...
Admin codes in Python.
>>>

For this situation the lengths of the two strings are equivalent, so the else explanation prints.

Note that you should keep Python indenting tradition of four whitespaces, else you will get a blunder:

>>> if len(admin) > len(Bee):
... print(admin codes in Java.')
 File "<stdin>", line 2
   print(admin codes in Java.')
       ^
IndentationError: expected an indented block
>>>

You can not just explore different avenues regarding code over various lines in the Python console, you can likewise import modules.

Importing Modules

The interpreter  of Python gives a fast method to you to verify whether modules are accessible in a particular programming condition. You can do this by utilizing the import explanation:

>>> import matplotlib
Traceback (most recent call last):
 File "<stdin>", line 2, in <module>
ImportError: No module named 'matplotlib'

For the situation above, the module matplotlib was not accessible inside the present programming condition.

Keeping in mind the end goal to install it, we'll have to leave the intelligent interpreter and intinstall roduce with pip obviously:

(my_env) admin@ubuntu:~/environments$ pip install matplotlib

Result

Collecting matplotlib
 Downloading matplotlib-2.0.2-cp35-cp35m-manylinux1_x86_64.whl (14.6MB)
...
Installing dfowloaded packages- pyparsing, cycler, python-dateutil, numpy, pytz, matplotlib
Successfully done installation of cycler-0.10.0 matplotlib-2.0.2 numpy-1.13.0 pyparsing-2.2.0 python-dateutil-2.6.0 pytz-2017.2

Once the matplotlib module alongside its conditions are effectively installed, you can backpedal into the interactive interpreter:

(my_env) admin@ubuntu:~/environments$ python

 

>>> import matplotlib

>>>

 

Now you will get no error message and can utilize the installed module either inside the shell or inside a document.

Way to Leave the Python Interactive Console


There are two fundamental approaches to leave the Python interactive console, either using keyboard shortcut or a Python function.

The keyboard easy way CTRL + D in *nix-based frameworks or CTRL + Z then the CTRL button in Windows frameworks will intrude on your console and return you to your unique terminal condition:

...
>>> age_at_death = death_year - birth_year
>>> print(age_at_death)
30
>>>
admin@ubuntu:~/environments$

Then again, the Python function quit() will stop out of the interactive console and furthermore take you back to the first terminal condition that you were beforehand in:


>>> octopus = 'Ollie'
>>> quit()
admin@PythonUbuntu:~/environments$

When you utilize the function quit(), it will appear in your history document, however the keyboard shortcut CTRL + D will not be recorded:

File: /home/admin/.python_history

...
age_at_death = death_year - birth_year
print(age_at_death)
octopus = 'Ollie'
quit()

Stopping the Python interpreter should be possible in any case, contingent upon what go off well for your work process and your history needs.

Retrieving History

A helpful aspect concerning the Python interactive console is that the greater part of your commands are logged to the .python_history record in *nix-based frameworks, which you can take a look at in a word editor like nano, for example:

$ nano ~/.python_history

Once it is opened with an easy to use text editor tool, your Python history record will look something similar to this, with your individual Python command history:

File: /home/Admin/.python_history

import pygame
quit()
if 10 > 5:
   print("Hi, All“
else:
   print("nope")
admin= admin
bee= bee

When you are finished with your file, you can press CTRL + X from your keyboard to leave nano.

By keeping the majority of your Python history, you can backpedal to past commands and examinations, and copy or paste that code for utilize in Python programming documents.