#Fibonacci numbers using lists
fibon=[0,1]
num=input("how many Fibonacci numbers do you want?")
for i in range(num-2):
fibon.append(fibon[-2]+fibon[-1])
print fibon
# program to convert wavelength into frequency and energy
#enter 0 for wavelength, to move out
h=6.6e-34
c=3e8
a=[]
while 2:
wave_length=float(input("Wavelength= " ))
if wave_length==0:
break
a.append(wave_length)
i=0
for k in a:
f=c/k
e=h*f
a[i]=[k,f,e]
i+=1
a.sort()
print("Wavelength \t\t Frequency \t\t Energy")
print
for k in a:
print("%10.5f \t\t %10.5f \t\t %e"% (k[0],k[1],k[2]))
Search This Blog
Thursday, December 30, 2010
Fibonacci series
#Fibanocci series
a=0
b=1
n=input("Howmany elements of Fibanocci series?")
for i in range(n):
print b
a,b=b,b+a
# End of program
by Prof. Purushothaman Vadakkedam
a=0
b=1
n=input("Howmany elements of Fibanocci series?")
for i in range(n):
print b
a,b=b,b+a
# End of program
by Prof. Purushothaman Vadakkedam
Wednesday, December 29, 2010
Easy Quadratic equation solution -using cmath library
#Solution of Quadratic equation
from cmath import *
a,b,c=input("Give coefficients in the order a,b,c seperated by comma. ")
d=sqrt(b*b-4*a*c)
print "Root 1 = ",(-b+d)/(2*a),"Root 2 = ",(-b-d)/(2*a)
Contributed by Prof. Purushothaman Vadakkedam
import cmath
print "Hello! This program finds the complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = cmath.sqrt(b * b - 4 * a * c) # formula solving discriminant
root1 = (-b + discRoot) / (2 * a) # formula solving root1
root2 = (-b - discRoot) / (2 * a) # formula solving root2
print
print "The solutions are:", root1, root2 # display the results
Contributed by Prof. Ahamed Ashraf
from cmath import *
a,b,c=input("Give coefficients in the order a,b,c seperated by comma. ")
d=sqrt(b*b-4*a*c)
print "Root 1 = ",(-b+d)/(2*a),"Root 2 = ",(-b-d)/(2*a)
Contributed by Prof. Purushothaman Vadakkedam
import cmath
print "Hello! This program finds the complex solutions to a quadratic"
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = cmath.sqrt(b * b - 4 * a * c) # formula solving discriminant
root1 = (-b + discRoot) / (2 * a) # formula solving root1
root2 = (-b - discRoot) / (2 * a) # formula solving root2
print "The solutions are:", root1, root2 # display the results
Contributed by Prof. Ahamed Ashraf
Arrays
#To find largest and smallest in a set of numbers
x=0.0
n=input("Count n of numbers ? ")
print "Type numbers one at a time "
for i in range(n):
x=input(i)
if i==0:
large=small=x
else:
if large<x:
large=x
if small>x:
small=x
print "Largest number = %f, smallest number = %f"%(large,small)
#end of program
#Sorting
from Numeric import *
x=zeros(10,Float)
a=zeros(10,Float)
d=zeros(10,Float)
n=input("Count n of numbers to be sorted? ")
print " Give set of numbers to be sorted "
for i in range(n):
x[i]=input(i+1)
a[i]=x[i]
d[i]=x[i]
for i in range(n):
for j in range(i+1,n):
if a[i]>a[j]:
t=a[i]
a[i]=a[j]
a[j]=t
if d[i]<d[j]:
t=d[i]
d[i]=d[j]
d[j]=t
print " given ascending descending"
for i in range(n):
print "%10.5f %10.5f %10.5f"%(x[i],a[i],d[i])
#end of program
Contributed by Prof. Purushothaman Vadakkedam
x=0.0
n=input("Count n of numbers ? ")
print "Type numbers one at a time "
for i in range(n):
x=input(i)
if i==0:
large=small=x
else:
if large<x:
large=x
if small>x:
small=x
print "Largest number = %f, smallest number = %f"%(large,small)
#end of program
#Sorting
from Numeric import *
x=zeros(10,Float)
a=zeros(10,Float)
d=zeros(10,Float)
n=input("Count n of numbers to be sorted? ")
print " Give set of numbers to be sorted "
for i in range(n):
x[i]=input(i+1)
a[i]=x[i]
d[i]=x[i]
for i in range(n):
for j in range(i+1,n):
if a[i]>a[j]:
t=a[i]
a[i]=a[j]
a[j]=t
if d[i]<d[j]:
t=d[i]
d[i]=d[j]
d[j]=t
print " given ascending descending"
for i in range(n):
print "%10.5f %10.5f %10.5f"%(x[i],a[i],d[i])
#end of program
Contributed by Prof. Purushothaman Vadakkedam
Tuesday, December 28, 2010
python programs-3
1.
#program to calculate effective resistance when resistances are connected in series and parallel
#program terminates when 0 value is entered for resistance
#summation doesn't take place for -ve values
rip=0
rs=0
while 6:
r=float(input("Enter resistance "))
if r==0:
break
elif r<0:
continue
rip=rip+(1/r)
rs=rs+r
rp=1/rip
print " Effective resistance in series %.5f "%rs
print "Effective resistance in parallel %.5f "%rp
2.
#print first n fibonacci numbers
n=abs(int(input("enter number")))
f1=1
f2=1
if n==1:
print f1
else:
print f1,f2,
if n>2:
count=2
while count<n:
f3=f1+f2
print f3,
f1=f2
f2=f3
count+=1
3.
#To reverse a number and check whether it is a palindrome
n=input("enter number")
num=n
m=0
while n>0:
d=n%10
m=d+m*10
n=n/10
if m==num:
print "palindrome number"
print m
#program to calculate effective resistance when resistances are connected in series and parallel
#program terminates when 0 value is entered for resistance
#summation doesn't take place for -ve values
rip=0
rs=0
while 6:
r=float(input("Enter resistance "))
if r==0:
break
elif r<0:
continue
rip=rip+(1/r)
rs=rs+r
rp=1/rip
print " Effective resistance in series %.5f "%rs
print "Effective resistance in parallel %.5f "%rp
2.
#print first n fibonacci numbers
n=abs(int(input("enter number")))
f1=1
f2=1
if n==1:
print f1
else:
print f1,f2,
if n>2:
count=2
while count<n:
f3=f1+f2
print f3,
f1=f2
f2=f3
count+=1
3.
#To reverse a number and check whether it is a palindrome
n=input("enter number")
num=n
m=0
while n>0:
d=n%10
m=d+m*10
n=n/10
if m==num:
print "palindrome number"
print m
Sunday, December 26, 2010
python programs-2
Example-3
#largest of three numbers
n1=input("Enter number 1")
n2=input("Enter number 2")
n3=input("Enter number 3")
large=n1
if n1>=n2 and n1>=n3:
print 'Largest %s'%n1
elif n2>=n1 and n2>=n3:
print 'Largest %s'%n2
else:
print 'Largest %s'%n3
Example -4
#to check whether triangle can be formed and calculate area
print("Enter three sides ")
a=input()
b=input()
c=input()
if a<=0 or b<=0 or c<=0 or c>a+b or b>a+c or a>b+c:
print "Triangle can not be formed "
else:
print "Triangle can be formed"
if a==b==c:
print "Eqilateral triangle"
elif a==b or b==c or c==a:
print "Isoceles triangle"
else:
print "Scalene triangle"
s=(a+b+c)/2.0
area=(s*(s-a)*(s-b)*(s-c))**.5
print "Area = %.4f"%area
Example-5
#roots of a quadratic equation
print "Enter coefficients"
a=float(input("a="))
b=float(input("b="))
c=float(input("c="))
d=b*b-4*a*c
if d==0:
print "Roots are equal"
print "Root= %.4f"% -(b/(2*a))
elif d>0:
print "Roots are real and unequal"
r1=(-b+d**.5)/(2*a)
r2=(-b-d**.5)/(2*a)
print "Root 1= %.4f and Root 2= %.4f"%(r1,r2)
else:
print "Roots are imaginary"
real=-(b/(2*a))
imag=(-d)**.5/(2*a)
print "Root 1= %.4f+j%.4f and Root 2= %.4f-j%.4f"%(real,imag,real,imag)
Example - 6
# print odd numbers in reverse order and calculate sum and average
n=input("Enter n ")
i=1
e=n*2-1
sum=0
while i<=n:
print e
sum=sum+e
e=e-2
i=i+1
average=sum/float(n)
print "Sum = %.2f"%sum
print "Average %.2f"%average
Example - 7
#print even and odd numbers in a range and calculate sum and average
n1=input("Enter lower limit")
n2=input("Enter upper limit")
sum=0
count=0
print "Even numbers"
i=n1
while i<=n2:
if i%2==0:
sum=sum+i
count=count+1
print i
i=i+1
print "Sum = %.2f"%sum
Average=sum/float(count)
print "Average = %.2f"%Average
sum=0
count=0
print "Odd numbers"
for i in range(n1,n2+1):
if i%2==1:
sum=sum+i
count=count+1
print i
print "Sum = %.2f"%sum
Average=sum/float(count)
print "Average = %.2f"%Average
#largest of three numbers
n1=input("Enter number 1")
n2=input("Enter number 2")
n3=input("Enter number 3")
large=n1
if n1>=n2 and n1>=n3:
print 'Largest %s'%n1
elif n2>=n1 and n2>=n3:
print 'Largest %s'%n2
else:
print 'Largest %s'%n3
Example -4
#to check whether triangle can be formed and calculate area
print("Enter three sides ")
a=input()
b=input()
c=input()
if a<=0 or b<=0 or c<=0 or c>a+b or b>a+c or a>b+c:
print "Triangle can not be formed "
else:
print "Triangle can be formed"
if a==b==c:
print "Eqilateral triangle"
elif a==b or b==c or c==a:
print "Isoceles triangle"
else:
print "Scalene triangle"
s=(a+b+c)/2.0
area=(s*(s-a)*(s-b)*(s-c))**.5
print "Area = %.4f"%area
Example-5
#roots of a quadratic equation
print "Enter coefficients"
a=float(input("a="))
b=float(input("b="))
c=float(input("c="))
d=b*b-4*a*c
if d==0:
print "Roots are equal"
print "Root= %.4f"% -(b/(2*a))
elif d>0:
print "Roots are real and unequal"
r1=(-b+d**.5)/(2*a)
r2=(-b-d**.5)/(2*a)
print "Root 1= %.4f and Root 2= %.4f"%(r1,r2)
else:
print "Roots are imaginary"
real=-(b/(2*a))
imag=(-d)**.5/(2*a)
print "Root 1= %.4f+j%.4f and Root 2= %.4f-j%.4f"%(real,imag,real,imag)
Example - 6
# print odd numbers in reverse order and calculate sum and average
n=input("Enter n ")
i=1
e=n*2-1
sum=0
while i<=n:
print e
sum=sum+e
e=e-2
i=i+1
average=sum/float(n)
print "Sum = %.2f"%sum
print "Average %.2f"%average
Example - 7
#print even and odd numbers in a range and calculate sum and average
n1=input("Enter lower limit")
n2=input("Enter upper limit")
sum=0
count=0
print "Even numbers"
i=n1
while i<=n2:
if i%2==0:
sum=sum+i
count=count+1
print i
i=i+1
print "Sum = %.2f"%sum
Average=sum/float(count)
print "Average = %.2f"%Average
sum=0
count=0
print "Odd numbers"
for i in range(n1,n2+1):
if i%2==1:
sum=sum+i
count=count+1
print i
print "Sum = %.2f"%sum
Average=sum/float(count)
print "Average = %.2f"%Average
python programs
Example -1.
#wave number, wave length, frequency of spectral lines Balmer series of hydrogen atom
r=1.097e7
n1=2.
n2=input('Enter n2 > 2\n')
n2=float(n2)
wave_number=r*(1/(n1*n1)-1/(n2*n2))
print 'Wave number = ',wave_number
wave_length=1/(wave_number*1e-10)
print 'Wavelength ',wave_length
frequency=3e8/(wave_length*1e-10)
print 'Frequency',frequency
Example- 2.
#radius and energy of the electron in nth orbit of hydrogen atom
import math
e=1.6e-19
m=9.1e-31
h=6.6e-34
es0=8.85e-12
c=3e8
n=input('Enter quantum number =')
radius=n*n*h*h*es0/(math.pi*e*e*m)/1e-10
print 'Radius = ',radius,'A'
energy=-m*e**4/(8*es0*es0*n*n*h*h)/e
print 'Energy = ',energy,'eV'
#wave number, wave length, frequency of spectral lines Balmer series of hydrogen atom
r=1.097e7
n1=2.
n2=input('Enter n2 > 2\n')
n2=float(n2)
wave_number=r*(1/(n1*n1)-1/(n2*n2))
print 'Wave number = ',wave_number
wave_length=1/(wave_number*1e-10)
print 'Wavelength ',wave_length
frequency=3e8/(wave_length*1e-10)
print 'Frequency',frequency
Example- 2.
#radius and energy of the electron in nth orbit of hydrogen atom
import math
e=1.6e-19
m=9.1e-31
h=6.6e-34
es0=8.85e-12
c=3e8
n=input('Enter quantum number =')
radius=n*n*h*h*es0/(math.pi*e*e*m)/1e-10
print 'Radius = ',radius,'A'
energy=-m*e**4/(8*es0*es0*n*n*h*h)/e
print 'Energy = ',energy,'eV'
Friday, December 24, 2010
Introduction
Python is one of those rare languages which can claim to be both simple and powerful.
You will find that you will be pleasantly surprised on how easy it is to concentrate on the
solution to the problem rather than the syntax and structure of the language you are
programming in.
You will find that you will be pleasantly surprised on how easy it is to concentrate on the
solution to the problem rather than the syntax and structure of the language you are
programming in.
The official introduction to Python is:
Python is an easy to learn, powerful programming language. It has efficient
high-level data structures and a simple but effective approach to object-oriented
programming. Python's elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and rapid application
development in many areas on most platforms.
Note
Guido van Rossum, the creator of the Python language, named the language after the
BBC show "Monty Python's Flying Circus". He doesn't particularly like snakes that kill
animals for food by winding their long bodies around them and crushing them.
Python is an easy to learn, powerful programming language. It has efficient
high-level data structures and a simple but effective approach to object-oriented
programming. Python's elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and rapid application
development in many areas on most platforms.
Note
Guido van Rossum, the creator of the Python language, named the language after the
BBC show "Monty Python's Flying Circus". He doesn't particularly like snakes that kill
animals for food by winding their long bodies around them and crushing them.
Features of Python
Simple
Python is a simple and minimalistic language. Reading a good Python program feels
almost like reading English, although very strict English! This pseudo-code nature of
Python is one of its greatest strengths. It allows you to concentrate on the solution to
the problem rather than the language itself.
Easy to Learn
As you will see, Python is extremely easy to get started with. Python has an
extraordinarily simple syntax, as already mentioned.
Free and Open Source
Python is an example of a FLOSS (Free/Libré and Open Source Software). In simple
terms, you can freely distribute copies of this software, read its source code, make
changes to it, and use pieces of it in new free programs. FLOSS is based on the
concept of a community which shares knowledge. This is one of the reasons why
Python is so good - it has been created and is constantly improved by a community who
just want to see a better Python.
High-level Language
When you write programs in Python, you never need to bother about the low-level
details such as managing the memory used by your program, etc.
Portable
Due to its open-source nature, Python has been ported to (i.e. changed to make it work
on) many platforms. All your Python programs can work on any of these platforms
without requiring any changes at all if you are careful enough to avoid any system-dependent features.
You can use Python on Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga,
AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acorn RISC OS,
VxWorks, PlayStation, Sharp Zaurus, Windows CE and even PocketPC !
Interpreted
This requires a bit of explanation.
A program written in a compiled language like C or C++ is converted from the source
language i.e. C or C++ into a language that is spoken by your computer (binary code
i.e. 0s and 1s) using a compiler with various flags and options. When you run the
program, the linker/loader software copies the program from hard disk to memory and
starts running it.
Python, on the other hand, does not need compilation to binary. You just run the
program directly from the source code. Internally, Python converts the source code
into an intermediate form called bytecodes and then translates this into the native
language of your computer and then runs it. All this, actually, makes using Python
much easier since you don't have to worry about compiling the program, making sure
that the proper libraries are linked and loaded, etc, etc. This also makes your Python
programs much more portable, since you can just copy your Python program onto
another computer and it just works!
Object Oriented
Python supports procedure-oriented programming as well as object-oriented
programming. In procedure-oriented languages, the program is built around
procedures or functions which are nothing but reusable pieces of programs. In
object-oriented languages, the program is built around objects which combine data
and functionality. Python has a very powerful but simplistic way of doing OOP,
especially when compared to big languages like C++ or Java.
Extensible
If you need a critical piece of code to run very fast or want to have some piece of
algorithm not to be open, you can code that part of your program in C or C++ and
then use it from your Python program.
Embeddable
You can embed Python within your C/C++ programs to give 'scripting' capabilities for
your program's users.
Extensive Libraries
The Python Standard Library is huge indeed. It can help you do various things
involving regular expressions, documentation generation, unit testing, threading,
databases, web browsers, CGI, FTP, email, XML, XML-RPC, HTML, WAV files,
cryptography, GUI (graphical user interfaces), Tk, and other system-dependent stuff.
Remember, all this is always available wherever Python is installed. This is called the
'Batteries Included' philosophy of Python.
Besides, the standard library, there are various other high-quality libraries such as
wxPython (http:/ / www. wxpython. org) , Twisted (http:/ / www. twistedmatrix. com/
products/ twisted), Python Imaging Library (http:/ / www. pythonware. com/ products/
pil/ index. htm) and many more.
Python is indeed an exciting and powerful language. It has the right combination of
performance and features that make writing programs in Python both fun and easy.
Simple
Python is a simple and minimalistic language. Reading a good Python program feels
almost like reading English, although very strict English! This pseudo-code nature of
Python is one of its greatest strengths. It allows you to concentrate on the solution to
the problem rather than the language itself.
Easy to Learn
As you will see, Python is extremely easy to get started with. Python has an
extraordinarily simple syntax, as already mentioned.
Free and Open Source
Python is an example of a FLOSS (Free/Libré and Open Source Software). In simple
terms, you can freely distribute copies of this software, read its source code, make
changes to it, and use pieces of it in new free programs. FLOSS is based on the
concept of a community which shares knowledge. This is one of the reasons why
Python is so good - it has been created and is constantly improved by a community who
just want to see a better Python.
High-level Language
When you write programs in Python, you never need to bother about the low-level
details such as managing the memory used by your program, etc.
Portable
Due to its open-source nature, Python has been ported to (i.e. changed to make it work
on) many platforms. All your Python programs can work on any of these platforms
without requiring any changes at all if you are careful enough to avoid any system-dependent features.
You can use Python on Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga,
AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acorn RISC OS,
VxWorks, PlayStation, Sharp Zaurus, Windows CE and even PocketPC !
Interpreted
This requires a bit of explanation.
A program written in a compiled language like C or C++ is converted from the source
language i.e. C or C++ into a language that is spoken by your computer (binary code
i.e. 0s and 1s) using a compiler with various flags and options. When you run the
program, the linker/loader software copies the program from hard disk to memory and
starts running it.
Python, on the other hand, does not need compilation to binary. You just run the
program directly from the source code. Internally, Python converts the source code
into an intermediate form called bytecodes and then translates this into the native
language of your computer and then runs it. All this, actually, makes using Python
much easier since you don't have to worry about compiling the program, making sure
that the proper libraries are linked and loaded, etc, etc. This also makes your Python
programs much more portable, since you can just copy your Python program onto
another computer and it just works!
Object Oriented
Python supports procedure-oriented programming as well as object-oriented
programming. In procedure-oriented languages, the program is built around
procedures or functions which are nothing but reusable pieces of programs. In
object-oriented languages, the program is built around objects which combine data
and functionality. Python has a very powerful but simplistic way of doing OOP,
especially when compared to big languages like C++ or Java.
Extensible
If you need a critical piece of code to run very fast or want to have some piece of
algorithm not to be open, you can code that part of your program in C or C++ and
then use it from your Python program.
Embeddable
You can embed Python within your C/C++ programs to give 'scripting' capabilities for
your program's users.
Extensive Libraries
The Python Standard Library is huge indeed. It can help you do various things
involving regular expressions, documentation generation, unit testing, threading,
databases, web browsers, CGI, FTP, email, XML, XML-RPC, HTML, WAV files,
cryptography, GUI (graphical user interfaces), Tk, and other system-dependent stuff.
Remember, all this is always available wherever Python is installed. This is called the
'Batteries Included' philosophy of Python.
Besides, the standard library, there are various other high-quality libraries such as
wxPython (http:/ / www. wxpython. org) , Twisted (http:/ / www. twistedmatrix. com/
products/ twisted), Python Imaging Library (http:/ / www. pythonware. com/ products/
pil/ index. htm) and many more.
Python is indeed an exciting and powerful language. It has the right combination of
performance and features that make writing programs in Python both fun and easy.
From: A Byte of Python
v1.92 (for Python 3.0)
v1.92 (for Python 3.0)
Subscribe to:
Posts (Atom)