- Create a dictionary named
studentwith the following keys and values. and print the same"name":"Alice""age":21"major":"Computer Science"
- Using the
studentdictionary, print the values associated with the keys"name"and"major". - Add a new key-value pair to the
studentdictionary:"gpa":3.8. Then update the"age"to22. - Remove the key
"major"from thestudentdictionary using thedelstatement. Print the dictionary to confirm the removal. - Check if the key
"age"exists in thestudentdictionary. PrintTrueorFalsebased on the result. - Create a dictionary
priceswith three items, e.g.,"apple": 0.5,"banana": 0.3,"orange": 0.7. Iterate over the dictionary and print each key-value pair. - Use the
len()function to find the number of key-value pairs in thepricesdictionary. Print the result. - Use the
get()method to access the"gpa"in thestudentdictionary. Try to access a non-existing key, e.g.,"graduation_year", with a default value of2025. - Create another dictionary
extra_infowith the following keys and values. Also mergeextra_infointo thestudentdictionary using theupdate()method."graduation_year":2025"hometown":"Springfield"
- Create a dictionary
squareswhere the keys are numbers from 1 to 5 and the values are the squares of the keys. Use dictionary comprehension. - Using the
pricesdictionary, print the keys and values as separate lists using thekeys()andvalues()methods. - Create a dictionary
schoolwith two nested dictionaries. Access and print the age of"student2"."student1":{"name": "Alice", "age": 21}"student2":{"name": "Bob", "age": 22}
- Use the
setdefault()method to add a new key"advisor"with the value"Dr. Smith"to thestudentdictionary if it does not exist. - Use the
pop()method to remove the"hometown"key from thestudentdictionary and store its value in a variable. Print the variable. - Use the
clear()method to remove all items from thepricesdictionary. Print the dictionary to confirm it’s empty. - Make a copy of the
studentdictionary using thecopy()method. Modify the copy by changing"name"to"Charlie". Print both dictionaries to see the differences. - Create two lists:
keys = ["name", "age", "major"]andvalues = ["Eve", 20, "Mathematics"]. Use thezip()function to create a dictionary from these lists. - Use the
items()method to iterate over thestudentdictionary and print each key-value pair. - Given a list of fruits:
["apple", "banana", "apple", "orange", "banana", "banana"], create a dictionaryfruit_countthat counts the occurrences of each fruit. - Use
collections.defaultdictto create a dictionaryword_countthat counts the number of occurrences of each word in a list:["hello", "world", "hello", "python"].