Code Explanation:
1. Import the json module
import json
The json module helps to convert Python objects into JSON strings (dumps) and back (loads).
2. Create a Python dictionary
data = {"a": 2, "b": 3}
A dictionary with two keys:
"a": 2
"b": 3
3. Convert dictionary to JSON string
js = json.dumps(data)
Converts the Python dictionary into a JSON-formatted string.
Now: js = '{"a": 2, "b": 3}'
4. Parse JSON back to Python dictionary
parsed = json.loads(js)
Converts JSON string back into a Python dictionary.
Now parsed = {"a": 2, "b": 3}
5. Add a new key-value pair with exponentiation
parsed["c"] = parsed["a"] ** parsed["b"]
"a" = 2, "b" = 3
Calculation: 2 ** 3 = 8
Now dictionary becomes: {"a": 2, "b": 3, "c": 8}
6. Print dictionary length and new value
print(len(parsed), parsed["c"])
len(parsed) = 3 (because keys are "a", "b", "c")
parsed["c"] = 8
Final Output:
3 8
.png)

0 Comments:
Post a Comment