Exercise Questions On Dictionary - III
Exercise Questions On Dictionary - III
Exercise 1:
Define a function accept_login(**users, username, password) with three parameters.
users a dictionary of username keys and password values
username a string containing login name
password a string containing password
The function should return True if the user exists and the password is correct; otherwise returns False.
Consider following two dictionaries and call above function as experimented follows.
dict1 = {“user1” : “password1”, “user2” : “password2”, “user3” : “password3”}
dict2 = {“user4” : “password4”, “user5” : “password5”, “user6” : “password6”}
Calling statements:
accept_login(dict1, “user2”, “password2”)
accept_login(dict1, “user11”, “password11”)
accept_login(dict2, “user2”, “password2”)
accept_login(dict2, “user11”, “password11”)
accept_login(dict1, “user1”, “123”)
Exercise 2:
Define a function that has variable-length keyword-arguments as parameter and this function must print
pyramid from following dictionary information:
dict1 = {“A”:5, “AB”:9, “XYZ”:3, “$”:5}
The keys indicate the content to be printed and the values indicates the numbers of rows to be printed.
Exercise 3:
Write individual functions to obtain following expected outputs. Every function accepts a dictionary as
parameter and returns resulting dictionary.
Exercise 4:
Create dictionary from following lists.
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
Expected Output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
Exercise 5:
Create a dictionary by extracting the keys from a given dictionary.
Sample dictionary:
dict1 = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"}
Keys to extract and create new dictionary:
keys = ["name", "salary"]
Exercise 6:
Merge two python dictionaries into one dictionary
Inputs:
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
Expected Output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
Exercise 7:
Delete a list of keys from a dictionary
Sample dictionary:
dict1 = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"
}
Keys to remove from existing dictionary:
keys = ["name", "salary"]
Exercise 8:
Consider following dictionary.
dict1 = {"class1": {"student1": {"name": "Tejas", "marks": {"physics": 70, "history": 80 } } ,
"student2": {"name": "Amey", "marks": {"physics": 57, "history": 55 } } },
"class2": {"student1": {"name": "Vishal", "marks": {"physics": 66, "history": 63 } },
"student2": {"name": "Suresh", "marks": {"physics": 67, "history": 45 } } },
}