
Annachi kadai
Create and print a dictionary Concept: A dictionary stores data in key–value pairs. student = { " name " : " Alice " , " age " : 21 , " major " : " Computer Science " } print ( student ) Explanation: Each key is linked to a value. This helps organize related data. 2.Access specific values Concept: Access values using keys. print ( student [ " name " ]) print ( student [ " major " ]) Explanation: We use the key inside square brackets to get the value. 3.Add and update values Concept: Dictionaries are mutable. student [ " gpa " ] = 3.8 student [ " age " ] = 22 print ( student ) Explanation: New key-value pairs can be added, and existing ones can be updated. 4.Remove a key using del Concept: Delete elements. del student [ " major " ] print ( student ) Explanation: Removes the key and its value permanently. 5.Check if a key exists Concept: Use in operator. print ( " age " in student ) Explanation: Returns True if the key exists, otherwise False. 6.Iterate through a dictionary Concept: Loop
Continue reading on Dev.to Python
Opens in a new tab




