Lab Task 9: Programming Exercises
Lab Task 9: Programming Exercises
PROGRAMMING EXERCISES:
Question 1.
Design a dictionary of your family. Once you get the printout update family dictionary with
your grandparents (maternal and paternal) including uncles and aunts (maternal and
paternal).
Program:
family={"Father":{"Name":"Ikhlaq Hussain","Age":56},
"Mother":{"Name":"Aqeela Narjis","Age":46},
"Brother":{"Name":"Taha Hussain","Age":22}}
ques=int(input("Enter how many members do you want to add?? "))
for m in range(ques):
mem=input("Enter Relation: ")
name=input("Enter Name: ")
age=int(input("Enter Age: "))
family[mem] = {"Name": name, "Age": age}
print("\tThe Family Tree")
for x in family:
print(x)
for y in family[x]:
print(y,"=",family[x][y])
Result:
Enter how many members do you want to add?? 6
Enter Relation: Maternal Grandfather
Enter Name: Akmal Hussain
Enter Age: 80
Enter Relation: Maternal Grandmother
Enter Name: Qamarunnisa
Enter Age: 72
Enter Relation: Paternal Grandfather
Enter Name: Mohammad Hussain
Enter Age: 82
Enter Relation: Paternal Grandmother
Enter Name: Saima Hussain
Enter Age: 73
Enter Relation: Maternal Aunt
Enter Name: Ismat Narjis
Enter Age: 30
Enter Relation: Maternal Uncle
Enter Name: Mubin Abbas
Enter Age: 48
The Family Tree
Father
Name = Ikhlaq Hussain
Age = 56
Mother
Name = Aqeela Narjis
Age = 46
Brother
Name = Taha Hussain
Age = 22
Maternal Grandfather
Name = Akmal Hussain
Age = 80
Maternal Grandmother
Name = Qamarunnisa
Age = 72
Paternal Grandfather
Name = Mohammad Hussain
Age = 82
Paternal Grandmother
Name = Saima Hussain
Age = 73
Maternal Aunt
Name = Ismat Narjis
Age = 30
Maternal Uncle
Name = Mubin Abbas
Age = 48
Question 2
Write a function to design a personal phone directory of your parents and friends. You must add
12 members. Then make a function to delete a member from a telephone directory. Print
total number of members in your personal phone directory.
Program:
def make_phone_directory(Range):
phone_directory={}
for x in range(Range):
name=input("Enter Name: ")
pnum=input("Enter Phone Number: ")
phone_directory[name]=pnum
return phone_directory
def delete_member(name):
p1.pop(name)
return p1
r=int(input("Enter number of contacts you want to add "))
p1=make_phone_directory(r)
ques=input("Do you want to delete any member(Yes/No)? ").casefold()
if ques=="yes":
delete_member(input("Enter name: "))
print("Total contacts:",len(p1))
print("\t\tThe Phone Directory")
for x, y in p1.items():
print(x, "=", y)
Result:
Enter number of contacts you want to add 12
Enter Name: aqeela
Enter Phone Number: 03032132713
Enter Name: taha
Enter Phone Number: 03442496574
Enter Name: darakhshan
Enter Phone Number: 03032453678
Enter Name: maha
Enter Phone Number: 03456783290
Enter Name: maria
Enter Phone Number: 03215678342
Enter Name: abiha
Enter Phone Number: 03256784532
Enter Name: adeena
Enter Phone Number: 03014562379
Enter Name: laiba
Enter Phone Number: 03412772365
Enter Name: hiba
Enter Phone Number: 03364527924
Enter Name: shaheera
Enter Phone Number: 03452646758
Enter Name: kinza
Enter Phone Number: 03412883452
Enter Name: amna
Enter Phone Number: 03456723187
Do you want to delete any member(Yes/No)? yes
Enter name: shaheera
Total contacts: 11
The Phone Directory
aqeela = 03032132713
taha = 03442496574
darakhshan = 03032453678
maha = 03456783290
maria = 03215678342
abiha = 03256784532
adeena = 03014562379
laiba = 03412772365
hiba = 03364527924
kinza = 03412883452
amna = 03456723187
Question 3.
Write a function hexASCII() that prints the correspondence between the lowercase
characters in the alphabet and the hexadecimal representation of their ASCII code.
Program:
def hexascii():
alpha="abcdefghijklmnopqrstuvwxyz"
for i in alpha:
print(i,"=",hex(ord(i)),end= "\t")
hexascii()
Result:
a = 0x61 b = 0x62 c = 0x63 d = 0x64 e = 0x65 f = 0x66
g = 0x67 h = 0x68 i = 0x69 j = 0x6a k = 0x6b l = 0x6c
m = 0x6d n = 0x6e o = 0x6f p = 0x70 q = 0x71 r = 0x72
s = 0x73 t = 0x74 u = 0x75 v = 0x76 w = 0x77 x = 0x78
y = 0x79 z = 0x7a
Question 4.
Create double dictionaries one of which is your choice of dishes. Other one is dishes cooked in a
week. Compare them and find how many dishes you will get of your choice to be cooked in next
week. Print the name of those dishes as well.
Program:
choices={"Dishes choice":{"Spicy Food":["Biryani","Nihari"],"Fast
Food":["Pasta","Pizza","Burger"]},
"Dishes cooked":{"Spicy Food":["Biryani","Chicken Karahi"],"Fast
Food":["Pizza","Pasta"]}}
d={}
for x,y in choices["Dishes choice"].items():
for i,j in choices["Dishes cooked"].items():
if x==i:
dish=[]
for a in y:
if a in j:
dish.append(a)
d[x]=dish
count=0
print("The dishes are:")
for m,n in d.items():
print(m.title())
for k in range(len(n)):
print(str(int(k+1))+str("."),n[k])
count+=1
print("You can eat dishes of your choice",count,"times!!")
Result:
The dishes are:
Spicy Food
1. Biryani
Fast Food
1. Pasta
2. Pizza
You can eat dishes of your choice 3 times!!
Question 5.
Design a list of guests with family members on your sister wedding. Each family members must
be counted. Your parents have made a list of guests and you have made another list. At the end
compare both the list and find the common guests which both of you have invited and count
them once. The program will return the number of guest with members and total number of
guest. Use functions to perform the required actions.
Program:
def wed_lst(g_d):
for x in g_d["my list"]:
for y in g_d["parent's list"]:
if x==y:
g_d["parent's list"].remove(y)
new_lst=g_d["my list"]+g_d["parent's list"]
return new_lst
guest_list={"my list":["ismat","amna","aliya","saad","mubin","ahmed","sara","maria"],
"parent's
list":["amna","ismat","amber","aabira","mubin","sarim","sumbul","adeena"]}
new=wed_lst(guest_list)
print("The total invited members are:",len(new))
for i in range(len(new)):
print(str(int(i+1))+str("."),new[i].title())
Result:
The total invited members are: 13
1. Ismat
2. Amna
3. Aliya
4. Saad
5. Mubin
6. Ahmed
7. Sara
8. Maria
9. Amber
10. Aabira
11. Sarim
12. Sumbul
13. Adeena