Python Dictionaries and Sets

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

flashcard set

Earn XP

Description and Tags

Practice flashcards covering key concepts related to dictionaries and sets in Python.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

What is a dictionary in Python?

A collection of key-value pairs with unique keys.

2
New cards

What happens if you try to access a non-existent key in a dictionary?

Raises a KeyError.

3
New cards

How do you add a new key-value pair to a dictionary?

Use dictionary[key] = value.

4
New cards

Which method safely retrieves a value from a dictionary without raising an error if the key doesn’t exist?

dictionary.get(key, default).

5
New cards

What does the popitem() method do?

Removes the last added key-value pair.

6
New cards

Which operator merges two dictionaries into a new one?

| (pipe operator).

7
New cards

What is the output of {1: 'a', 2: 'b'}.values()?

['a', 'b'].

8
New cards

What is a set in Python?

A collection of unique, unordered elements.

9
New cards

How do you add an element to a set?

Use set.add(element).

10
New cards

What is the difference between remove() and discard() for sets?

remove() raises an error if the element doesn’t exist.

11
New cards

Which operator finds the intersection of two sets?

& (ampersand).

12
New cards

What does setA <= setB check?

If setA is a subset of setB.

13
New cards

What is the output of {x**2 for x in {1, 2, 3}}?

{1, 4, 9}.

14
New cards

Which method returns the symmetric difference of two sets?

set1.symmetric_difference(set2).

15
New cards

What does set() with no arguments create?

An empty set.