Help functionality and tab completion
?
command returns the docstring of the corresponding method, class, etc.
help(.)
gives the full documentation.
dict?
help(dict)
a = {"a":1,"b":2}
?
can also be applied to instances
a?
iPython supports tab completion giving the list of available functions or for a function a description of the function singature.
a.update({"c":3})
a.keys()
Run scripts and use system shell commmands
!
allows to invoke system shell commands.
With run
we can invoke python scripts in an iPython shell
!ls
run script.py
%load <filename>
loads the content of the file into the cell
# %load script.py
#!/usr/bin/python
import sys
if __name__ == '__main__':
print("Hello, I am a script!")
Magic function
iPython comes with a set of functions that can be called in a command-line style.
%
runs the line as a command, %%
the whole cell, time
and timeit
are two examples (single and statistical evaluation)
%time a = 1
%%time
for _ in range(int(1e6)):
pass
user refers to the time spent to execute the command, sys refers to the time spent in the kernel
%timeit a = 1
%%timeit
for _ in range(int(1e6)):
pass
a = %run script.py
t = %time dict()
%%debug
a = 3
for _ in range(a):
a+=1
print("End")
%%perl
use strict;
my $a = 5;
while($a > 0) {
print "$a ";
$a--;
}
print "\n";
%%perl
# Function definition
sub Hello {
print "Hello, World!\n";
}
Hello()
%%latex
$$a=1+\gamma$$
Some text
\begin{eqnarray}
a^2+b^2 &=&c^2\\
x&=&y
\end{eqnarray}
%load_ext rpy2.ipython
%%R
library(lattice)
attach(mtcars)
# scatterplot matrix
splom(mtcars[c(1,3,4,5,6)], main="MTCARS Data")
Markdown text can be used to document your work. HTML and custom tags allow you to structure your text and explain your code
, for example what a list comprehension is squares = [n**2 for n in numbers]
.
And even LaTeX code can be used in markdown cells: $\nabla f = (\partial_x f,\partial_y f,\partial_z f)$