Brainstorming 07: Mastering Python Dictionaries

Easy Level (10 Questions)

  1. Create a dictionary to store the following: keys as 'name', 'age', and 'city' with values 'Alice', 20, and 'New York'.

  2. Write a program to add a new key-value pair 'country': 'USA' to the dictionary person = {'name': 'Alice', 'age': 20}.

  3. Given a dictionary student = {'name': 'John', 'class': '11th'}, write a program to retrieve and print the value associated with the key 'class'.

  4. Write a program to check if the key 'age' exists in a dictionary person = {'name': 'John', 'age': 18, 'city': 'Delhi'}.

  5. Write a program to delete the key 'city' from the dictionary person = {'name': 'John', 'age': 18, 'city': 'Delhi'}.

  6. Write a program to print only the keys of a dictionary car = {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}.

  7. Write a program to print only the values of a dictionary car = {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}.

  8. Given a dictionary marks = {'Math': 85, 'Science': 90, 'English': 75}, write a program to increase the value of each subject by 5.

  9. Write a program to update the dictionary car = {'brand': 'Ford', 'model': 'Mustang'} with the new key-value pair 'year': 1964.

  10. Write a program to clear all elements from a dictionary data = {'key1': 1, 'key2': 2, 'key3': 3}.


Medium Level (6 Questions)

  1. Write a program to count the occurrences of each character in the string "hello world" using a dictionary.

  2. Given two dictionaries dict1 = {'a': 100, 'b': 200} and dict2 = {'c': 300, 'd': 400}, write a program to merge them into a new dictionary.

  3. Write a program to find and print all keys in a dictionary that have even values. For example, data = {'x': 1, 'y': 2, 'z': 3, 'w': 4} should print ['y', 'w'].

  4. Write a program to calculate and print the sum of all values in a dictionary numbers = {'a': 5, 'b': 10, 'c': 15}.

  5. Given a dictionary inventory = {'apples': 5, 'bananas': 8, 'oranges': 12}, write a program to double the quantity of each fruit in the dictionary.

  6. Write a program to find the length of a dictionary colors = {'red': 1, 'blue': 2, 'green': 3, 'yellow': 4}.


Hard Level (4 Questions)

  1. Write a program to reverse the keys and values in a dictionary. For example, { 'a': 1, 'b': 2, 'c': 3 } should become { 1: 'a', 2: 'b', 3: 'c' }.

  2. Write a program to create a dictionary where each key is a number from 1 to 5, and the corresponding value is the cube of the key.

  3. Create a dictionary with student names as keys and marks as values. Write a program to find the name of the student with the highest marks.

  4. Given a dictionary of lists, grades = {'Math': [78, 88, 92], 'Physics': [90, 85, 87]}, write a program to calculate the average score for each subject and add it as a new key-value pair in the dictionary (e.g., {'Math': 86, 'Physics': 87.3}).
Previous Post Next Post