Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Section 3D: Collect and process data using dictionaries

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Which of the following is the correct way to create an empty dictionary in Python?

a)

`my_dict = []`

b)

`my_dict = {}`

c)

`my_dict = ()`

d)

`my_dict = set()`

2.

What will be the output of the following code? ```python my_dict = {'a': 1, 'b': 2} print(my_dict['b']) ```

a)

`'b'`

b)

`1`

c)

`2`

d)

`KeyError`

3.

Which method would you use to get a list of all the keys in a dictionary?

a)

`values()`

b)

`items()`

c)

`keys()`

d)

`get()`

4.

How do you add a new key-value pair to an existing dictionary called `d` with key `'x'` and value `10`?

a)

`d.add('x', 10)`

b)

`d['x'] = 10`

c)

`d.append('x', 10)`

d)

`d.insert('x', 10)`

5.

Which of the following statements will remove the key `'age'` from the dictionary `person`?

a)

`person.remove('age')`

b)

`del person['age']`

c)

`person.popitem('age')`

d)

`person.clear('age')`

6.

Given the dictionary `d = {'a': 1, 'b': 2, 'c': 3}`, which code will check if the key `'b'` exists in `d`?

a)

`'b' in d`

b)

`d.has_key('b')`

c)

`'b' == d`

d)

`d['b'] == None`

7.

Which method returns a view object that displays a list of a dictionary's key-value tuple pairs?

a)

`keys()`

b)

`values()`

c)

`items()`

d)

`pairs()`

8.

Suppose you have the dictionary `d = {'x': 5, 'y': 10}`. Which code will print all the values in the dictionary?

a)

`print(d.keys())`

b)

`print(d.items())`

c)

`print(d.values())`

d)

`print(d.all())`

9.

Given `d = {'a': 1, 'b': 2, 'c': 3}`, which code will iterate through all the keys and print them?

a)

`for key in d: print(key)`

b)

`for key in d.keys(): print(key)`

c)

Both A and B

d)

`for key in d.values(): print(key)`

10.

If you want to remove a key-value pair from a dictionary and get its value at the same time, which method should you use?

a)

`remove()`

b)

`pop()`

c)

`del`

d)

`clear()`

11.

You have a dictionary `grades = {'Alice': 85, 'Bob': 90, 'Charlie': 78}`. Write a code snippet to increase every student's grade by 5 points.

a)

```python for key in grades: grades[key] += 5 ```

b)

```python for value in grades: value += 5 ```

c)

```python grades.values() += 5 ```

d)

```python for key, value in grades.items(): key += 5 ```

12.

Given `d = {'a': 1, 'b': 2, 'c': 3}`, which code will print both keys and values?

a)

```python for key in d: print(key, d[key]) ```

b)

```python for key, value in d.items(): print(key, value) ```

c)

Both A and B

d)

```python for value in d.values(): print(value) ```

13.

Suppose you want to count the number of times each word appears in a list: `words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']`. Which of the following code snippets will correctly build a dictionary with word counts?

a)

```python counts = {} for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 ```

b)

```python counts = {} for word in words: counts[word] = 1 ```

c)

```python counts = {} for word in words: counts[word] += 1 ```

d)

```python counts = {} for word in words: counts.add(word) ```

14.

You have a dictionary `inventory = {'apples': 10, 'bananas': 5, 'oranges': 8}`. Write a code snippet to remove all items with a value less than 8.

a)

```python for key in inventory: if inventory[key] < 8: del inventory[key] ```

b)

```python for key in list(inventory.keys()): if inventory[key] < 8: del inventory[key] ```

c)

```python for key, value in inventory.items(): if value < 8: inventory.pop(key) ```

d)

```python inventory.clear() ```

15.

Given a dictionary `d = {'a': 1, 'b': 2, 'c': 3}` and a list `keys_to_remove = ['b', 'c', 'd']`, which code will remove only the keys that exist in `d`?

a)

```python for key in keys_to_remove: del d[key] ```

b)

```python for key in keys_to_remove: if key in d: del d[key] ```

c)

```python for key in d: if key in keys_to_remove: del d[key] ```

d)

```python d.clear() ```