PYTHON PROGRAMMING LAB
ASSIGNMENT FOR 5 MARKS
#Question_1
#Append (factorial 1 to 5)
def fact_a(n):
y=[1]
for i in range(2,n+1):
[Link](y[-1]*i)
return y[-1]
print(fact_a(1))
print(fact_a(2))
print(fact_a(3))
print(fact_a(4))
print(fact_a(5))
output:
# Using For Loop
def fact_b(n):
res =1
for i in range(1,n+1):
res*=i
return res
print(fact_b(6))
print(fact_b(7))
print(fact_b(8))
print(fact_b(9))
print(fact_b(10))
output:
#Using While Loop
def fact_c(n): #
i=1
res = 1
while i<=n:
res*=i
i+=1
return res
print(fact_c(11))
print(fact_c(12))
print(fact_c(13))
print(fact_c(14))
print(fact_c(15))
output:
def fact_d(n): #using recursion
if n == 1:
return 1
else:
return n*fact_d(n-1)
print(fact_d(16))
print(fact_d(17))
print(fact_d(18))
print(fact_d(19))
print(fact_d(20))
output:
#USING ALL THE DEFINED FUNCTION IN ONE PROGRAMME
x=int(input('Enter the number '))
if 1<= x <=5:
print(fact_1(x))
elif 6<= x <=10:
print(fact_6(x))
elif 11<= x <= 15:
print(fact_11(x))
elif 16<= x <= 20:
print(fact_16(x))
else :
print('Enter number between 1 - 20 ')
output:
#Ques 2
def bill_calc(menu_file, order_file, cust_name):
menu = {}
with open(menu_file, 'r') as m:
for line in m:
dish, price = [Link]().split(' : ')
menu[dish] = float(price)
order = {}
with open(order_file, 'r') as o:
for line in o:
dish, quantity = [Link]().split(' : ')
order[dish] = int(quantity)
total = 0
for dish, quantity in [Link]():
if dish in menu:
total += menu[dish] * quantity
else:
print(f"{dish} is not in the menu.")
return
gst = 0.18 *total
tip = 0.05 *total
bill = total + gst + tip
output_file = f'bill_{cust_name}.txt'
with open(output_file, 'w') as b:
[Link](f"Customer: {cust_name}\n")
for dish, quantity in [Link]():
[Link](f"{dish} x {quantity}: {menu[dish] * quantity}\n")
[Link](f"Total : {total:.2f}\n")
[Link](f"GST (18%) : {gst:.2f}\n")
[Link](f"Tip (5%) : {tip:.2f}\n")
[Link](f"Final Bill: {bill:.2f}\n")
print(f"Bill for {cust_name} has been generated in {output_file}.")
bill_calc('[Link]', '[Link]', 'Narendra')
# Files for Question 2
# menu_card.txt
Dabeli : 45
Pizza : 250
Pasta : 100
Namkeen : 50
Sandwich : 35
french_Fries : 65
cold_drink : 40
Ice_Cream : 60
# [Link]
Dabeli : 3
Pizza : 1
Namkeen : 2
cold_drink : 2
Ice_Cream : 1
#outputs:
# Ques 3
keys = ['name', 'age', 'gender']
values = ['John', 25, 'Male']
dict_1 = dict(zip(keys, values))
# Joining Two Dictionaries
dict2 = {'name': 'John', 'age': 25}
dict3 = {'gender': 'Male', 'city': 'New York'}
[Link](dict2)
# Key With Minimum Value
my_dict = {'apple': 30, 'banana': 20, 'orange': 10}
min_key = min(my_dict, key=my_dict.get)
# Sorting
my_dict = {'banana': 20, 'apple': 30, 'orange': 10}
sorted_dict_by_value = dict(sorted(my_dict.items(), key=lambda item:
item[1]))