#list of tuples containing data of solids
#each tuple contains name of substance, density(10**3), Young's modulus(10**10), Rigidity modulus(10**10)
solids=[('Aluminium',2.7,6.9,2.4),('Brass',8.6,9,3.45),('Copper',8.89,11,3.4),\
('Glass',2.6,6,3.0),('Iron',7.5,10,3.5)]
print "Substance\t\t\tDensity\t\t\tYoung's modulus\t\t\tRigidity modulus"
print
for item in sorted(solids,key=lambda a:(-a[1],a[2]),reverse=True):
print item[0],"\t\t\t",item[1],"\t\t\t\t",item[2],"\t\t\t\t",item[3]
Search This Blog
Tuesday, January 11, 2011
Sorting in multiple fields using lists of tuples
Sunday, January 9, 2011
Dictionary example
#program to determine resistance of a resistor using colour bands--uses dictionary
color1={'black':0,'brown':1,'red':2,'orange':3,\
'yellow':4, 'green':5,'blue':6,'violet':7,\
'gray':8,'white':9}
color2={'black':1e0,'brown':1e1,'red':1e2,'orange':1e3,\
'yellow':1e4, 'green':1e5,'blue':1e6,'violet':1e7,\
'gray':1e8,'white':1e9,'gold':1e-1,'silver':1e-2}
color3={'brown':1,'red':2,'green':0.5,'blue':0.25,\
'violet':0.1,'gray':0.05,'gold':5,'silver':10,'none':20}
resist=0
while 1:
b1=str(raw_input("Enter colour first band = ")).lower()
if b1 in color1:
resist=color1[b1]
break
else:
print "Enter a valid color"
continue
resist *=10
while 1:
b2=str(raw_input("Enter colour second band = ")).lower()
if b2 in color1:
resist +=color1[b2]
break
else:
print "Enter a valid color"
continue
while 1:
b3=str(raw_input("Enter colour third band = ")).lower()
if b3 in color2:
resist *=color2[b3]
break
else:
print "Enter a valid color"
continue
while 1:
b4=str(raw_input("Enter colour fourth band = ")).lower()
if b4 in color3:
tol=color3[b4]
break
else:
print "Enter a valid color"
continue
print resist," +-",tol,"%"
color1={'black':0,'brown':1,'red':2,'orange':3,\
'yellow':4, 'green':5,'blue':6,'violet':7,\
'gray':8,'white':9}
color2={'black':1e0,'brown':1e1,'red':1e2,'orange':1e3,\
'yellow':1e4, 'green':1e5,'blue':1e6,'violet':1e7,\
'gray':1e8,'white':1e9,'gold':1e-1,'silver':1e-2}
color3={'brown':1,'red':2,'green':0.5,'blue':0.25,\
'violet':0.1,'gray':0.05,'gold':5,'silver':10,'none':20}
resist=0
while 1:
b1=str(raw_input("Enter colour first band = ")).lower()
if b1 in color1:
resist=color1[b1]
break
else:
print "Enter a valid color"
continue
resist *=10
while 1:
b2=str(raw_input("Enter colour second band = ")).lower()
if b2 in color1:
resist +=color1[b2]
break
else:
print "Enter a valid color"
continue
while 1:
b3=str(raw_input("Enter colour third band = ")).lower()
if b3 in color2:
resist *=color2[b3]
break
else:
print "Enter a valid color"
continue
while 1:
b4=str(raw_input("Enter colour fourth band = ")).lower()
if b4 in color3:
tol=color3[b4]
break
else:
print "Enter a valid color"
continue
print resist," +-",tol,"%"
Saturday, January 8, 2011
Lists---Tuples---Dictionaries
"A list"
cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
"If you want to print for example the first name on the list you would do the following"
print cats[0]
"Also if you want to add a name to the list you would do this"
cats.append('Catherine')
"To remove a name you would do this"
del cats[0]
cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
"If you want to print for example the first name on the list you would do the following"
print cats[0]
"Also if you want to add a name to the list you would do this"
cats.append('Catherine')
"To remove a name you would do this"
del cats[0]
Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the names of the months of the year.
"Here is an example of a tuple"
months = ('January','February','March','April','May','June',\
'July','August','September','October','November',' December')
"So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)"
months = ('January','February','March','April','May','June',\
'July','August','September','October','November',' December')
"So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)"
There are also dictionaries
Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.
"Here is a simple phonebook"
phonebook = {'Andrew Parson':8806336, \
'Emily Everett':6784346, 'Peter Power':7658344, \
'Lewis Lame':1122345}
"To add a name to the phonebook you would do the following"
phonebook['Gingerbread Man'] = 1234567
"If you want to remove a name you would do the same as you would do in a list"
del phonebook['Andrew Parson']
daniweb IT discussion community
Wednesday, January 5, 2011
Value of pi using Monte Carlo Method
#To determine value of pi using Monte Carlo Method
from random import *
j=0
for i in range(10000000):
x=random()
y=random()
if (x**2+y**2)<=1:j+=1
print "Value of pi = ",4.0*j/i
by Prof. Purushothaman Vadakkedam
from random import *
j=0
for i in range(10000000):
x=random()
y=random()
if (x**2+y**2)<=1:j+=1
print "Value of pi = ",4.0*j/i
by Prof. Purushothaman Vadakkedam
Trapezoidal and Simpson's rule -Integration
#This program calculates integral of 1/1+x in the interval 0 to 1
#using both trapezodal and simpsons rule using Lists without
#importing any modules
h=.001
N=1000
x=[h*i for i in range(N)]
y=[1/(1+i) for i in x]
trapezoidal=h/2*(y[0]+y[-1]+2*
sum(y[1:-2]))
print "tapezoidal rule gives%.6f"%trapezoidal
simpsons=(h/3)*(y[0]+y[-1]+4* sum(y[1:-2:2])+2*sum(y[2:-3:2] ))
print "simpsons rule gives %.6f"%simpsons
#using both trapezodal and simpsons rule using Lists without
#importing any modules
h=.001
N=1000
x=[h*i for i in range(N)]
y=[1/(1+i) for i in x]
trapezoidal=h/2*(y[0]+y[-1]+2*
print "tapezoidal rule gives%.6f"%trapezoidal
simpsons=(h/3)*(y[0]+y[-1]+4*
print "simpsons rule gives %.6f"%simpsons
by Prof. Ahamed Ashraf
Saturday, January 1, 2011
Simple version of orthogality problem
from math import *
dotproduct=[sin(pi*i/180)*cos(pi*i/180)for i in range(180)]
dotproduct=[sin(pi*i/180)*cos(pi*i/180)for i in range(180)]
print sum(dotproduct)
by Prof. Ahamed Ashraf
by Prof. Ahamed Ashraf
Arrays-2
#To verify orthogonality of sine and cosine functions
#Principle: Create two vctors X and Y using the values of sine
#and cosine for the same angle within relevant range.
#Show that sum of products xi.yi=0'
import Numeric, math
sum=0
for i in range(180):
theta=math.pi*i/180
x=math.cos(theta)
y=math.sin(theta)
sum+=x*y
print "The sum of products xi.yi=%e is negligible. Hence orthogonal in the range (0-180)degrees"%sum
by Prof. Purushothaman Vadakkedam
#Principle: Create two vctors X and Y using the values of sine
#and cosine for the same angle within relevant range.
#Show that sum of products xi.yi=0'
import Numeric, math
sum=0
for i in range(180):
theta=math.pi*i/180
x=math.cos(theta)
y=math.sin(theta)
sum+=x*y
print "The sum of products xi.yi=%e is negligible. Hence orthogonal in the range (0-180)degrees"%sum
by Prof. Purushothaman Vadakkedam
Subscribe to:
Posts (Atom)