You are on page 1of 23

Python Practical file

Submitted by-
Mohit Bhaskar (3422)
Hindu College
1. WAP to enter name and display as “Hello, Name”.
Program:
name= input("Enter name::", )
print("Hello",name)
Output:
2. Write a menu driven program to enter and print arithmetic operations
like +,-,*,/,//,%.
Program:
import os
print("CALCULATOR
MENU","\n1.Addition","\n2.Subtraction","\n3.Multiplication","\n4.Divisi
on","\n5.Greatest Integer(Floor Division)","\n6.Modulus","\n7.EXIT")
num1=int(input("Enter number 1::"))
num2=int(input("Enter number 2::"))
result=0
for choice in range(1,8):
choice=int(input("Enter Your Choice(1-7)::"))
os.system('cls')
if choice==1:
result=num1+num2
print("Addition of numbers is::",result)
elif choice==2:
if num1>=num2:
result=num1-num2
print("Substraction of numbers is::",result)
elif num2>num1:
result=num2-num1
print("Subtraction of numbers is::",result)
elif choice==3:
result=num1*num2
print("Multiplication of numbers is::",result)
elif choice==4:
result=num1/num2
print("Division of numbers is::",result)
elif choice==5:
result=num1//num2
print("Floor Division of numbers is::",result)
elif choice==6:
result=num1%num2
print("Modulus of numbers is::",result)
elif choice==7:
print("EXITING!!")
exit
else:
print("WRONG CHOICE ENTERED!!")
exit

Output:
3. WAP to compute the roots of a quadratic equation.
Program:
import os
import cmath
a=float(input("Enter the value of a::"))
b=float(input("Enter the value of b::"))
c=float(input("Enter the value of c::"))
d=(b**2)-(4*a*c)
x1=(-b-cmath.sqrt(d))/(2*a)
x2=(-b+cmath.sqrt(d))/(2*a)
print("The solutions are ",x1," and ",x2)
Output:
4. Write a menu driven program to reverse the entered numbers and
print the sum of digits entered.
Program:
import os
print("\nMENU","\n1.Reverse the Number","\n2.Print the sum of
digits","\n3.EXIT")
number=int(input("Enter The Number::"))
for choice in range(1,4):
choice=int(input("Enter your choice::"))
os.system('cls')
if choice==1:
rev=0
temp1=number
while(temp1>0):
rem1=temp1%10
rev=(rev*10)+rem1
temp1=temp1//10
print("\nReverse of entered number is::",rev)
elif choice==2:
s=0
temp2=number
while(temp2>0):
rem2=temp2%10
s=s+rem2
temp2=temp2//10
print("\nSum of all digits of entered number is::",s)
elif choice==3:
print("\nEXITING!!")
exit
else:
print("WRONG CHOICE ENTERED!!")
break
Output:
5. Write a menu driven program to enter the number and print whether
the number is odd/even or prime.
Program:
import os
print("\nMENU","\n1.ODD or EVEN","\n2.Prime","\n3.EXIT")
num=int(input("Enter number::"))
for choice in range(1,4):
choice=int(input("Enter choice(1-3)::"))
os.system('cls')
if choice==1:
temp1=num
if(temp1%2)==0:
print("Entered number is Even")
else:
print("Entered number is Odd")
elif choice==2:
temp2=num
if(temp2>1):
for i in range(2,temp2):
if(temp2%i)==0:
print(temp2,"is not a prime number")
print(i,"times",temp2//i,"is",temp2)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
elif choice==3:
print("EXITING!!")
break
exit
else:
print("WRONG CHOICE ENTERED!!")
break
exit
Output:
6. WAP to find maximum out of three numbers.
Program:

num1=float(input("Enter first number::"))

num2=float(input("Enter second number::"))

num3=float(input("Enter third number::"))

if(num1>num2)and(num1>num3):

largest=num1

elif(num2>num1)and(num2>num3):

largest=num2

else:

largest=num3

print("The largest number is",largest)

Output:
7. WAP to display ASCII code of a character and vice versa.
Program:
c1=input("Enter the character::")
print("The ASCII code for entered character is::",ord(c1))
c2=int(input("Enter ASCII value::"))
print("The character value for entered ASCII value is::",chr(c2))
Output:
8. WAP to check if the number if Armstrong or not.
Program:
num=int(input("Enter the number::"))
sum=0
temp=num
while(temp>0):
digit=temp%10
sum=sum+(digit**3)
temp=temp//10
if num==sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output:
9. WAP to find factorial of entered number using recursion.
Program:
def rfact(n):
if n==1:
return n
else:
return n*rfact(n-1)
num=int(input("Enter number::"))
if num<0:
print("FACTORIAL DOESN'T EXIST!!")
elif num==0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",rfact(num))
Output:
10. WAP to enter the number of terms and print Fibonacci series.
Program:
def rfib(n):
if n<=1:
return n
else:
return(rfib(n-1)+rfib(n-2))
nterms=int(input("Enter the number of terms to be print::"))
if nterms<=0:
print("INCORRECT INPUT!!")
else:
print("Fibonacci sequence::")
for i in range(nterms):
print(rfib(i))
Output:
11.WAP to enter the numbers and print greatest number using loop.
Program:
num=int(input("Enter the number of values::"))
a=[]
print("Enter numbers::")
for i in range(num):
value=int(input())
a.append(value)
largest=a[0]
for large in a:
if large>largest:
largest=large
print("The maximum value from the entered numbers is::",largest)
Output:
12.WAP to enter a string and check whether if it’s a palindrome or not
using loop.
Program:
def p(s):
x=""
for i in s:
x=i+x
if(x==s):
print("It's a palindrome")
else:
print("It's not a palindrome")
string=input("Enter the string::")
p(string)
Output:
13. WAP to enter 5 subjects numbers and print the grades A/B/C/D/E/F.
Program:
s1=int(input("Enter marks of the first subject::"))
s2=int(input("Enter marks of the second subject::"))
s3=int(input("Enter marks of the third subject::"))
s4=int(input("Enter marks of the fourth subject::"))
s5=int(input("Enter marks of the fifth subject::"))
avg=(s1+s2+s3+s4+s5)/5
if(avg>=90):
print("Grade: A")
elif((avg>=80)&(avg<90)):
print("Grade: B")
elif((avg>=70)&(avg<80)):
print("Grade: C")
elif((avg>=60)&(avg<70)):
print("Grade: D")
else:
print("Grade: F")
Output:
14.WAP to display the following pattern.
5
45
345
2345
12345

Program:
n=6
for i in range(1,n):
for j in range(n-1,i,-1):
print(" ",end=' ')
for k in range(n-i,n):
print(k,end=' ')
print()
Output:
15.Write a function in python to calculate the value of sin(x) using it’s
Taylor series expansion up to n terms.
Program:
import math
def taysin(x,n):
s=0
for i in range(n):
si=(-1)**i
pi=22/7
y=x*(pi/180)
s=s+((y**(2.0*i+1))/math.factorial(2*i+1))*si
return s
print("Taylor series expansion of sin(x)")
x=int(input("Enter value of x in degrees::"))
n=int(input("Enter the number of terms::"))
print(round(taysin(x,n),2))
Output:
16.WAP to show multiple matrices operations.
Program:
import os
r1=int(input("Enter the number of rows of matrix 1::"))
c1=int(input("Enter the number of columns of matrix 1::"))
matrix1=[]
matrix2=[]
result1=[]
result2=[]
result3=[]
print("Enter the values row wise(Matrix 1)::")
for i in range(r1):
a=[]
for j in range(c1):
a.append(int(input()))
matrix1.append(a)
r2=int(input("Enter the number of rows of matrix 2::"))
c2=int(input("Enter the number of columns of matrix 2::"))
print("Enter the values row wise(Matrix 2)::")
for i in range(r2):
b=[]
for j in range(c2):
b.append(int(input()))
matrix2.append(b)
os.system('cls')
print("MATRIX OPERATION
MENU","\n1.Addition","\n2.Substraction","\n3.Multiplication","\n4.EXIT
")
for choice in range(1,5):
choice=int(input("Enter your choice(1-4)::"))
if choice==1:
for i in range(r1):
result1.append([(matrix1[i][j]+matrix2[i][j]) for j in range(c1)])
print("Result Matrix after Addition::")
for r in result1:
print(r)
elif choice==2:
for i in range(r1):
result2.append([(matrix1[i][j]-matrix2[i][j]) for j in range(c1)])
print("Result Matrix after substraction::")
for r in result2:
print(r)
elif choice==3:
for i in range(r1):
result3.append([(matrix1[i][j]*matrix2[i][j]) for j in range(c1)])
print("Result Matrix after multiplication::")
for r in result3:
print(r)
elif choice==4:
print("EXITING!!")
break
exit
else:
print("WRONG CHOICE ENTERED!!")
break
exit
Output:
17.WAP to calculate distance between two points.
Program:
import math
x1=int(input("Enter the x-coordinate of first point::"))
y1=int(input("Enter the y-coordinate of first point::"))
x2=int(input("Enter the x-coordinate of second point::"))
y2=int(input("Enter the y-coordinate of second point::"))
d=math.sqrt(math.pow(x2-x1,2)+math.pow(y2-y1,2))
print("Distance between (",x1,",",y1,") and (",x2,",",y2,") is:",d)
Output:

You might also like