Home

Overview python commands

Printing
>>>print 5+3
8
>>>print "Cat"+"Dog"
CatDog
>>>print "Cat","Dog"
Cat Dog
# A trailing comma avoids the 
# newline after the output
>>>print "hello"*5
hello hello hello hello hello


Operators
#arithmetic operators:
=, +, -, *, /, +=, -=
// #integer division
** #exponential
%  #remainder
#comparison operators:
==, >, <, >=, <=, !=, <>, not


Types
type()
float(), int(), str()


Number formatting
>>> x = 22/3

>>> str(x)
'7.33333333333'

>>> print 'a number: %i' % x
a number: 7

>>> print 'another number: %f' % x
another number: 7.333333

>>> print 'and yet another: %e' % x
and yet another: 7.333333e+00

>>> print '%3i' % x
  7

>>> print '%03i' % x
007

>>> print '%6.4f' % x
7.3333


Formatting strings
.upper()
.lower()
.split()
.join()
.partition()
.translate()
.replace()

Input / output
raw_input()
urllib.urlopen() #from urllib library
.read() #read object made by urlopen()
open()
.readline()
.write()
.close()


Flow control

if:
elif:
else:

for x in myList:
for x in range(1,5):
for x in range(10,0,-1):
for i,b in enumerate(myList):

while:

try:
except:

break
continue

def myFunction():
	return


lists
newList=[]
newList.append()
newList.extend()
newList.insert()
newList.remove()
del
newList.pop()
newList.index()
newList.sort()
newList.reverse()
newList[1:5]
newList[1:-1]
newList[:]
newList[0][2]
sorted()


graphics
from pylab import figure, show
fig = figure()
panel = fig.add_subplot(1,1,1)
panel.scatter(x,y,color=’red’)
panel.plot(x,y)
panel.set_xlabel()
panel.set_ylabel()
panel.set_aspect()
panel.xaxis.set_visible()
panel.yaxis.set_visible()
panel.errorbar()
panel.set_title()
panel.legend()
show()

matplotlib.animation.FuncAnimation


miscellaneous
global
len()
numpy.linspace()
array()
copy.deepcopy()
id()
random.randint #from random library
random.random