Back to articles
Task – Python Dictionary

Task – Python Dictionary

via Dev.to TutorialTanishka V

1.Create a dictionary named student with the following keys and values. and print the same "name": "Alice" "age": 21 "major": "Computer Science" CODE: student = { "name": "Alice", "age": 21, "major": "Computer Science" } print(student) OUTPUT: {'name': 'Alice', 'age': 21, 'major': 'Computer Science'} EXPLANATION: A dictionary stores data as key-value pairs Keys: name, age, major 2.Using the student dictionary, print the values associated with the keys "name" and "major". CODE: print(student["name"]) print(student["major"]) OUTPUT: Alice Computer Science EXPLANATION: Use keys to access values 3.Add a new key-value pair to the student dictionary: "gpa": 3.8. Then update the "age" to 22. CODE: student["gpa"] = 3.8 student["age"] = 22 print(student) OUTPUT: {'name': 'Alice', 'age': 22, 'major': 'Computer Science', 'gpa': 3.8} EXPLANATION: New key added Existing key updated 4.Remove the key "major" from the student dictionary using the del statement. Print the dictionary to confirm the remo

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
4 views

Related Articles