Press shift + enter to run a cell.
You can go back to previous cells, change them and re-run them.
print("Hello World")
X = 112
print(X)
range(10)
IPython notebook allows tab-completion and shows docstrings (by pressing tab [shift-tab in latest versions] after the opening parantheses), or using ?
range?
Cells can be arbitrary long or short, and can define functions that will be available in other cells.
def fib(n):
if n in [0, 1]:
return n
return fib(n - 1) + fib(n - 2)
for x in range(5):
print(fib(x))
Numpy array are the most common numeric data type.
As in other environments, it is very beneficial to vectorize your code (array-based computing) to make use of fast C and Fortran implementations.
import numpy as np
np.ones(10)
np.ones((10, 10))
np.arange(10)
Numpy allows broadcasting over rows, leading to practical short-hand notations.
X = np.ones((10, 10)) + np.array([3, 5, 1, 10, 6, 12, 98, 1, 0, 3])
print(X)
Most libraries in Python use object oriented interfaces.
X.mean(axis=0)
Numpy has all standard array functions, linear algebra, and fancy indexing.
X[:3, 1:4]
X[:, ::2]
X = np.random.randint(10, size=(32, 103))
X
X[np.random.randint(32, size=10)]
X[np.random.randint(32, size=10)].shape
For all of your plotting needs!
Enable in-line plotting (can be done in config file)
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.random.uniform(size=10))
plt.bar(np.arange(10), np.random.uniform(size=10))
plt.hist(np.random.normal(size=1000))
x, y = np.random.uniform(size=(2, 10))
plt.scatter(x, y, marker="x")
print(np.eye(5))
plt.matshow(np.eye(5))
f(x) = x ** 2
using lines. How can you get a smooth plot?