Skip to main content

Write a Python Program to perform Basic Operations Of DICTIONARY

Description

The provided Python code focuses on dictionary manipulation. It starts by initializing a dictionary named 'colleges' with key-value pairs, prints the original dictionary, adds, updates, and removes key-value pairs, creates a copy, and demonstrates that changes in one dictionary do not affect the other as they are separate copies.

Code

program7a.py
colleges={"name":"MLBP","code":"India","ID":"315"}
print(colleges)
colleges["location"]="Belagavi"
print(colleges)
colleges["location"]="B.Kudachi"
print(colleges)
colleges.pop("code")
print(colleges)
mycolleges=colleges.copy()
print(mycolleges)

Explanation of above code

  • Dictionary Initialization: The code commences by initializing a dictionary titled 'colleges.' This dictionary is populated with key-value pairs that encompass significant details about a college, including its name, location, and an identification number. Dictionaries in Python facilitate the storage of heterogeneous data types, making them a versatile choice for managing data.
  • Printing the Original Dictionary: Following the dictionary's initialization, the code harnesses the 'print' function, a fundamental Python function, to visually present the original contents of the 'colleges' dictionary. This step offers a clear depiction of the dictionary's initial state, replete with its key-value pairs, ensuring that the starting point is explicitly apparent.
  • Adding a New Key-Value Pair: The code transitions to a pivotal phase, introducing a novel key-value pair to the 'colleges' dictionary. Specifically, it appends the key 'location' with the value 'Belagavi' directly to the dictionary. This augmentation is executed via a straightforward assignment, further enriching the dictionary's data.
  • Printing the Dictionary with the Added Key-Value Pair: Subsequent to the integration of 'location: Belagavi,' the code propels forth by printing the modified 'colleges' dictionary. This visual representation serves to corroborate the incorporation of the new key-value pair, thereby elucidating the updated content.
  • Modifying an Existing Key's Value: The code proceeds to engage in the alteration of an extant key's associated value within the 'colleges' dictionary. In particular, the value linked with the key 'location' is transformed from 'Belagavi' to 'B.Kudachi.' This revision is effectuated via a direct value assignment operation, and it denotes a dynamic manipulation of the dictionary's contents.
  • Printing the Dictionary with the Modified Value: In the wake of the 'location' key's value modification, the code takes another stride forward, rendering the updated 'colleges' dictionary in print form. This presentation, imbued with the adjusted value for the 'location' key, articulates the effect of the transformation, ensuring a tangible representation of the dictionary's refined state.
  • Removing a Key-Value Pair: The code approaches a pivotal operation that involves the 'pop' method, an essential function for dictionaries in Python. Employing this method, it orchestrates the eradication of the key 'code' and its corresponding value from the 'colleges' dictionary. This deliberate excision effectively trims down the dictionary's size and structure.
  • Printing the Dictionary with the Removed Key-Value Pair: Subsequent to the methodical elimination of the 'code' key-value pair, the code proceeds to once again print the 'colleges' dictionary. This iteration of presentation serves as a validation of the successful removal of the 'code' key and its accompanying value, thereby affirming the dictionary's streamlined composition.
  • Creating a Copy of the Dictionary: The code takes a leap into the realm of data duplication, capitalizing on the 'copy' method to generate a replicated version of the 'colleges' dictionary. This duplicate is denoted by the identifier 'mycolleges' and serves as an autonomous copy, allowing for separate operations and manipulations.
  • Printing the Copied Dictionary: Finally, the code shines a spotlight on the 'mycolleges' dictionary, which stands as a distinct copy of 'colleges.' The act of printing this separate copy underscores a fundamental concept in Python: changes applied to one dictionary do not propagate to the other, as they are autonomous entities. This visual presentation emphasizes the autonomy of the copied dictionary and serves as a practical illustration of data replication in Python.

Learn more

Reference