| 1 |
================= |
| 2 |
Python vs IPython |
| 3 |
================= |
| 4 |
|
| 5 |
This document is meant to highlight the main differences between the Python |
| 6 |
language and what are the specific constructs you can do only in IPython. |
| 7 |
|
| 8 |
Unless expressed otherwise all of the constructs you will see here will raise a |
| 9 |
``SyntaxError`` if run in a pure Python shell, or if executing in a Python |
| 10 |
script. |
| 11 |
|
| 12 |
Each of these features is described more in detail in the further parts of the documentation. |
| 13 |
|
| 14 |
|
| 15 |
Quick overview: |
| 16 |
=============== |
| 17 |
|
| 18 |
|
| 19 |
All the following constructs are valid IPython syntax: |
| 20 |
|
| 21 |
.. code-block:: ipython |
| 22 |
|
| 23 |
In [1]: ? |
| 24 |
|
| 25 |
.. code-block:: ipython |
| 26 |
|
| 27 |
In [1]: ?object |
| 28 |
|
| 29 |
|
| 30 |
.. code-block:: ipython |
| 31 |
|
| 32 |
In [1]: object? |
| 33 |
|
| 34 |
.. code-block:: ipython |
| 35 |
|
| 36 |
In [1]: *pattern*? |
| 37 |
|
| 38 |
.. code-block:: ipython |
| 39 |
|
| 40 |
In [1]: %shell like --syntax |
| 41 |
|
| 42 |
.. code-block:: ipython |
| 43 |
|
| 44 |
In [1]: !ls |
| 45 |
|
| 46 |
.. code-block:: ipython |
| 47 |
|
| 48 |
In [1]: my_files = !ls ~/ |
| 49 |
In [1]: for i, file in enumerate(my_files): |
| 50 |
...: raw = !echo $file |
| 51 |
...: !echo {file[0].upper()} $raw |
| 52 |
|
| 53 |
|
| 54 |
.. code-block:: ipython |
| 55 |
|
| 56 |
In [1]: %%perl magic --function |
| 57 |
...: @months = ("July", "August", "September"); |
| 58 |
...: print $months[0]; |
| 59 |
|
| 60 |
|
| 61 |
Each of these constructs is compiled by IPython into valid python code and will |
| 62 |
do most of the time what you expect it will do. Let's see each of these examples |
| 63 |
in more detail. |
| 64 |
|
| 65 |
|
| 66 |
Accessing help |
| 67 |
============== |
| 68 |
|
| 69 |
As IPython is mostly an interactive shell, the question mark is a simple |
| 70 |
shortcut to get help. A question mark alone will bring up the IPython help: |
| 71 |
|
| 72 |
.. code-block:: ipython |
| 73 |
|
| 74 |
In [1]: ? |
| 75 |
|
| 76 |
IPython -- An enhanced Interactive Python |
| 77 |
========================================= |
| 78 |
|
| 79 |
IPython offers a combination of convenient shell features, special commands |
| 80 |
and a history mechanism for both input (command history) and output (results |
| 81 |
caching, similar to Mathematica). It is intended to be a fully compatible |
| 82 |
replacement for the standard Python interpreter, while offering vastly |
| 83 |
improved functionality and flexibility. |
| 84 |
|
| 85 |
At your system command line, type 'ipython -h' to see the command line |
| 86 |
options available. This document only describes interactive features. |
| 87 |
|
| 88 |
MAIN FEATURES |
| 89 |
------------- |
| 90 |
... |
| 91 |
|
| 92 |
A single question mark before or after an object available in the current |
| 93 |
namespace will show help relative to this object: |
| 94 |
|
| 95 |
.. code-block:: ipython |
| 96 |
|
| 97 |
In [6]: object? |
| 98 |
Docstring: The most base type |
| 99 |
Type: type |
| 100 |
|
| 101 |
|
| 102 |
A double question mark will try to pull out more information about the object, |
| 103 |
and if possible display the python source code of this object. |
| 104 |
|
| 105 |
.. code-block:: ipython |
| 106 |
|
| 107 |
In[1]: import collections |
| 108 |
In[2]: collections.Counter?? |
| 109 |
|
| 110 |
Init signature: collections.Counter(*args, **kwds) |
| 111 |
Source: |
| 112 |
class Counter(dict): |
| 113 |
'''Dict subclass for counting hashable items. Sometimes called a bag |
| 114 |
or multiset. Elements are stored as dictionary keys and their counts |
| 115 |
are stored as dictionary values. |
| 116 |
|
| 117 |
>>> c = Counter('abcdeabcdabcaba') # count elements from a string |
| 118 |
|
| 119 |
>>> c.most_common(3) # three most common elements |
| 120 |
[('a', 5), ('b', 4), ('c', 3)] |
| 121 |
>>> sorted(c) # list all unique elements |
| 122 |
['a', 'b', 'c', 'd', 'e'] |
| 123 |
>>> ''.join(sorted(c.elements())) # list elements with repetitions |
| 124 |
'aaaaabbbbcccdde' |
| 125 |
... |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
If you are looking for an object, the use of wildcards ``*`` in conjunction |
| 130 |
with a question mark will allow you to search the current namespace for objects with |
| 131 |
matching names: |
| 132 |
|
| 133 |
.. code-block:: ipython |
| 134 |
|
| 135 |
In [24]: *int*? |
| 136 |
FloatingPointError |
| 137 |
int |
| 138 |
print |
| 139 |
|
| 140 |
|
| 141 |
Shell Assignment |
| 142 |
================ |
| 143 |
|
| 144 |
|
| 145 |
When doing interactive computing it is a common need to access the underlying shell. |
| 146 |
This is doable through the use of the exclamation mark ``!`` (or bang). |
| 147 |
|
| 148 |
This allows to execute simple commands when present in beginning of the line: |
| 149 |
|
| 150 |
.. code-block:: ipython |
| 151 |
|
| 152 |
In[1]: !pwd |
| 153 |
/User/home/ |
| 154 |
|
| 155 |
Edit file: |
| 156 |
|
| 157 |
.. code-block:: ipython |
| 158 |
|
| 159 |
In[1]: !mvim myfile.txt |
| 160 |
|
| 161 |
|
| 162 |
The line after the bang can call any program installed in the underlying |
| 163 |
shell, and support variable expansion in the form of ``$variable`` or ``{variable}``. |
| 164 |
The later form of expansion supports arbitrary python expressions: |
| 165 |
|
| 166 |
.. code-block:: ipython |
| 167 |
|
| 168 |
In[1]: file = 'myfile.txt' |
| 169 |
|
| 170 |
In[2]: !mv $file {file.upper()} |
| 171 |
|
| 172 |
|
| 173 |
The bang (``!``) can also be present on the right hand side of an assignment, just |
| 174 |
after the equal sign, or separated from it by a white space. In this case the |
| 175 |
standard output of the command after the bang will be split out into lines |
| 176 |
in a list-like object and assigned to the left hand side. |
| 177 |
|
| 178 |
This allows you, for example, to put the list of files of the current working directory in a variable: |
| 179 |
|
| 180 |
.. code-block:: ipython |
| 181 |
|
| 182 |
In[1]: my_files = !ls |
| 183 |
|
| 184 |
|
| 185 |
You can combine the different possibilities in for loops, conditions, functions...: |
| 186 |
|
| 187 |
.. code-block:: ipython |
| 188 |
|
| 189 |
my_files = !ls ~/ |
| 190 |
for i, file in enumerate(my_files): |
| 191 |
raw = !echo $backup $file |
| 192 |
!cp $file {file.split('.')[0] + '.bak'} |
| 193 |
|
| 194 |
|
| 195 |
Each ``!`` gets executed in a separate shell, so changing directory by ``!cd`` or env vars ``!export FOO=bar`` will have no effect. |
| 196 |
Use instead the built-in magics ``%cd DIR/`` (there are also ``%pushd DIR/``, ``%dirs``, ``%popd``) and ``%env FOO=bar``. |
| 197 |
|
| 198 |
|
| 199 |
Magics |
| 200 |
------ |
| 201 |
|
| 202 |
Magic functions (magics) are often present in the form of shell-like syntax, but they are |
| 203 |
python functions under the hood. The syntax and assignment possibilities are |
| 204 |
similar to the one with the bang (``!``) syntax, but with more flexibility and |
| 205 |
power. Magic functions start with a percent sign (``%``) or double percent signs (``%%``). |
| 206 |
|
| 207 |
A magic call with a single percent sign will act only on one line: |
| 208 |
|
| 209 |
.. code-block:: ipython |
| 210 |
|
| 211 |
In[1]: %xmode |
| 212 |
Exception reporting mode: Verbose |
| 213 |
|
| 214 |
Magics support assignment: |
| 215 |
|
| 216 |
.. code-block:: ipython |
| 217 |
|
| 218 |
In [1]: results = %timeit -r1 -n1 -o list(range(1000)) |
| 219 |
62.1 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each) |
| 220 |
|
| 221 |
In [2]: results |
| 222 |
<TimeitResult : 62.1 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)> |
| 223 |
|
| 224 |
Magics with double percent signs (``%%``) can spread over multiple lines, but they do not support assignments: |
| 225 |
|
| 226 |
.. code-block:: ipython |
| 227 |
|
| 228 |
In[1]: %%bash |
| 229 |
... : echo "My shell is:" $SHELL |
| 230 |
... : echo "My disk usage is:" |
| 231 |
... : df -h |
| 232 |
My shell is: /usr/local/bin/bash |
| 233 |
My disk usage is: |
| 234 |
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on |
| 235 |
/dev/disk1 233Gi 216Gi 16Gi 94% 56788108 4190706 93% / |
| 236 |
devfs 190Ki 190Ki 0Bi 100% 656 0 100% /dev |
| 237 |
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net |
| 238 |
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /hom |