2. The Interpreter, an Interactive Shell
By Bernd Klein. Last modified: 01 Feb 2022.
The Terms 'Interactive' and 'Shell'
The term "interactive" traces back to the Latin expression "inter agere". The verb "agere" means amongst other things "to do something" and "to act", while "inter" denotes the spatial and temporal position to things and events, i.e. "between" or "among" objects, persons, and events. So "inter agere" means "to act between" or "to act among" these.
With this in mind, we can say that the interactive shell is between the user and the operating system (e.g. Linux, Unix, Windows or others). Instead of an operating system an interpreter can be used for a programming language like Python as well. The Python interpreter can be used from an interactive shell.
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. Afterwards, the shell waits for the next input.
A shell in biology is a calcium carbonate "wall" which protects snails or mussels from its environment or its enemies. Similarly, a shell in operating systems lies between the kernel of the operating system and the user. It's a "protection" in both directions. The user doesn't have to use the complicated basic functions of the OS but is capable of using the interactive shell which is relatively simple and easy to understand. The kernel is protected from unintentional and incorrect usages of system functions.
Python offers a comfortable command line interface with the Python Shell, which is also known as the "Python Interactive Shell". It looks like the term "Interactive Shell" is a tautology, because "shell" is interactive on its own, at least the kind of shells we have described in the previous paragraphs.
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Using the Python Interactive Shell
With the Python interactive interpreter it is easy to check Python commands. The Python interpreter can be invoked by typing the command "python" without any parameter followed by the "return" key at the shell prompt:
python
Python comes back with the following information:
$ python
Python 2.7.11 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
A closer look at the output above reveals that we have the wrong Python version. We wanted to use Python 3.x, but what we got is the installed standard of the operating system, i.e. version 2.7.11+.
The easiest way to check if a Python 3.x version is installed: Open a Terminal. Type in python but no return. Instead, type the "Tab" key. You will see possible extensions and other installed versions, if there are some:
bernd@venus:~$ $ python python python3 python3.7-config python-config python2 python3.6 python3.7m pythontex python2.7 python3.6-config python3.7m-config pythontex3 python2.7-config python3.6m python3-config python-whiteboard python2-config python3.6m-config python3m python2-pbr python3.7 python3m-config bernd@venus:~$ python
If no other Python version shows up, python3.x has to be installed. Afterwards, we can start the newly installed version by typing python3:
$ python3 Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.
Once the Python interpreter is started, you can issue any command at the command prompt ">>>". Let's see, what happens, if we type in the word "hello":
hello
OUTPUT:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-f572d396fae9> in <module> ----> 1hello NameError: name 'hello' is not defined
Of course, "hello" is not a proper Python command, so the interactive shell returns ("raises") an error.
The first real command we will use is the print command. We will create the mandatory "Hello World" statement:
print("Hello World")
OUTPUT:
Hello World
It couldn't have been easier, could it? Oh yes, it can be written in a even simpler way. In the Interactive Python Interpreter - but not in a program - the print is not necessary. We can just type in a string or a number and it will be "echoed"
"Hello World"
OUTPUT:
'Hello World'
3
OUTPUT:
3
How to Quit the Python Shell
So, we have just started, and we are already talking about quitting the shell. We do this, because we know, how annoying it can be, if you don't know how to properly quit a program.
It's easy to end the interactive session: You can either use exit() or Ctrl-D (i.e. EOF) to exit. The brackets behind the exit function are crucial. (Warning: Exit without brackets works in Python2.x but doesn't work anymore in Python3.x)
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Upcoming online Courses
The Shell as a Simple Calculator
In the following example we use the interpreter as a simple calculator by typing an arithmetic expression:
4.567 * 8.323 * 17
OUTPUT:
646.189397
Python follows the usual order of operations in expressions. The standard order of operations is expressed in the following enumeration:
- exponents and roots
- multiplication and division
- addition and subtraction
In other words, we don't need parentheses in the expression "3 + (2 * 4)":
3 + 2 * 4
OUTPUT:
11
The most recent output value is automatically stored by the interpreter in a special variable with the name "_". So we can print the output from the recent example again by typing an underscore after the prompt:
_
OUTPUT:
11
The underscore can be used in other expressions like any other variable:
_ * 3
OUTPUT:
33
The underscore variable is only available in the Python shell. It's NOT available in Python scripts or programs.
Using Variables
It's simple to use variables in the Python shell. If you are an absolute beginner and if you don't know anything about variables, please refer to our chapter about variables and data types. Values can be saved in variables. Variable names don't require any special tagging, like they do in Perl, where you have to use dollar signs, percentage signs and at signs to tag variables:
maximal = 124
width = 94
print(maximal - width)
OUTPUT:
30
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Multiline Statements
We haven't introduced multiline statements so far. So beginners can skip the rest of this chapter and can continue with the following chapters.
We will show how the interactive prompt deals with multiline statements like for loops.
l = ["A", 42, 78, "Just a String"]
for character in l:
print(character)
OUTPUT:
A 42 78 Just a String
After having input "for character in l: " the interpretor expects the input of the next line to be indented. In other words: the interpretor expects an indented block, which is the body of the loop. This indented block will be iterated. The interpretor shows this "expectation" by displaying three dots "..." instead of the standard Python interactive prompt ">>>". Another special feature of the interactive shell: when we are done with the indented lines, i.e. the block, we have to enter an empty line to indicate that the block is finished. Attention: the additional empty line is only necessary in the interactive shell! In a Python program, it is enough to return to the indentation level of the "for" line, the one with the colon ":" at the end.
Strings
Strings are created by putting a sequence of characters in quotes. Strings can be surrounded by single quotes, double quotes or triple quotes, which are made up of three single or three double quotes. Strings are immutable. In other words, once defined, they cannot be changed. We will cover this topic in detail in another chapter.
"Hello" + " " + "World"
OUTPUT:
'Hello World'
A string in triple quotes can span several lines without using the escape character:
city = """
... Toronto is the largest city in Canada
... and the provincial capital of Ontario.
... It is located in Southern Ontario on the
... northwestern shore of Lake Ontario.
... """
print(city)
OUTPUT:
Toronto is the largest city in Canada and the provincial capital of Ontario. It is located in Southern Ontario on the northwestern shore of Lake Ontario.
Multiplication on strings is defined, which is essentially a multiple concatenation:
".-." * 4
OUTPUT:
'.-..-..-..-.'
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Upcoming online Courses