0% found this document useful (0 votes)
6 views3 pages

Exercise Questions On Dictionary - III

The document contains 9 exercises involving dictionary operations in Python. The exercises cover defining functions to work with dictionaries, inverting keys and values, extracting subsets, merging dictionaries, and accessing nested dictionary values.

Uploaded by

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

Exercise Questions On Dictionary - III

The document contains 9 exercises involving dictionary operations in Python. The exercises cover defining functions to work with dictionaries, inverting keys and values, extracting subsets, merging dictionaries, and accessing nested dictionary values.

Uploaded by

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

Dictionary Exercise Set - 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.

inverts {key:value} pairs


passed dictionary: {"key1":"value1", "key2":"value2", "key3":"value3"}
should return : {“value1”: “key1”, “value2”:“key2”, “value3”:“key3” }

returns collective values as list_elements:


passed dictionary: {"key1":"value1", "key2":"value2", "key3":"value3"}
should return: {"key1":[value1,value2,value3], "key2":[value1,value2,value3],
"key3":[value1,value2,value3] }

returns collective values as list_elements, except own value:


passed dictionary: {"key1":"value1", "key2":"value2", "key3":"value3"}
should return : {"key1":[value2,value3], "key2":[value1,value3], "key3":[value1,value2] }

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"]

Expected Output: {'city': 'New york', 'age': 25}

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 } } },
}

1. Print the History marks of “Amey”


2. Print the sum of Physics and history marks of “Tejas”
3. Print the name of “student1” from “class2”
4. Print the Physics marks of “Suresh”
Exercise 9:
Initialize dictionary with default values
Given:
employees = [“Rutuja”, “Pranali”]
default_values = {"designation": 'Developer', "salary": 8000}
Expected Output:
{
“Rutuja”: {'designation': 'Developer', 'salary': 8000},
“Pranali”: {'designation': 'Developer', 'salary': 8000}
}

You might also like