Python

Introduction to Interactive Programming in Python

In this article, we will make an introduction to interactive programming in Python.

1. Introduction

Python is an easy-to-learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python website and may be freely distributed. The same site also contains distributions of and pointers to many free third-party Python modules, programs and tools, and additional documentation.

2. Python Interactive Programming

Python has two basic modes: normal and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command-line shell that gives immediate feedback for each statement while running previously fed statements inactive memory.

When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs >>> for continuation lines, it prompts with the secondary prompt, by default three dots (). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:

~$ python3
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns to the primary prompt; when input came from a file, it exits with a nonzero exit status after printing the stack trace:

>>> a = 'we'/2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'
>>>

Typing the interrupt character (usually Control-C or Delete) to the primary or secondary prompt cancels the input and returns to the primary prompt. Typing an interrupt while a command is executing raises the KeyboardInterrupt exception, which may be handled by a try statement.

When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named PYTHONSTARTUP to the name of a file containing your start-up commands. This is similar to the .profile feature of the Unix shells.

This file is only read in interactive sessions, not when Python reads commands from a script, and not when /dev/tty is given as the explicit source of commands (which otherwise behaves like an interactive session). It is executed in the same namespace where interactive commands are executed so that objects that it defines or imports can be used without qualification in the interactive session.

2.1 Examples

In this section, we will see some examples of using the interactive mode of python. The assumption is that you have got python installed on your machine. On macOS or Linux, open a terminal and simply type “python“. On Windows, bring up the command prompt and type “py“, or start an interactive Python session by selecting “Python (command line)“, “IDLE“, or similar program from the taskbar / app menu. IDLE is a GUI that includes both an interactive mode and options to edit and run files. To check which version of python is installed type python --version on your terminal:

~$ python --version
Python 3.9.9
~$

To start the interactive mode type python on the command line:

~$ python
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now let us see some working examples. Let’s start by writing the Hello World example:

>>> print('Hello World')
Hello World
>>>

Easy right. Of course – this is Python what else you were expecting. Now let us write a simple program to print the addition of two numbers:

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

Now let us see an example of continuation statement:

>>> for x in range (5):
...   print('Number',x)
...
Number 0
Number 1
Number 2
Number 3
Number 4
>>>

On the second line, you see that it starts with ... instead of >>>, this is because the interpreter is expecting more statements before it can interpret anything.

It’s easy to end the interactive session: You can either use exit() or Ctrl-D (i.e. EOF) to exit.

3. Summary

In this article, we learned about Python Interactive Mode. First, we discussed a little about Python programming language, then we discussed how to use the interactive mode of Python programming language. In the end, we looked at some of the examples of using the interactive mode. The interactive shell is also interactive in the way that it stands between the commands or actions and their execution. In other words, the shell waits for commands from the user, which it executes and returns the result of the execution. Afterward, the shell waits for the next input.

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button