u,v=[3],[]
pu=u
v=pu
pv=v
print "u=",u[0],
print "&u=",id(u),
print "pu=",id(u),
print "*pu",pu[0]
print "v=",v[0],
print "&v=",id(v),
print "pv=",id(v),
print "*pv",pv[0]
v=[3]
u1=2*(v[0]+5)
pv=v
u2=2*(pv[0]+5)
print "u1=%d u2=%d" %(u1,u2)
v=[3]
pv=v
print "*pv=%d v=%d" %(pv[0],v[0])
pv[0]=0
print "*pv=%d v=%d" %(pv[0],v[0])
def funct1(u,v):
u=0
v=0
print "Within funct1 : u=%d v=%d" %(u,v)
return
def funct2(u,v):
u[0]=0
v[0]=0
print "Within funct2 : *pu=%d *pv=%d" %(u[0],v[0])
return
u=[1]
v=[3]
print "Before calling funct1: u=%d v=%d" %(u[0],v[0])
funct1(u[0],v[0])
print "After calling funct1 : u=%d v=%d" %(u[0],v[0])
print "Before calling funct2: u=%d v=%d" %(u[0],v[0])
funct2(u,v)
print "After calling funct2 : u=%d v=%d" %(u[0],v[0])
def scan_line(line,pv,pc,pd,pw,po):
for c in line:
if c=='A' or c=='E' or c=='I' or c=='O' or c=='U':
pv[0]+=1
elif c>='A' and c<='Z':
pc[0]+=1
elif c>='0' and c<='9':
pd[0]+=1
elif c==' ' or c=='\t':
pw[0]+=1
else:
po[0]+=1
return
vowel,consonants,digits,whitespc,other=[0],[0],[0],[0],[0]
line="Personal computers with memories in excess of 4096 KB are now quite common."
line=line.upper()
scan_line(line,vowel,consonants,digits,whitespc,other)
print "\n\n"
print "No. of vowels : ",vowel[0]
print "No. of consonants : ",consonants[0]
print "No. of digits : ",digits[0]
print "No. of whitespace characters : ",whitespc[0]
print "No. of other characters : ",other[0]
x=[10,11,12,13,14,15,16,17,18,19]
for i in range(0,10):
print "i=%d x[i]=%d *(x+1)=%d" %(i,x[i],x[i]),
print " &x[i]=",id(x[i]),
print " x+i=",id(x[i])
def main():
global x
y="This string is declared within main"
y=list(y)
x=list(x)
print x
print y
return
x="This string is declared externally"
main()
def reorder(x):
n=len(x)
for item in range(0,n-1):
for i in range(item+1,n):
if x[i]<x[item]:
temp=x[item]
x[item]=x[i]
x[i]=temp
return
n=10
x=[]
for i in range(0,n):
inp=i+1
print "\ni=%d x=%d" %(i+1,inp),
x.append(inp)
reorder(x)
print "\n"
for i in range(0,n):
print "i=%d x=%d" %(i+1,x[i])
i=[4]
i[0]=1
f=0.3
d=0.005
c='*'
px=i
print 'Values: i=%d f=%f d=%f c=%c \n' %(i[0],f,d,c)
print 'Addresses: &i=%X &f=%X &d=%X &c=%X \n' %(id(i[0]),id(f),id(d),id(c))
print 'Pointer Values: px=%X px+1=%X px+2=%X px+3=%X' %(id(px[0]),id(px[0]+1),id(px[0]+2),id(px[0]+3))
a=[1,2,3,4,5,6]
px=a[0]
py=a[5]
print 'px=%X py=%X \n' %(id(px),id(py))
print 'py - px = %X' %(id(py)-id(px))
def readinput(m,n,i=0):
at=[]
for row in range(0,m):
temp=[]
for col in range(0,n):
t=i
i+=2
temp.append(t)
at.append(temp)
return at
def computesum(a,b,m,n):
c=[]
for row in range(0,m):
temp=[]
for col in range(0,n):
t=a[row][col]+b[row][col]
temp.append(t)
c.append(temp)
return c
def writeoutput(c,m,n):
for row in range(0,m):
for col in range(0,n):
print "%4d" %(c[row][col]),
print
return
print "\n FIRST TABLE : \n"
a=readinput(5,5,1)
writeoutput(a,5,5)
print "\n SECOND TABLE : \n"
b=readinput(5,5,50)
writeoutput(b,5,5)
c=computesum(a,b,5,5)
print "Sums of the elements : \n"
writeoutput(c,5,5)
def readinput(m,n,i=0):
at=[]
for row in range(0,m):
temp=[]
for col in range(0,n):
t=i
i+=2
temp.append(t)
at.append(temp)
return at
def computesum(a,b,m,n):
c=[]
for row in range(0,m):
temp=[]
for col in range(0,n):
t=a[row][col]+b[row][col]
temp.append(t)
c.append(temp)
return c
def writeoutput(c,m,n):
for row in range(0,m):
for col in range(0,n):
print "%4d" %(c[row][col]),
print
return
print "\n FIRST TABLE : \n"
a=readinput(5,5,1)
writeoutput(a,5,5)
print "\n SECOND TABLE : \n"
b=readinput(5,5,50)
writeoutput(b,5,5)
c=computesum(a,b,5,5)
print "Sums of the elements : \n"
writeoutput(c,5,5)
def reorder(x):
n=len(x)
for item in range(0,n-1):
for i in range(item+1,n):
if x[item]>x[i]:
temp=x[item]
x[item]=x[i]
x[i]=temp
return
x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']
print 'Original list of strings :\n\n'
for i in x:
print "String : ",i
reorder(x)
print "\nReodered list of strings : \n\n"
for i in x:
print "String : ",i
def convert(mm,dd,yy):
yy-=1900
ndays=long(30.42*(mm-1)+dd)
if mm==2:
ndays+=1
if mm>2 and mm<8:
ndays-=1
if yy%4==0 and mm<2:
ndays+=1
ncycles=yy/4
ndays+=ncycles*1461
nyears=yy%4
if nyears>0:
ndays+=365*nyears+1
if ndays>59:
ndays-=1
day=ndays%7
return day
def main(mm,dd,yy):
day_of_week=convert(mm,dd,yy)
print "%s, %s %d %d" %(weekday[day_of_week],month[mm-1],dd,yy)
return
weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
month=['January','February','March','April','May','June','July','August',\
'September','October','November','December']
main(10,29,1929)
print
main(8,15,1945)
print
main(7,20,1969)
print
main(5,24,1997)
print
main(8,30,2010)
print
main(4,12,2069)
def ratio_choose(freq,a,m,n):
if freq=='C':
ratio=md3(a,m,n)
elif freq=='D':
ratio=md2(a,m,n)
else:
ratio=md1(a,m,n)
return ratio
def table(freq,a,m,n):
print "Interest Rate Future Amount\n\n"
for count in range(1,21):
i=0.01*count
f=a*ratio_choose(freq,i,m,n)
print " %2d %.2f" %(count,f)
return
def md1(i,m,n):
factor=1+i/m
ratio=12*(factor**(m*n)-1)/i
return ratio
def md2(i,m,n):
factor=1+i/m
ratio=(factor**(m*n)-1)/(factor**(m/12)-1)
return ratio
def md3(i,dummy,n):
ratio=(10**(i*n)-1)/(10**(i/12)-1)
return ratio
def main(freq):
m=0
freq=freq.upper()
if freq=='A':
m=1
print "\nAnual Compounding\n"
elif freq=='S':
m=2
print "\nSemiannual Compounding\n"
elif freq=='Q':
print "\nQuaterly Compounding\n"
m=4
elif freq=='M':
m=12
print "\nMonthly Compounding\n"
elif freq=='D':
m=360
print "\nDaily Compounding\n"
elif freq=='C':
m=0
print "\nContinuous Compounding\n"
else:
print "\nERROR!!! Please Repeat\n\n"
return
return m
a,n,freq=100,3,'m'
print "FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\n\n"
print "Frequency of Compunding (A,S,Q,M,D,C): ",freq
print "Amount of Each Monthly Payement : ",a
print "Number of years: ",n
m=main(freq)
table(freq,a,m,n)