PYTHON PROGRAMMING
PRACTICAL FILE
SUBMITTED TO: BY: KALPANA
DR JAGVINDER ROLL NUMBER: 25236761021
SEM:2
MOR
Q1. Write a program to enter name and display as “Hello, Name”.
a=input("enter the name")
print("Hello ",a)
Q2. Write a menu driven program to enter two numbers and print the arithmetic
operations like a. + b. – c. * d. / e. // f. %.
print("1. sum")
print("2. subtract")
print("3. multiply")
print("4. divide")
print("5. floor")
print("6. remainder")
num=int(input("enter your choice from 1/2/3/4/5/6"))
a=int(input("enter first number"))
b=int(input("enter second number"))
if num==1:
print("sum = ", a+b)
elif num==2:
print("subtract = ", a-b)
elif num==3:
print("multiply = ", a*b)
elif num==4:
print("divide = ", a/b)
elif num==5:
print("floor = ", a//b)
elif num==6:
print("remainder = ", a%b)
else:
print("error")
Q3. Write a program to compute the roots of a quadratic equation.
import math
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
d = b*b - 4*a*c # discriminant
if d > 0:
root1 = (-b + [Link](d)) / (2*a)
root2 = (-b - [Link](d)) / (2*a)
print("Roots are real and different")
print("Root 1:", root1)
print("Root 2:", root2)
elif d == 0:
root = -b / (2*a)
print("Roots are real and same")
print("Root:", root)
else:
real = -b / (2*a)
imag = [Link](-d) / (2*a)
print("Roots are complex")
print("Root 1:", real, "+", imag, "i")
print("Root 2:", real, "-", imag, "i")
Q4. Write a menu driven program to reverse the entered numbers and print the sum of
digits entered.
while True:
print("\nMENU")
print("1. Reverse the number")
print("2. Sum of digits")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
num = int(input("Enter a number: "))
rev = 0
temp = num
while temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp = temp // 10
print("Reversed number:", rev)
elif choice == 2:
num = int(input("Enter a number: "))
sum_digits = 0
temp = num
while temp > 0:
digit = temp % 10
sum_digits += digit
temp = temp // 10
print("Sum of digits:", sum_digits)
elif choice == 3:
print("Exiting program")
break
else:
print("Invalid choice! Try again.")
Q5. Write a menu driven program to enter the number and print whether the number is
odd or even prime.
while True:
print("\nMENU")
print("1. Check Odd or Even")
print("2. Check Prime")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
elif choice == 2:
num = int(input("Enter a number: "))
if num <= 1:
print(num, "is not Prime")
else:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "is Prime")
else:
print(num, "is not Prime")
elif choice == 3:
print("Exiting program")
break
else:
print("Invalid choice! Try again.")
Q6. Write a program to find maximum out of entered 3 numbers.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Maximum number is:", a)
elif b >= a and b >= c:
print("Maximum number is:", b)
else:
print("Maximum number is:", c)
OR
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b:
if a > c:
print("Maximum number is:", a)
else:
print("Maximum number is:", c)
else:
if b > c:
print("Maximum number is:", b)
else:
print("Maximum number is:", c)
Q7. Write a program to display ASCII code of a character and vice versa.
print("1. ASCII to Character")
print("2. Character to ASCII")
while True:
a=int(input("enter your choice"))
if a==1:
num=int(input("enter a ascii character"))
print("the character is ", chr(num))
elif a==2:
character=input("enter a character")
print("the ascii number is", ord(character))
else:
print("Invalid Choice")
choice=input("do you want to continue yes or no")
if [Link]()!='y':
break
Q8. Write a program to check if the entered number is Armstrong or not.
num1=int(input("enter a number"))
temp=num1
sum=0
digits = len(str(num1))
while temp>0:
digit=temp%10
sum = sum + digit**digits
temp=temp//10
if sum==num1:
print(num1,"is a armstrong number")
else:
print(num1,"is not a armstrong number")
Q9. Write a program to find factorial of the entered number using recursion.
while True:
a=int(input("enter a number"))
def factorial(n):
if n==1 or n==0:
return 1
else:
return n* factorial(n-1)
if a<0:
print("factorial does not exist for negative numbers")
else:
print("factorial of ",a," is ", factorial(a))
choice=input("do you want to continue yes or no")
if [Link]()!='y':
break
Q10. Write a program to enter the number of terms and to print the Fibonacci Series.
while True:
a=int(input("enter a number"))
def fibonacci(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fibonacci(n-1)+ fibonacci(n-2)
if a<0:
print("fibonacci does not exist for negative numbers")
else:
print("fibonacci series :", end=" ")
for i in range(a):
print(fibonacci(i), end=" ")
choice=input("do you want to continue yes or no")
if [Link]()!='y':
break
Q11. Write a program to enter the numbers and to print greatest number using loop.
while True:
numbers=[]
a=int(input("enter the number "))
for i in range(a):
num=int(input("enter the numbers"))
i=i+1
[Link](num)
greatest=numbers[0]
for num in numbers:
if num > greatest:
greatest=num
print(greatest)
choice=input("do you want to continue yes or no")
if [Link]()!='y':
break
Q12. Write a program to enter the string and to check if it’s palindrome or not using loop.
s = input("Enter a string: ")
i = len(s) - 1
rev = ""
while i >= 0:
rev += s[i]
i -= 1
if s == rev:
print("The string is Palindrome")
else:
print("The string is Not a Palindrome")
Q13. Write a program to enter the 5 subjects’ numbers and print the grades A/B/C/D/E.
total = 0
for i in range(1, 6):
marks = int(input(f"Enter marks of subject {i}: "))
total += marks
average = total / 5
if average >= 90:
grade = "A"
elif average >= 75:
grade = "B"
elif average >= 60:
grade = "C"
elif average >= 45:
grade = "D"
else:
grade = "E"
print("Total Marks:", total)
print("Average Marks:", average)
print("Grade:", grade)
Q14. Write a program in python language to display the given pattern:
n=5
for i in range(n, 0, -1):
# print spaces
for space in range(i - 1):
print(" ", end="")
# print numbers
for num in range(i, n + 1):
print(num, end=" ")
print()
Q15. Write a python function sin (x, n) to calculate the value of sin(x) using its Taylor series
expansion up to n terms.
def sin(x, n):
sum = 0
sign = 1
for i in range(n):
power = 2 * i + 1
fact = 1
# factorial calculation
for j in range(1, power + 1):
fact = fact * j
sum = sum + sign * (x ** power) / fact
sign = -sign
return sum
# Main program
x = float(input("Enter value of x (in radians): "))
n = int(input("Enter number of terms: "))
result = sin(x, n)
print("sin(x) =", result)
Q16. Write a program to determine EOQ using various inventory models.
while True:
import math
print("eoq models")
print("1. basic eoq model ")
print("2. eoq with finite production,cost constant, no shortages")
print("3. eoq with shortages allowed,infinite production,cost constant")
choice=int(input("enter a choice 1/2/3"))
match choice:
case 1:
print("constant demand, no shortages,production rate is infinite")
d=float(input("enter demand"))
a=float(input("enter ordering cost"))
i=float(input("enter holding cost or inventory carrying cost per year"))
eoq=[Link]((2*d*a)/i)
print("eoq is ", round(eoq,2))
case 2:
print("constant demand, no shortages,production rate is finite")
d=float(input("enter demand"))
a=float(input("enter ordering cost"))
i=float(input("enter holding cost or inventory carrying cost per year"))
p=float(input("enter production rate"))
t=int(input("enter your choice"))
eoq=[Link]((2*d*a)/(i*(1-d/p)))
print("eoq is ", round(eoq,2))
case 3:
print("constant demand, shortages allowed,production rate is infinite")
d=float(input("enter demand"))
a=float(input("enter ordering cost"))
i=float(input("enter holding cost or inventory carrying cost per year"))
s=float(input("enter shortages"))
eoq=[Link]((2*d*a*(i+s))/(i*s))
print("eoq is ", round(eoq,2))
case _:
print("invalid choice ! please enter 1/2/3")
choice=input("do you want to continue yes or no")
if [Link]()!='y':
break