0% found this document useful (0 votes)
16 views

Python

Uploaded by

atiur.iitk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python

Uploaded by

atiur.iitk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python & Pylab Cheat Sheet NumPy & Friends Statistics

Running The following import statement is assumed: sum(l,d) or a.sum(d) sum elements along d
from pylab import * mean(l,d) or a.mean(d) mean along d
python3 standard python shell.
ipython3 improved interactive shell. General Math std(l,d) or a.std(d) standard deviation along d
ipython3 --pylab ipython including pylab min(l,d) or a.min(d) minima along d
f: float, c: complex: max(l,d) or a.max(d) maxima along d
python3 file.py run file.py
abs(c) absolute value of f or c
python3 -i file.py run file.py, stay in interactive mode
sign(c) get sign of f or c Misc functions
To quit use exit() or [ctrl]+[d] fix(f) round towards 0
floor(f) round towards − inf loadtxt(file) read values from file
Getting Help polyval(coeff,xvals) evaluate polynomial at xvals
ceil(f) round towards + inf
help() interactive Help f.round(p) round f to p places roots(coeff) find roots of polynomial
help(object) help for object angle(c) angle of complex number map(func,list) apply func on each element of list
object? ipython: help for object sin(c) sinus of argument
object?? ipython: extended help for object arcsin(c) arcsin of argument Plotting
%magic ipython: help on magic commands cos, tan,... analogous
Import Syntax, e.g. for π Plot Types
Defining Lists, Arrays, Matrices
import numpy use: numpy.pi plot(xvals, yvals, ’g+’) mark 3 points with green +
import numpy as np use: np.pi l: list, a: array:
errorbar() like plot with error bars
from numpy import pi use: pi [[1,2],[3,4,5]] basic list
semilogx(), semilogx() like plot, semi-log axis
from numpy import * use: pi (use sparingly) array([[1,2],[3,4]]) array from ”rectangular” list
loglog() double logarithmic plot
matrix([[1,2],[3,4]]) matrix from 2d-list
Types polar(phi_vals, rvals) plot in polar coordinates
range(min, max, step) integers in [min, max)
i = 1 Integer hist(vals, n_bins) create histogram from values
list(range(...)) list from range()
f = 1. Float bar(low_edge, vals, width) create bar-plot
arange(min, max, step) integer array in [min, max)
c = 1+2j Complex with this: contour(xvals,yvals,zvals) create contour-plot
frange(min, max, step) float array in [min, max]
True/False Boolean c.real 1.0 linspace(min, max, num) num samples in [min, max]
’abc’ String c.imag 2.0 meshgrid(x,y) create coord-matrices Pylab Plotting Equivalences
"abc" String c.conjugate() 1-2j zeros, ones, eye generate special arrays
figure() fig = figure()
Operators ax = axes()
Element Access
mathematics comparison subplot(2,1,1) ax = fig.add_subplot(2,1,1)
l[row][col] list: basic access plot() ax.plot()
+ addition = assign
l[min:max] list: range access [min,max) errorbar() ax.errorbar()
- subtraction == equal
a[row,col] or a[row][col] array: basic access semilogx, ... analogous
* multiplication != unequal
a[min:max,min:max] array: range access [min,max) polar() axes(polar=True) and ax.plot()
i/i int division < less
a[list] array: select indices in list axis() ax.set_xlim(), ax.set_ylim()
i/f float division <= less-equal
a[np.where(cond)] array: select where cond true grid() ax.grid()
** power >= greater-equal
% modulo > greater title() ax.set_title()
List/Array Properties
xlabel() ax.set_xlabel()
Basic Syntax
len(l) size of first dim legend() ax.legend()
raw_input(’foo’) read string from command-line a.size total number of entries colorbar() fig.colorbar(plot)
class Foo(Object): ... class definition a.ndim number of dimensions
def bar (args): ... function/method definition a.shape size along dimensions Plotting 3D
if c: ... elif c: ... else: branching ravel(l) or a.ravel() convert to 1-dim
try: ... except Error : ... exception handling a.flat iterate all entries from mpl_toolkits.mplot3d import Axes3D
while cond: ... while loop
for item in list: ... for loop Matrix Operations ax = fig.add_subplot(...,projection=’3d’)
[item for item in list] for loop, list notation or ax = Axes3D(fig) create 3d-axes object
a: array, M: matrix: ax.plot(xvals, yvals, zvals) normal plot in 3d
Useful tools a*a element-wise product ax.plot_wireframe wire mesh
pylint file.py static code checker dot(a,a) or M*M dot product ax.plot_surface colored surface
pydoc file parse docstring to man-page cross(a,a) cross product
python3 -m doctest run examples in docstring inv(a) or M.I inverted matrix License: CC-by-sa
file.py transpose(a) or M.T transposed matrix Copyright: January 15, 2016, Nicola Chiapolini
python3 -m pdb file.py run in debugger det(a) calculate determinate http://www.physik.uzh.ch/∼nchiapol

You might also like