student = {"name": "John", "age": 20, "grade": "A"}
for key, value in student.items():
print(f"{key}: {value}")
#source code --> clcoding.com
Code Explanation:
Define the Dictionary:
student = {"name": "John", "age": 20, "grade": "A"}
A dictionary named student is created with key-value pairs:
"name" → "John"
"age" → 20
"grade" → "A"
Loop Through the Dictionary:
for key, value in student.items():
The .items() method returns key-value pairs as tuples.
The for loop iterates through each key-value pair.
Print the Key-Value Pairs:
print(f"{key}: {value}")
The print() function formats and prints the key followed by its value.
Output:
name: John
age: 20
grade: A
0 Comments:
Post a Comment