Practical File of Programming Ith Python Chetna Rani
Practical File of Programming Ith Python Chetna Rani
QUESTIONS:
QUES1. WAP to calculate total marks, percentage and grade of a student.
Marks obtained in each of three subjects are to be input by the user. Assign
grades according to the following criteria: Grade A: if Percentage >= 80 Grade
B: if Percentage >= 60 and Percentage < 80 Grade C: if Percentage >= 40 and
Percentage < 60 Grade D: if Percentage < 40
ANS.
print("enter the marks of five subjects:")
subject_1=float(input())
subject_2=float(input())
subject_3=float(input())
subject_4=float(input())
subject_5=float(input())
total=subject_1+subject_2+subject_3+subject_4+subject_5
average=total/5
percentage=(total/500)*100
if average>=90:
grade='A'
elif average>=80 and average<90:
grade='B'
elif average>=70 and average<80:
grade='C'
elif average >=60 and average<70:
grade='D'
else:
grade='E'
print("the total marks is:",total,"/500.00")
print("the average marks is:",average)
print("the percentage is :",percentage,"%")
print("the grade is :",grade)
OUTPUT:
ANS.
C def sum_of_n_natural_numbers(num):
if(num==0):
return num
else:
return(num+sum_of_n_natural_numbers(num-1))
number= int(input("please enter any number:"))
total_value=sum_of_n_natural_numbers(number)
print("sum of natural numbers from 1 to {0}={1}".format(number,total_value))
QUES 4. WAP to print the following conversion table (use looping constructs)
ANS.
def print_conversion_table():
print("Height (in feet)\tHeight (in Inches)")
print("-" * 35)
Height_in_feet= 5.0
while Height_in_feet<=6.0:
Height_in_Inches = Height_in_feet*12
print(f"{Height_in_feet:.1f}ft\t\t{Height_in_Inches:.1f} Inches")
Height_in_feet+=0.1
print_conversion_table()
Ques 5:WAP that takes a positive integer n and the produce n lines of output as
shown:
*
**
***
* * * * (sample output for n = 4)
ANS.
Ques6: Write a menu driven program using user defined functions to print the area of rectangle,
square, circle and triangle by accepting suitable input from user.
ANS
import math
def area_rectangle():
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
area = length * breadth
print(f"The area of the rectangle is: {area}")
def area_square():
side = float(input("Enter the side of the square: "))
area = side ** 2
print(f"The area of the square is: {area}")
def area_circle():
radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius ** 2
print(f"The area of the circle is: {area:.2f}")
def area_triangle():
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print(f"The area of the triangle is: {area}")
def main():
while True:
print("\nMenu:")
print("1. Area of Rectangle")
print("2. Area of Square")
print("3. Area of Circle")
print("4. Area of Triangle")
print("5. Exit")
if choice == '1':
area_rectangle()
elif choice == '2':
area_square()
elif choice == '3':
area_circle()
elif choice == '4':
area_triangle()
elif choice == '5':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
# Example usage:
number = int(input("Enter a number: "))
print(f"The factorial of {number} is: {factorial_iterative(number)}")
Ques 8:WAP to print the series and its sum: (use functions) 1/1! + 1/2! + 1/3!
…….1/n!
ANS. import math
def factorial(num):
"""Calculate the factorial of a number."""
return math.factorial(num)
def series_sum(n):
"""Calculate the series sum."""
total = 0
for i in range(1, n + 1):
total += 1 / factorial(i)
return total
def print_series_and_sum(n):
"""Print the series and its sum."""
series = " + ".join([f"1/{i}!" for i in range(1, n + 1)])
sum_of_series = series_sum(n)
print(f"The series is: {series}")
print(f"The sum of the series is: {sum_of_series:.5f}")
# Append
even_list.append(100)
odd_list.append(101)
print("\nAfter appending 100 to Even List and 101 to Odd List:")
print("Even Numbers List:", even_list)
print("Odd Numbers List:", odd_list)
# Extend
even_list.extend([102, 104])
odd_list.extend([103, 105])
print("\nAfter extending Even List with [102, 104] and Odd List with [103,
105]:")
print("Even Numbers List:", even_list)
print("Odd Numbers List:", odd_list)
# Sort
even_list.sort()
odd_list.sort()
print("\nAfter sorting:")
print("Even Numbers List:", even_list)
print("Odd Numbers List:", odd_list)
# Index
print("\nIndex of 100 in Even List:", even_list.index(100))
print("Index of 101 in Odd List:", odd_list.index(101))
# Remove
even_list.remove(100)
odd_list.remove(101)
print("\nAfter removing 100 from Even List and 101 from Odd List:")
print("Even Numbers List:", even_list)
print("Odd Numbers List:", odd_list)
# Reverse
even_list.reverse()
odd_list.reverse()
print("\nAfter reversing the lists:")
print("Even Numbers List:", even_list)
print("Odd Numbers List:", odd_list)
# Main function
def main():
# Input numbers from the user
n = int(input("Enter the number of elements: "))
numbers = []
print("Enter the numbers:")
for _ in range(n):
numbers.append(int(input()))
# Main function
def main():
cube_dict = create_cube_dictionary()
print("The dictionary with keys as numbers from 1 to 5 and values as their
cubes:")
print(cube_dict)