Dicts and Loops

To loop over a dictionary, use the in keyword. This will loop over the keys one at a time. The values can be accessed with the keys:
student = {"name": "Tom", "age": 17, "height": 68}

for key in student:
  print(f"{key}: {student[key]}")

# name: Tom
# age: 17
# height: 68
There are a few dictionary methods that provide more flexibility when looping over a dictionary.
.keys() returns a list of the keys:
for key in student.keys():
  print(key)

# name
# age
# height
.values() returns a list of the values:
for value in student.values():
  print(value)

# Tom
# 17
# 68
.items() returns both keys and their values, which can be accessed by using two loop variables separated by a comma:
for key, value in student.items():
  print(f"{key}: {value}")

# name: Tom
# age: 17
# height: 68

Exercise 1 of 5

Print the name of each student on a separate line. Do NOT use .keys().
scores = {"Alice": 98, "Bob": 77, "Charlie": 85}

Your code

Exercise 2 of 5

Print the name of each student on a separate line. Use .keys().
scores = {"Alice": 98, "Bob": 77, "Charlie": 85}

Your code

Exercise 3 of 5

Print the score of each student on a separate line. Use .values().
scores = {"Alice": 98, "Bob": 77, "Charlie": 85}

Your code

Exercise 4 of 5

Write the start of the loop to print the name and score of each student.
scores = {"Alice": 98, "Bob": 77, "Charlie": 85}
# start of loop here
  print(f"{name} scored {score}")

Your code

Exercise 5 of 5

Write the body of the loop to print the name and score of each student, like in the previous exercise.
scores = {"Alice": 98, "Bob": 77, "Charlie": 85}
for name in scores:
  # print name and score here

Your code