The Python interpreter can be started by installing the Python language and typing python
at the command prompt. On Windows Python interpreter can be invoked from the Command Prompt application or from the Start menu.
Python 3.5.2 | packaged by conda-forge | (default, Jul 26 2016, 01:32:08)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
With the interpreter running, you can begin to type and execute code snippets. Here we'll use the interpreter as a simple calculator, performing calculations and assigning values to variables:
>>> 1 + 1
2
>>> x = 5
>>> x * 3
15
The interpreter makes it very convenient to try out small snippets of Python code and to experiment with short sequences of operations.
An alternative interpreter called IPython (for Interactive Python) includes a host of convenient enhancements to the basic Python interpreter.
It can be started by typing ipython
at the command prompt (or from Start menu on Windows):
$ ipython
Python 3.5.2 | packaged by conda-forge | (default, Jul 26 2016, 01:32:08)
Type "copyright", "credits" or "license" for more information.
IPython 5.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.
ipython
In [1]:
The main aesthetic difference between the Python interpreter and the enhanced IPython interpreter lies in the command prompt: Python uses >>>
by default, while IPython uses numbered commands (e.g. In [1]:
).
Regardless, we can execute code line by line just as we did before:
ipython
In [1]: 1 + 1
Out[1]: 2
In [2]: x = 5
In [3]: x * 3
Out[3]: 15
Note that just as the input is numbered, the output of each command is numbered as well.
Command | Description |
---|---|
foo? |
get help for the object 'foo' |
foo?? |
get more help for the object 'foo' |
%hist |
Command history. %hist -g foo will search history for 'foo' |
_i, _ii, _iii |
Previous, next previous, next next previous input |
_, __, ___ |
Previous, next previous, next next previous output |
%timeit x=2**100 |
time the execution of x=2**100 |
%reset |
Cleans up the namespace |
%lsmagic |
print currently available magic functions |
Cell magic | Description |
---|---|
%%timeit x=20 x**100 |
time x**100 with a setup of x=20 ; setup code is not counted |
%%writefile blahblah.txt text inside some file |
write text to file |
An extended IPython reference card is available here.
By convention, Python scripts are saved in files with a .py extension. For example, let's create a script called test.py which contains the following:
# file: test.py
print("Running test.py")
x = 5
print("Result is", 3 * x)
To run this file, we make sure it is in the current directory and type python
filename
at the command prompt:
$ python test.py
Running test.py
Result is 15
For more complicated programs, creating self-contained scripts like this one is a must.
CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py , CPython caches the byte code in a foo.pyc file right next to the source.
Though the notebook began as a Python-only format, it has since been made compatible with a large number of programming languages, and is now an essential part of the Jupyter Project.
The notebook is useful both as a development environment, and as a means of sharing work via rich computational and data-driven narratives that mix together code, figures, data, and text.
That's what we are using today to give you this presentation and will be using later throughout the workshop.