student = {"name": "John", "age": 20, "grade": "A"}
for key, value in student.items():
print(f"Key: {key}, Value: {value}")
#source code --> clcoding.com
Code Explanation:
Define the Dictionary
student = {"name": "John", "age": 20, "grade": "A"}
We create a dictionary named student that contains:
"name" → "John"
"age" → 20
"grade" → "A"
Use a Loop to Iterate Through the Dictionary
for key, value in student.items():
.items() → This method returns pairs of keys and values from the dictionary.
The for loop assigns each key to key and each value to value on every iteration.
Print the Key-Value Pairs
print(f"Key: {key}, Value: {value}")
We use an f-string (f"...") to format and display the key and value.
Expected Output
Key: name, Value: John
Key: age, Value: 20
Key: grade, Value: A
0 Comments:
Post a Comment