Conditional looping with a dictionary
Your friend at LLM Camp has provided you with a dictionary called courses
. It contains the names of courses as keys and their associated topic as a value. There are three topics: "AI"
, "Programming"
, and "Data Engineering"
.
courses = {"LLM Concepts": "AI",
"Introduction to Data Pipelines": "Data Engineering",
"AI Ethics": "AI",
"Introduction to dbt": "Data Engineering",
"Writing Efficient Python Code": "Programming",
"Introduction to Docker": "Programming"}
This is a great chance to practice looping through dictionaries!
This exercise is part of the course
Introduction to Python for Developers
Exercise instructions
- Create a for loop to iterate over
key
,value
incourses.items()
. - Check if the
value
is"AI"
, and print thekey
if so. - Else, check if the
value
is"Programming"
, printing thekey
if so. - Otherwise, print the
key
to confirm it is a"Data Engineering"
course.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Loop through the dictionary's keys and values
____ key, value ____ courses.items():
# Check if the value is "AI"
____ ____ ____ "____":
print(____, "is an AI course")
# Check if the value is "Programming"
____ ____ ____ "____":
print(____, "is a Programming course")
# Otherwise, print that it is a "Data Engineering" course
____:
print(____, "is a Data Engineering course")