Many people, when they think of Python, they think of the interactive Jupyter environment in a browser. We will come to Jupyter soon. But it is important to understand that Python itself is a more fundamental concept: it is a programming language, and is not dependent on any browser or environment as such.
The most basic way to use Python would be from the Linux command line (called a 'shell'), if you use the Linux operating system. For users of a Macbook, also MacOS has a command line interface (a 'terminal'). If you use Windows it is less obvious, but it, too, has a command line interface.
Here we will discuss how to use Python interactively from the command-line.
To run Python code interactively, one can use the standard Python prompt, which can be launched by typing
python in your standard shell:
$ python Python 3.4.1 (default, May 21 2014, 21:17:51) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
>>> indicates that Python is ready to accept commands. If you type
a = 1 then press enter, this will assign the value
a. If you then type
a you will see the value of
a (this is equivalent to
>>> a = 1 >>> a 1
The Python shell can execute any Python code, even multi-line statements, though it is often more convenient to use Python non-interactively for such cases.
The default Python shell is limited, so we will use the IPython (or interactive Python) shell here. This is an add-on package that adds many features to the default Python shell, including the ability to edit and navigate the history of previous commands, as well as the ability to tab-complete variable and function names. To start up IPython, type:
$ ipython Python 3.4.1 (default, May 21 2014, 21:17:51) Type "copyright", "credits" or "license" for more information. IPython 2.1.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In :
The first time you start up IPython, it will display a message which you can skip over by pressing
>>> symbols are now replaced by
In [x], and output, when present, is prepended with
Out [x]. If we now type the same commands as before, we get:
In : a = 1 In : a Out: 1
If you now type the up arrow twice, you will get back to
a = 1.
To exit the Python shell at any time and return to the command prompt of the terminal, type