0% found this document useful (0 votes)
25 views6 pages

Python-Lab Assignment 5marks

The document contains a Python programming lab assignment with multiple questions focused on calculating factorials using different methods (for loop, while loop, recursion) and generating a bill from a menu based on customer orders. It includes functions for reading menu and order files, calculating totals with GST and tips, and generating a bill file. Additionally, it demonstrates dictionary operations such as creating, updating, finding minimum values, and sorting.

Uploaded by

naru2go999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

Python-Lab Assignment 5marks

The document contains a Python programming lab assignment with multiple questions focused on calculating factorials using different methods (for loop, while loop, recursion) and generating a bill from a menu based on customer orders. It includes functions for reading menu and order files, calculating totals with GST and tips, and generating a bill file. Additionally, it demonstrates dictionary operations such as creating, updating, finding minimum values, and sorting.

Uploaded by

naru2go999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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):
y.append(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 = line.strip().split(' : ')
menu[dish] = float(price)

order = {}
with open(order_file, 'r') as o:
for line in o:
dish, quantity = line.strip().split(' : ')
order[dish] = int(quantity)

total = 0
for dish, quantity in order.items():
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:
b.write(f"Customer: {cust_name}\n")
for dish, quantity in order.items():
b.write(f"{dish} x {quantity}: {menu[dish] * quantity}\n")
b.write(f"Total : {total:.2f}\n")
b.write(f"GST (18%) : {gst:.2f}\n")
b.write(f"Tip (5%) : {tip:.2f}\n")
b.write(f"Final Bill: {bill:.2f}\n")

print(f"Bill for {cust_name} has been generated in {output_file}.")

bill_calc('menu.txt', 'order.txt', '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
# order.txt

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'}
dict2.update(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]))

You might also like