Command Summary
You can get more information on the commands below from the following websites:
Quick Tour
Command |
Module |
Description |
python |
NA |
Command entered at the system command line to start Python |
ipython |
NA |
Command entered at the system command line to start IPython |
print('some text') |
built-in |
Python print command |
* + = / |
built-in |
Multiplication, addition, subtraction, and division |
** |
built-in |
x**y is x to the power y |
exit() |
built-in |
Exits Python |
from modulename import * |
built-in |
Imports all of the code in modulename into an interactive Python session |
import modulename as mn |
built-in |
Imports all of the code in modulename into an interactive Python session. Attributes, functions, and methods in modulename are referenced using mn. notation. For example to reference the function func() , in your code use mn.func() . |
linspace(start,stop,num=n) |
pylab (numpy) |
Creates an evenly spaced array of n elements from start to stop |
x,y,z=loadtxt(filename,usecols=(0,2,5),unpack=True) |
pylab (numpy) |
Loads data from a text file. filename is the name of the text file.
usecols specifies which columns to read, with 0 being the first.
unpack=True transposes the returned array, so that the data may be unpacked using x,y,z = loadtxt(...) |
len(x) |
built-in |
Returns the length or number of elements in the array x . |
plot(x,y,'bo') |
pylab (matplotlib.pyplot) |
Plot x and y using blue circle markers (bo ) |
xlabel('axis label') |
pylab (matplotlib.pyplot) |
Add a label to the x axis of a plot |
ylabel('axis label') |
pylab (matplotlib.pyplot) |
Add a label to the y axis of a plot |
title('plot title') |
pylab (matplotlib.pyplot) |
Add a title to a plot |
show() |
pylab (matplotlib.pyplot) |
Displays all plots in figure windows in non-interactive mode. |
fitpar=polyfit(x,y,deg) |
pylab (numpy) |
Fit a polynomial of degree deg (i.e. if deg = 2 , y(x) = a2 x2 + a1 x + a0, then fitpar = a2, a1, a0) |
sin(x) |
pylab (numpy) |
Returns the sine of x (x in radians)
x can be a single number or an array |
log10(x) |
pylab (numpy) |
Returns the base 10 log of x
x can be a single number or an array |
sqrt(x) |
pylab (numpy) |
Returns the square root of x
x can be a single number or an array |
Python Programming Essentials
if
statement syntax
if (conditional statement):
block of code
elif (conditional statement):
block of code
else (conditional statement):
block of code
while
statement syntax
while condition:
block of code
for
statement syntax
for element in sequence:
block of code
Function definition syntax
def function name(arguments):
function code
return function outputs
Command |
Module |
Description |
type(object) |
built-in |
Returns the object type |
array( [2,3,4] ) |
pylab (numpy) |
Creates a numpy array from a python list |
linspace(start, stop, num=n) |
pylab (numpy) |
Creates an evenly spaced numpy array of n elements from start to stop |
logspace(a, b, num=n) |
pylab (numpy) |
Creates an array of n numbers logarithmically spaced from 10a to 10b |
zeros(n) |
pylab (numpy) |
Creates a array of n zeros |
ones(n) |
pylab (numpy) |
Creates a array of n ones. |
rand(n) |
pylab (numpy) |
Creates an array of n random numbers in the interval 0 to 1 |
print("string with format specifiers" % (variables)) |
built-in |
Formatted print |
mystr = input("prompt text") |
built-in |
Writes "prompt text" to the screen. It reads a line from input, converts it to a string and returns it to mystr . |
range(start, stop, step) |
built-in |
Creates a list of integers from start to stop-1 , step specifies the increment, start and step are optional arguments. |
import module_name |
built-in |
Imports all modules_name objects, module_name objects are referenced as module_name.object_name |
import module_name as myname |
built-in |
Imports module_name objects under the name myname , objects referenced as myname.object_name |
from module_name import * |
built-in |
Imports all objects in module_name using the wild card symbol |
from module_name import objectA,objectB |
built-in |
Imports objectA and objectB from module_name |
Finding Roots of Equations
Command |
Module |
Description |
figure(num) |
pylab (matplotlib) |
Creates a new figure window or makes the figure num active. The num parameter is an integer that specifies the number of the figure window. |
clf() |
pylab (matplotlib) |
Clears the current figure. |
fsolve(func, x_est) |
scipy.optimize |
Returns the root of the equation defined by func(x)=0 given the starting estimate x_est . |
Numerical Integration
Command |
Module |
Description |
value,error = quad(func,a,b,args=(func_params)) |
scipy.integrate |
Integrate func from a to b . Extra arguments are passed using the optional args parameter. The limits of integration can be set to ±∞ infinity using inf or -inf . |
value,error = dblquad(func,a,b,gfun,hfun,args=(func_params)) |
scipy.integrate |
Computes the double integral. Note that gfun and hfun are the limits of integration for the inner integral and must be functions not numerical values. |
value,error = tplquad(func,a,b,gfun,hfun,qfun,rfun,args=(func_params)) |
scipy.integrate |
Computes the triple integral. Works like dblquad() . Except that qfun and rfun are the inner-most limits of integration. |
trapz(yd,xd) |
pylab (numpy) |
Computes the integral of a function given at discrete points xd and yd using the trapezoidal rule. NOTE the inverted order of the arguments of trapz(yd,xd) . |
Ordinary Differential Equations I
Command |
Module |
Description |
x = append(x,z) |
pylab
(numpy) |
Appends the array z to the array x . |
Ordinary Differential Equations II
Command |
Module |
Description |
X = odeint(f_func,X0,t) |
scipy.integrate |
Solves a system of first-order ODEs defined by the function f_func(state,time) given the initial conditions X0 at the times specified by the array t . It returns the two-dimensional array X . The first argument of X specifies the time point. The second specifies the component of the state vector. |
subplot(nRow, nCol, plotNum) |
pylab (matplotlib.pyplot) |
Makes an array of plots on the same page, where nRow specifies the number of plots per row, nCol specifies the number of columns, and plotNum specifies where the next plot should be placed. Numbering starts at the upper-left corner. |
axis('equal') |
pylab (matplotlib.pyplot) |
Forces the plot to have same scaling on x and y axes. |
axis([xmin, xmax, ymin, ymax]) |
pylab (matplotlib.pyplot) |
Sets the minimums and maximums for the x and y axes. |
For more information on the new plotting commands see the 2D Plotting tutorial or the matplotlib website.
Data Fitting
Command |
Module |
Description |
af,cov= curve_fit(func,x,y,sigma=yerr,p0=ag) |
scipy.optimize |
Does a non-linear least squares fit to the function func() using data in x and y with uncertainty sigma and initial guess parameters p0 . Returns best fit parameters and covariance matrix. |
prob=chi2.cdf(chisqr,dof) |
scipy.stats |
Computes the probability of getting a χ2 in the range 0 to chisqr , where dof is the number of degrees of freedom. |
errorbar(x,y,yerr=sigma,fmt='bo') |
pylab (matplotlib.pyplot) |
Plots data with errorbars given in yerr . fmt specifies the plot symbol, the default is fmt='-'. |
Monte Carlo Simulations
Command |
Module |
Description |
rand(d0, d1, ..., dn) |
numpy.random |
Creates an array of the given shape and populates it with random samples from a uniform distribution over [0, 1). |
randn(d0, d1, ..., dn) |
numpy.random |
Creates an array of the given shape and populates it with random numbers sampled from a univariate "normal" (Gaussian) distribution of mean 0 and variance 1. |