#----------------------------- 1- Creating a Dictionary ------------------------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
print(company)
#----------------------------- 2- Modifying a Dictionary -----------------------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
company["age"] = 26 # updating age to 26
print(company)
#----------------------------- 3- Adding and Dleting Dictionary ----------------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
#adding key-value pair
company["city"] = "Madurai"
print(company)
# Deleting a key-value pair
del company["grade"] #deleting the key- value pair with key = "grade"
print(company)
#----------------------------- 4- Iterating through Dictionary -----------------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
# Iterating through keys
print("\nKeys\n")
for key in company:
print(key)
# Iterating through values
print("\nValues\n")
for value in company.values():
print(value)
# Iterating through key-value pairs
print("\nKey Value\n")
for key, value in company.items():
print(key, value)
#----------------------------- 5- Checking existing key in Dictionary ----------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
# Checking if a key exists
if "name" in company:
print("Name Key is present in the dictionary")
#---------------------------- 6- Sorting Keys the Dictionary -------------------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
# Sorting a dictionary by keys
sortK = dict(sorted(company.items()))
print(sortK)
#--------------------------- 7- Concatination of Dictionary --------------------------
# Creating a dictionary
company = {"name": "WebGapp", "age": 15, "grade": "A"}
# Merging two dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
merged = {**dict1, **dict2, **company}
print(merged)
#-------------------------- 8- Nested Dictionary -------------------------------------
# Creating a nested dictionary
webgapp = {
"make": "WebGapp",
"Design": "HTML",
"year": 2018,
"Expert": {
"special": "Web development",
"course": "Python"
}
}
print(webgapp)