Overview

This is a list of basic exercises to allow you to practice utilizing dictionaries and sets in the Python programming language.

Exercises 1: Dictionaries

Given the following table of schools and their team nickname:

School Nickname
ND Fighting Irish
UWEC Blugold
Stanford Cardinal
UCLA Bruin
Berkeley Bear

  1. Create a dictionary using the {} literal syntax.

  2. Test to see if 'ND' is in the dictionary.

  3. Test to see if 'USC' is in the dictionary.

  4. Print all the keys in the dictionary.

  5. Print all the values in the dictionary.

  6. Print all the items in the dictionary.

  7. Print all the items in the dictionary sorted by key.

  8. Write a function, count_schools, that returns the number of schools mentioned in a string.

    >>> count_schools('I was going to go to Stanford but decided to follow my heart and went to ND and then ended up at UWEC')
    

Solutions

Toggle Solutions

# 1. Create a dictionary using the {} literal syntax.
school_to_nickname = {
    'ND'       : 'Fighting Irish',
    'UWEC'     : 'Blugold',
    'Stanford' : 'Cardinal',
    'UCLA'     : 'Bruin',
    'Berkeley' : 'Bear',
}

# 2. Test to see if 'Notre Dame' is in the dictionary.

'ND' in school_to_nickname

# 3. Test to see if 'USC' is in the dictionary.

'USC' in school_to_nickname

# 4. Print all the keys in the dictionary.

for school in school_to_nickname:
    print school

# Alternative
print school_to_nickname.keys()

# 5. Print all the values in the dictionary.

for nickname in school_to_nickname.values():
    print nickname

# Alternative
print school_to_nickname.values()

# 6. Print all the items in the dictionary.

for school, nickname in school_to_nickname.items():
    print school, nickname

# Alternative
print school_to_nickname.items()

# 7. Print all the items in the dictionary sorted by key

for school, nickname in sorted(school_to_nickname.items()):
    print school, nickname

# 8. Write a function, `count_schools`, that returns the number of schools mentioned in a string.

def count_schools(text):
    count = 0
    for word in text.split():
        if word in school_to_nickname:
            count += 1
    return count

print count_schools('I was going to go to Stanford but decided to follow my heart and went to ND and then ended up at UWEC')

Exercises 2: Sets

Given the following sequence of numbers:

5, 4, 7, 0, 1, 4, 6, 6, 3, 7
  1. Create a set containing the numbers

  2. Add the following numbers to the set:

    9, 2, 8, 6, 7

  3. Print out all the numbers in the set

  4. Check if any of the following are in the set:

    7, 1, 5, 10

Solutions

Toggle Solutions

# 1. Create a set of the numbers

numbers = set([5, 4, 7, 0, 1, 4, 6, 6, 3, 7])

# 2. Add the following numbers to the set:

for number in 9, 2, 8, 6, 7:
    numbers.add(number)

# 3. Print out all the numbers in the set

for number in numbers:
    print number

# Alternatively
print numbers

# 4. Check if any of the following are in the set:
#
for number in 7, 1, 5, 10:
    if number in numbers:
        print '{} is in {}'.format(number, numbers)
    else:
        print '{} is not in {}'.format(number, numbers)