0% found this document useful (0 votes)
6 views10 pages

Info4 Chap7

Chapter 7 covers dictionaries, which are collections of unique key-value pairs that are ordered and changeable. It explains how to create, access, remove, and traverse dictionary items, as well as how to use them as function parameters. The chapter also includes exercises and answers related to dictionary operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Info4 Chap7

Chapter 7 covers dictionaries, which are collections of unique key-value pairs that are ordered and changeable. It explains how to create, access, remove, and traverse dictionary items, as well as how to use them as function parameters. The chapter also includes exercises and answers related to dictionary operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 7

Dictionaries

2024/2025

C.SAYAH
Definition and creation of a dictionary

What’s a dictionary?
• A dictionary is a collection of key-value pairs, where each key is unique, and it maps
to a specific value.
• Dictionaries are ordered and changeable, and don’t allow duplicate keys.
How to create a dictionary?
• To create a dictionary, use curly braces {}
• Each key is separated from its value using a colon :
• Key-value pairs are separated by commas ,

1
Definition and creation of a dictionary

How to access dictionary values?


• Use the key to get a value.

➢ If the key doesn’t exist, a keyError will be raised.

• To avoid errors if the key doesn’t exist, use the method get().

2
Removing dictionary items

❑ Use the function pop(key) to remove a key and returns its value.

❑ Use the function del dict[key] to delete a key.

❑ Use the function popitem() to remove and returns the last key-value pair.

❑ Use the function clear() to empty the dictionary : it returns an empty dict {}.

3
Traversing dictionary items

• You can loop through a dictionary by using a for loop, and there are three traversing methods
➢ Loop through keys

➢ Loop through values

➢ Loop through both keys and values

4
Dictionaries as function parameters

• Dictionaries can be used as function parameters :

➢ Passing a dictionary as an argument


You can pass a dictionary to a function just like other variable.

5
Dictionaries as function parameters

➢ Passing a dictionary as a kwargs (keyword arguments)


If you don’t know in advance how many key-value pairs will be passed to the function
use **kwargs.

6
Exercise

❖ What will be the output of the following code?


student = {“name”:“Emma”, “age”:22, “field”: “computer science”}
print(student[“grade”])
A. Emma
B. 22
C. Computer Science
D. KeyError

❖ You have the dictionary:


car = {"brand": "Toyota", "model": "Corolla", "year": 2020}
• Modify the year to 2023 and add a new key color with the value red.
Hint
• Use the function update({key:value}) to add new pair to the dictionary, or you can access
directly using dict[key] = value.
7
Answers

❖ What will be the output of the following code?


student = {“name”:“Emma”, “age”:22, “field”: “computer science”}
print(student[“grade”])
A. Emma
B. 22
C. Computer Science
D. KeyError ✅
❖ Updating the dictionary car

8
Thank you!
Do you have questions?

You might also like