Explanation of the Code:
-
Creating an Empty Dictionary:
-
This initializes an empty dictionary named personsAges.
-
-
Adding Key-Value Pairs:
-
Here, two key-value pairs are added to the dictionary:
"Smith" as the key with 21 as the value.
"Mike" as the key with 27 as the value.
-
-
Looping Through the Dictionary:
-
The .items() method retrieves key-value pairs from the dictionary.
-
The for loop iterates over these pairs.
key represents the names ("Smith", "Mike"), and value represents the ages (21, 27).
print(key) prints only the keys.
-
Expected Output:
Since only the key is printed, we get the names "Smith" and "Mike" as output.
0 Comments:
Post a Comment