Programming Problems
1. Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split()]
Answer: Program to read email ID’s
emailids = [] n = int(input("Enter How many students email id's you want to add : ")) for i in range(1, n+1): email = input("Enter email id of student" +str(i)+" : ") emailids.append(email) emailtuple = tuple(emailids) idlst = [] domain =[] for email in emailtuple: lst = email.split('@') idlst.append(lst[0]) domain.append(lst[1]) unametuple = tuple(idlst) domaintuple = tuple(domain) print("User name ==> ") print(unametuple) print("Domain name ==> ") print(domaintuple)
Output:
Enter How many students email id’s you want to add : 2
Enter email id of student1 : abc@gmail.com
Enter email id of student2 : xyz@yahoo.com
User name ==>
(‘abc’, ‘xyz’)
Domain name ==>
(‘gmail.com’, ‘yahoo.com’)
2. Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not.
We can accomplish these by:
(a) writing a user defined function
Answer: Writing a user defined function
def search_name(tname, name): if name in tname: return True else: return False n = int(input("How many students :")) namelst = [] for i in range(1, n+1): name = input("Enter Name : ") namelst.append(name) nametup = tuple(namelst) name = input("Enter name to search : ") if search_name(nametup, name): print("Found") else: print("Not Found")
(b) using the built-in function
Answer: Using the built-in function
n = int(input("How many students :")) namelst = [] for i in range(1, n+1): name = input("Enter Name : ") namelst.append(name) nametup = tuple(namelst) name = input("Enter name to search : ") if nametup.count(name)> 0: #to check occurrence of name print("Found") else: print("Not Found")
3. Write a Python program to find the highest 2 values in a dictionary.
Answer:
numdict = {5 : 555, 8:23, 9:254, 3:658, 7:658} val = sorted(numdict.values(), reverse = True) print("Highest two values are ", val[0], "and", val[1])
4. Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : ‘w3resource’
Expected output : {‘3’: 1, ‘s’: 1, ‘r’: 2, ‘u’: 1, ‘w’: 1, ‘c’: 1, ‘e’: 2, ‘o’: 1}
Answer:
string = ‘w3resource’ dict = {} index = 0 for char in string: if char not in dict: dict[char] = index index = index + 1 print("Dictionary :", dict)
Output is :
Dictionary : {‘w’: 0, ‘3’: 1, ‘r’: 2, ‘e’: 3, ‘s’: 4, ‘o’: 5, ‘u’: 6, ‘c’: 8}
5. Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names
Answer:
phonedict = {} n = int(input("How many friends details want to add : ")) for t in range(n): name = input("Enter Friends Name : ") phone = input("Enter Phone Number : ") if name not in phonedict: phonedict[name] = phone print("Friends Dictionary is : ", phonedict) while True: print("1. Display All Friends") print("2. Add Friend ") print("3. Delete a friend ") print("4. Modify phone number :") print("5. Search friend") print("6. Sorted on Names") print("7. Exit") choice = int(input("Enter your choice (1-7)")) if choice == 1: print("Name\t Phone Number") for key in phonedict: print(key, phonedict[key]) elif choice == 2: name = input("Enter Friends Name : ") phone = input("Enter Phone Number : ") if name not in phonedict: phonedict[name] = phone else: print("Name already exists") elif choice == 3: name = input("Enter Name to delete : ") if name in phonedict: del phonedict[name] else: print("No such name exist") elif choice == 4: name = input("Enter Name to delete : ") if name in phonedict: phone = input("Enter new phone number : ") phonedict[name] = phone else: print("No such name exist") elif choice == 5: name = input("Enter Name to Search : ") if name in phonedict: print(name, "\t", phonedict[name]) else: print("Friend does not exists") elif choice == 6: keys = sorted(phonedict) print("Sorted Phone Directory is ") for k in keys: print(k, "\t", phonedict[k])elif choice == 7:
break
else:
print("invalid choice, please input valid")