Python Interpreter

Python offers a comfortable command line interface with the Python shell, which is also known as 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 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
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
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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")
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"
'Hello World'
>>> 3
3
>>>

Quit Python shell

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 Python 3.x)

Leave a comment