0% found this document useful (0 votes)
141 views

Python Programs

The document contains 24 Python programs with different programming concepts like functions, loops, conditional statements, etc. Each program has comments describing the aim and expected output. The programs cover concepts such as quadratic equations, swapping values, finding maximum/minimum, checking positive/negative/zero, even/odd, leap year, simple interest calculation, grading system, factorials, Fibonacci series, number tables, patterns, area calculation, temperature conversion, checking palindrome and a menu driven calculator.

Uploaded by

virat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Python Programs

The document contains 24 Python programs with different programming concepts like functions, loops, conditional statements, etc. Each program has comments describing the aim and expected output. The programs cover concepts such as quadratic equations, swapping values, finding maximum/minimum, checking positive/negative/zero, even/odd, leap year, simple interest calculation, grading system, factorials, Fibonacci series, number tables, patterns, area calculation, temperature conversion, checking palindrome and a menu driven calculator.

Uploaded by

virat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 79

Submitted to:

Mr.Sumit Babberwal

PROGRAM-1
AIM->To show the quadratic equation function.
a=input("enter the first coffecient")
b=input("enter the second coffectient")
c=input("enter the third coffecient")
print "a=",a
print "b=",b
print "c=",c
x=input("enter the value of variable")
d=a*x*x+b*x+c
print "the value of eqaution a*x*x+b*x+c =",d

OUTPUT::::-
PROGRAM-2
#AIM->TO SHOW THE SWAPPING OF TWO NUMBERS
a=input("enter the first value")
b=input("enter the second value")
print "a=",a
print "b=",b
a=a+b
b=a-b
a=a-b
print "value of a after swapping=",a
print "value of b after swapping=",b

OUTPUT:::::-
PROGRAM-3
AIM->To print the maximum and minimum among three numbers:

x= input('Enter the first number: ')

y=input('Enter the second number: ')


z=input('Enter the third number: ')

print "The three numbers you entered are:",x,',',y,'&',z

max=x

min=x

if (y>x)&(y>z):max=y

elif (z>x)&(z>y):max=z

elif (y<x)&(y<z):min=y

elif (z<x)&(z<y):min=z

print 'The maximum of these numbers is: ',max

print 'The minimum of these numbers is: ',min

OUTPUT::::-
PROGRAM-4
AIM->To check whether a number is positive negative
or zero
a=input("enter a number")

print "The number you entered is ",a

if(a>0):

print(a),

print ("is positive number")

elif(a<0):

print(a),

print("is negative number")

else:

print("is the number is 0")

OUTPUT:::::-
PROGRAM-5
AIM->To check whether a number is odd or even
a=input("enter a number")

print "The number you entered is=",a


if(a%2==0):

if(a<0):

print a,("is a negative even number")

else:

print a,("is an even number")

else:

if(a<0):

print a,("is a negative odd number")

else:

print a,("is an odd number")

OUTPUT::::-
{

PROGRAM-6
AIM->To check whether the year entered is leap year
or not
x=input("Enter the year: ")

if(x<=0):

print "the year you entered is invalid input"

elif(x%4==0):

print x,"->This is a leap year"

else:

print x,"->This is not a leap year"

OUTPUT::::-
PROGRAM-7
AIM->To calculate the simple interest and amount..
p=input("Enter the principal amount: ")
r=input("Enter the interest rate in percent: ")
t=input("Enter the time period(In years): ")
I=(p*r*t)/100.0
print "The amount payable after",t,"years at the
rate of",r,"% is"I
print "the amount payable is =",p+I

OUTPUT::::::-
PROGRAM-8
AIM->grade awarding
m1=input("enter rhe marks in subject 1(out of
100)")
m2=input("enter the marks in subject 2(out of
100)")
m3=input("enter the marks in subject 3(out of
100)")
print "marks in subject 1=",m1
print "marks in subject 2=",m2
print "marks in subject 3=",m3
avg=(m1+m2+m3)/3
print "average marks =",avg
marksgot=m1+m2+m3
print "total marks obtained=",marksgot
perc=(marksgot*100)/300.00
print "percentage of marks obtained=",perc
if(perc>=90):
print "grade of student is A1"
elif(perc<90 and perc>=80):
print "grade of student is A"
elif(perc<80 and perc>=70):
print "grade of student is B1"
elif(perc<70 and perc>=60):
print "grade of student is B"
elif(perc<60 and perc>=50):
print "grade of student is C1"
elif(perc<50 and perc>=40):
print "grade of student is C"
elif(perc<40 and perc>=33):
print "grade of student is D"
else:
print "grade of student is E"

OUTPUT:::::-
PROGRAM-9
AIM->To print the factorial of a number

Using for loop--- Using while loop-


x=input('Enter the --
number: ') i=input("enter the
fact=1 number whose factorial u
if x<0: want to find.." )
print 'Not a valid fact=1
input' while(i>=1):
else: fact=fact*i
for i in range(x,1,- i=i-1
1): print "the factorial
fact=fact*i is",fact
print 'The factorial
is,fact
OUTPUT::::-
PROGRAM-10
AIM->to print the Fibonacci series of n terms
x=input("How many elements do you want to print: ")
print "the Fibonacci series upto",x,"terms is: "
fib1=0
fib2=1
print fib1,"\n",fib2
i=2
while(i<x):
fib3=fib1+fib2
fib1=fib2
fib2=fib3
print fib2
i=i+1

OUTPUT:::::-
PROGRAM-11
AIM->To print the table of a given number.
n=input("enter the number whose table is to print")
table=1
i=1
print "the table of number u entered is as
follows",n
while(i<=10):
table=n*i
i=i+1
print(table)

OUTPUT:::::-
PROGRAM-12
AIM->To print the pattern(like a right angled-
triangle).
n=input("enter the number of rows")
s=raw_input("enter the symbol")
i=1
while(i<=n):
print(s*i)
i=i+1
OUTPUT:

PROGRAM-13
AIM->To print the pattern(inverted right angled
triangle)
n=input("enter the number of rows")
s=raw_input("enter the symbol")
i=1
while(n>=i):
print(s*n)
n=n-1
OUTPUT::::::-

PROGRAM-14
AIM->To print the pattern("triangle)
n=input("enter the number of rows")
s=raw_input("enter the symbol")
i=0
while(n>=1):
print ' '*i,s*n
n=n-1
i=i+1
OUTPUT::::-

PROGRAM-15
AIM->To print the pattern()
n=input("enter the number of rows")
s=raw_input("enter the symbol")
i=n-1
j=1
while(i<(n*2) and (i>0)):
print ' '*i,s*j
i=i-1
j=j+1
PROGRAM-16
AIM->To print the pattern(equilateral triangle)
n=input("enter the number of rows")
s=raw_input("enter the symbol")
i=n-1
j=1
while(i<(n*2) and (i>0)):
print ' '*i,s*j
i=i-1
j=j+2
OUTPUT::::-
PROGRAM-17
print("to print the table of numbers 10 to 1")
n=1
m=10
while(m>=n):

print(1*m),(2*m),(3*m),(4*m),(5*m),(6*m),(7*m),
(8*m),(9*m),(10*m)
m=m-1
OUTPUT:::::-

PROGRAM-18
AIM->To print the series 1+1/1!-
2/2!+3/3!........+n/n!
x=input("How many terms do you want to add: ")
sum=1.0
factorial=1
def fact(j):
fact=1
for j in range(j,1,-1):
fact=fact*j
return fact
for i in range (1,x):
sum=sum+((-1.0)**(i+1)*i)/fact(i)
print "The sum upto",x,"terms = ",sum

OUTPUT:::::-
PROGRAM-19
AIM->To check whether a number is prime or not.
x=input('Input the number: ')
count=0
if x<0:
print 'This is a negative number'
elif x==1:
print '1 is neither a prime nor a composite
number'
else:
for n in range (1,(x/2)+1):
if x%n==0:
count=count+1
if (count==1):
print 'This is a prime number'
elif (count>1):
print 'This is a not a prime number'
OUTPUT:::::-

PROGRAM-20
AIM->menu driven program of a calculator:
loop=1
while loop==1:
print "select from the options below"
print "1.)ADDITION"
print "2.)SUBTRACTION"
print "3.)MULTIPLICATION"
print "4.)DIVISION"
print "5.)EXIT"
i=input("enter your choice")
if(i==1):
a=input("enter the first number")
b=input("enter the second number")
print "sum of two numbers",a,"&",b,"=",a+b
elif(i==2):
a=input("enter the first number")

b=input("enter the second number")


if(a>b):
print "result when ",b,"is subtracted
from a is=",a-b
else:
print "result when ",a,"is
subtracted from b is=",b-a
elif(i==3):
a=input("enter the first number ")
b=input("enter the second number")
print "result when",a,"is multiplied
by",b,"we g0t",a*b
elif(i==4):
a=input("enter the first number ")
b=input("enter the second number")
if(a>b):
print "result when",a,"is divided
by",b,"is=",a/b
else:
print "result when",b,'is divided
by',a,"is=",b/a
elif(i==5):
loop=0
break
else:
print "not a valid input:"
break
PROGRAM-21
a=1

while a==1:

print "select from the options::::::::::::::::"

print'1 rectangle'

print "2 square"

print '3 triangle'

print "4.circle"

print "5 exit"

x=int(input("enter a choice"))

if(x==1):

l=input("enter the length")

b=input("enter the breadth")

area=l*b*1.0

print("area of rectangle"),area,'sq m'

elif(x==2):

s=input("enter the side of square")

area=s*s*1.0

print("area of square"),area,'sq m'

elif(x==3):

h=input("enter the height of triangle")

b=input("enter the base of triangle")

area=(1*h*b*1.0)/2

print("area of triangle"),area,'sq m'

elif(x==4):
r=input("enter the radius of circle")

area=3.14*r*r*1.0

print("area of circle"),area,'sq m'

elif(x==5):

a=0

else:

print "invalid input"

break

OUTPUT::::::-
PROGRAM-22
AIM->Temperature converter program
print "select from the options given below"
print "1.celsius to fahrenheit"
print "2.fahrenheit to celsius"
print "3.exit"
loop=1
while loop==1:
choice=input('enter the choice')
if(choice==1):
c= input('enter temp in celsius')
f=(9/5.0)*c+32
print c,'degree celsius=',f,'degree
fahrenheit'
elif (choice==2):
f= input('enter temp in fahrenheit')
c=(5*(f-32))/9.0
print f,'degree fahrenheit=',c,'degree
celsius'
elif(choice==3):
loop=0
else:
print 'not a valid input'
break
OUTPUT:::::-
PROGRAM-23
AIM->To check whether a number is palindrome or
not::::-
n=input("enter the number")
a=n
b=0
while(a!=0):
b=(a%10)+(b*10)
a=a/10
if(n==b):
print n,"is palindrome"
else:
print n,'is not palindrome'

OUTPUT:::::-
PROGRAM-24
AIM->To implement the temperature using functions:
loop=1
while(loop==1):
print 'Enter your choice'
print '1. celsius to fahrenheit conversion'
print '2. fahrenheit to celsius conversion'
print '3. exit'
n=input("enter your choice")
def cel():
f=(9/5.0)*x+32
print x,"degree celsius =",f,"degree
fahrenheit"
def fah():
c=(5*(x-32))/9.0
print x,"degree fahrenheit =",c,"degree
celsius "
if n==1:
x=input("Enter the temperature in degree
celsius: ")
print cel()
elif n==2:
x=input("Enter the temperature in degree
fahrenheit: ")
print fah()
else:
loop=0
OUTPUT::::::-

PROGRAM-25
AIM->To print the roman value of a number
x=input("Enter the number(1 to 3000): ")
def romandig(n,onechar,fivechar,tenchar):
if n==0: return""
elif n==1: return onechar
elif n==2: return onechar*2
elif n==3: return onechar*3
elif n==4: return onechar + fivechar
elif n==5: return fivechar
elif n==6: return fivechar + onechar
elif n==7: return fivechar + onechar*2
elif n==8: return fivechar + onechar*3
elif n==9: return onechar + tenchar
else: return

def romannum(n):
return romandig( n/1000,'M','','') +
romandig((n/100)%10,'C','D','M')+
romandig((n/10)%10,'X','L','C') +
romandig(n%10,'I','V','X')
if (x>=0 & x<=3000): num=romannum(x)
else: print "Out of Range: Enter numbers between 1-
3000"
print "The roman number for",x,"is: ",num
OUTPUT::::-

PROGRAM-26
AIM->Menu driven program of merging calculator,area
calculator and temperature converter
# to merge the calculator ,area calculator and
temperature converter....
a=1
while(a==1):
print 'select from the below.......'
print '1.calculator'
print '2.temperature converter'
print '3.area calculator'
print '4.exit'
c=input('enter the choice')
if(c==1):
loop=1
while(loop==1):
print 'selct the operation'
print '1.sum'
print '2.subtract'
print '3.multiply'
print '4.divide'
print '5.return to main menu'
s=input('select from the following
operation')
if(s==1):
b=int(input('enter the first
number'))
c=int(input('enter the second
number'))
print 'the sum of two numbers
is',b+c
elif(s==2):
b=int(input('enter the first
number'))
c=int(input('enter the second
number'))
print 'result when c is subtracted
from b',b-c
elif(s==3):
b=int(input('enter the first
number'))
c=int(input('enter the second
number'))
print 'multiplication of two
numbers is',b*c
elif(s==4):
b=float(input('enter the first
number'))
c=float(input('enter the second
number'))
print 'result when a is divided
from b is',b/c
elif(s==5):
print('return to the main menu')
else:
print'invalid selection'
loop=0

elif(c==2):
p=1
while(p==1):
print '1.celsius to fahrenheit'
print '2.fahrenheit to celsius'
print '3.return to main menu'
s=float(input('enter your choice'))
if(s==1):
c=float(input('enter the temp in
celsius'))
f=(9/5)*x+32
print c,'degree
celsius=',f,'degree fahrenheit'
elif(s==2):
f=float(input('enter the temp in
fahrenheit'))
c=(5*(x-32))/9
print f,'degree
fahrenheit',c,'degree celsius'
else:
print 'invalid selection'
p=0
elif(c==3):
l=1
while(l==1):
print 'selct from below'
print '1.square'
print '2.rectangle'
print '3.circle'
print '4.exit'
s=int(input('enter your choice'))
if(s==1):
a=input('enter the side of
square')
print 'area of square',a*a
elif(s==2):
a=input('enter the length of
rectangle')
b=input('enter the breadth of
rectangle')
print 'area of rectangle',a*b
elif(s==3):
a=float(input('enter the
radius of circle'))
print 'area of
circle=',3.14*a*a
else:
'invalid selection'
l=0
else:
print'invalid selection'
print'1.press 1 to continue'
print'2.exit please invalid input'
x=input('enter a choice')
if(x==1):
print' u can continue'
else:
print 'exit from here'
a=0

OUTPUT::::-
PROGRAM-27
AIM->exception handling(Try and except:)
x=5
y=0
try:
z=x/y
except ZeroDivisionError:
print "x=5","y=0","'''''zero division
error''''' will occur due to dividing by zero"

OUTPUT::::-

PROGRAM-28
AIM->Diamond pattern
x=input("How many rows do you want in kite
pattern(Odd number only): ")
k=(x+1)/2
for i in range(1,x+1):
j=abs(k-i)
if (i<=k):
print " "*j,"* "*i
elif (i>k):
print " "*j,"* "*(x+1-i)

OUTPUT::::-
PROGRAM-29
AIM->Factorial using recursive functions
def factorial(n):
if n==1:
return 1
else:
return n*factorial(n-1)
n=input("Enter the number: ")
b=factorial(n)
print "the factorial of ",n,"is",b
OUTPUT:::
PROGRAM-30
AIM->Fibonacci using recursive functions
#Fibonacci series using recursive functions
print "The following prints the N th fibonacci
series term"
def fibo(n):
if n==1:
return 0
elif n==2:
return 1
elif n>2:
return (fibo(n-1)+fibo(n-2))
x=input("Enter the N th terms: ")
b=fibo(x)
print b
OUTPUT::::-
PROGRAM-31
AIM->To count vowels in a sting
name=raw_input("enter a string")
vowel=['a','e','i','o','u','A','E','I','O','U']
count=0
for letter in name:
if( letter in vowel ):
count=count+1
print "the number of vowels present in
string",name,"=",count
OUTPUT::::-

PROGRAM-32
AIM->To remove symbols from a string
a=raw_input("enter a string with either of
following symbols
!,@,#,$,%,^,&,*,(,),_,+,{,},[,],|,\,),;,:")
b=("!,@,#,$,%,^,&,*,(,),_,+,{,},[,],|,\,),;,:")
for i in range(len(a)):
if(a[i] in b):
a=a.replace(a[i]," ")
print a

OUTPUT::::
PROGRAM-33
AIM->To print the reverse of a string.
def reverse(str1):
if str1=='':
return str1
else:
n=len(str1)
a=reverse(str1[1:n])+str1[0]
return a
str1=raw_input("enter a string")
print reverse(str1)

OUTPUT:
PROGRAM-34
AIM->To check whether a string is palindrome or
not
a=raw_input("Input the word: ")
l=len(a)
for i in range (0,l):
if a[i]==a[l-i-1]:
print "this is palindrome"
break
else:
print"This is not a Palindrome."
break
OUTPUT:

PROGRAM-35
AIM->Implement string functions
a=raw_input("enter a string")
b=raw_input("enter a word")
print "the string you entered is",a
c=a+b
print "the string after concatenation=",c
g=raw_input("enter a word from string which you
want to replace")
d=raw_input("enter a new word which you want to
place in place of repalced word")
s=c.replace(g,d)
print "the replaced string is",s
print s.upper()

OUTPUT:
PROGRAM-36
AIM->Matrix addition
m=input("Enter the number of rows in 1st matrix: ")
n=input("Enter the number of columns in 1st matrix:
")
p=input("Enter the number of rows in 2nd matrix: ")
q=input("Enter the number of columns in 2nd matrix:
")
if (m!=p | n!=q):
print "Order of the two matrices is not same.
Thus can't be added"
elif (m==p & n==q):
matrix_1=[]
matrix_2=[]
addition=[]
print "Enter the first matrix"
for i in range (0,m):
matrix_1.append([])
print "Enter the elements of
the",i+1,"row:"
for j in range (0,n):
matrix_1[i].append(input(""))
print "Enter the second matrix"
for i in range (0,p):
matrix_2.append([])
print "Enter the elements of
the",i+1,"row:"
for j in range (0,q):
matrix_2[i].append(input(""))
for i in range(len(matrix_1)):
addition.append([])
for j in range(len(matrix_1[0])):
addition[i].append(matrix_1[i][j] +
matrix_2[i][j])
for x in addition: print(x)

OUTPUT::::
PROGRAM-37
AIM->Class implementation
class ComplexCompute(object):
def __init__(self, realPart, imagPart):
self.realPart = realPart
self.imagPart = imagPart
def __add__(self, other):
r1=self.realPart
i1=self.imagPart
r2=other.realPart
i2=self.imagPart
resultR=r1+r2
resultI=i1+i2
result=complex(resultR, resultI)
return result
def __sub__(self, other):
r1=self.realPart
i1=self.imagPart
r2=other.realPart
i2=self.imagPart
resultR=r1-r2
resultI=i1-i2
result=complex(resultR, resultI)
return result
def __mul__(self, other):
r1=self.realPart
i1=self.imagPart
r2=other.realPart
i2=self.imagPart
resultR=(r1*r2-i1*i2)
resultI=(r1*i2+r2*i1)
result=complex(resultR, resultI)
return result
def main():
c1 = ComplexCompute(2,4)
c2 = ComplexCompute(1,6)
print c1+c2
print c1-c2
print c1*c2
main()
OUTPUT::::-
PROGRAM-38
AIM->class of a bankaccount
class bankaccount:
def _init_(self):
bal=0
def bal(self):
self.bal=0
def deposit(self,amt):
self.bal=self.bal+amt
def withdraw(self,amt):
self.bal=self.bal-amt
def getbal(self):
return(self.bal)
def main():
act1=bankaccount()
act2=bankaccount()
act1.bal()
act2.bal()
loop=1
while loop==1:
print "select from options"
print "1.deposit"
print "2.withdraw"
print "3.transfer"
print "4.exit"
i=input("enter your choice")
if(i==1):
amt=input("Enter the amount to be
deposited in account1")
act1.deposit(amt)
print "amount deposited from account
1=",act1.getbal()
elif(i==2):
amt1=input("enter the amount to be
withdraw from account 1")
act1.deposit(amt)
act1.withdraw(amt1)
print "amount withdrawn from account
1=",amt1
print "amount left in account
1=",act1.getbal()
elif(i==3):
amt=input("enter the amount to be
dposited in account 1")
amt1=input("enter the amount to be
withdraw from account 1")
a=act1.deposit(amt)
b=act1.withdraw(amt1)
print "amount withdrawn from account
1=",amt1
act2.deposit(amt1)
print "balance left in account
1=",act1.getbal()
print "money transferred in account
2=",act2.getbal()
elif(i==4):
loop=0
break
else:
print "invalid choice ----------------
>>>>>>>>>>>>>try again"
main()
main()

OUTPUT::::-
PROGRAM-39
AIM->Stack implementation...
#Stacks
class stack:
def __init__(self):
self.values=list()
self.top=0
self.bottom=0
def pushing(self,element):
if self.bottom==0:
self.top=self.top+1
self.values.insert(self.top,element)
def isempty(self):
if self.top==0:
return True
else:
return False
def popping(self):
if not(self.isempty()):
val=self.values[self.top]
del self.values[self.top]
if(self.top)==(self.bottom):
self.top=0
self.bottom=0
else:
self.top=self.top-1
else:
print " Underflow"
return None
def topel(self):
if not(self.isempty()):
return self.values[self.top]
else:
return None

def main():
while True:
print "Enter your Choice: "
print "1: For deletion"
print "2: For inserting an element in the
stack"
print "3: For removing an element fron the
stack"
print "4: For displaying the top element"
ch=input("")
s=stack()
if ch==1:
elem=raw_input("enter the element
first")
s.pushing(elem)
print "the element in stack is=",elem
print "do u want to delete the complete
stack"
a=raw_input("enter your choice(y/n)")
if (a!='y' and a!='Y'):
print "invalid choice"
break
else:
del s
print "the stack is deleted"

elif ch==2:
elem=raw_input("Enter the element: ")
s.pushing(elem)
elif ch==3:
print s.popping()
elif ch==4:
print s.topel()
else:
print "Invalid Choice. Try Again"
choice=raw_input("Press 'Y' to continue: ")
if (choice!='Y' and choice!='y'):
break
main()

OUTPUT:::::-

You might also like