Python · Sets
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Creating Your First Set
Complete the code to create a set with three Codémon types:
# Create a set of Codémon types
codemon_types = ____'water', 'fire', 'grass'____
print(codemon_types, type(codemon_types))
Options: { } [ ] ( )
Hint : Sets are created using curly braces. Think about which bracket type defines a set.
Exercise 2 — Adding a Single Element
Add 'Rockachu' to the team:
team = {'Aquachu', 'Pyrochu'}
team.____('Rockachu')
print(team)
Options: add append insert update push
Hint : Think about the method specifically designed to add a single element to a set.
Exercise 3 — Combining Two Sets
Combine both teams into one set:
team_a = {'Aquachu', 'Pyrochu'}
team_b = {'Florachu', 'Zap'}
all_codemon = team_a ____ team_b
print(all_codemon)
Options: | & - ^ + *
Hint : Think about the operation that combines all elements from both sets without duplicates.
Exercise 4 — Converting List to Set
Convert the list to a set to remove duplicates:
caught = ['fire', 'water', 'fire', 'grass', 'water']
unique_types = ____(caught)
print(unique_types)
Options: set list dict tuple Set unique
Hint : Use the function that converts any iterable into a set structure.
Exercise 5 — Finding Common Elements
Find Codémon that appear in both regions:
region_1 = {'Aquachu', 'Pyrochu', 'Florachu'}
region_2 = {'Pyrochu', 'Zap', 'Florachu'}
common = region_1 ____ region_2
print(common)
Options: & | - ^ + *
Hint : Think about the operation that finds elements present in both sets.
Exercise 6 — Adding Multiple Elements
Add multiple Codémon from the list to your team:
team = {'Aquachu'}
new_catches = ['Pyrochu', 'Florachu', 'Zap']
team.____(new_catches)
print(team)
Options: update add extend append insert merge
Hint : Think about the method that adds multiple elements from an iterable to a set.
Exercise 7 — Removing an Element Safely
Remove 'Shadowchu' from the team, but don't cause an error if it's not there:
team = {'Aquachu', 'Pyrochu', 'Florachu'}
team.____('Shadowchu')
print(team)
Options: discard remove delete pop clear drop
Hint : Think about which method removes an element without raising an error if it doesn't exist.
Exercise 8 — Checking Membership
Check if 'Pyrochu' is in the active team:
active_team = {'Aquachu', 'Pyrochu', 'Florachu'}
if 'Pyrochu' ____ active_team:
print('Pyrochu is ready!')
Options: in is == contains exists has
Hint : Use the operator that checks if an element belongs to a collection.
Exercise 9 — Understanding Set Uniqueness
What will be the length of this set?
codemon = {'Aquachu', 'Pyrochu', 'Aquachu', 'Florachu', 'Pyrochu'}
print(len(codemon))
Options
• ```3```
• ```5```
• ```2```
• ```4```
Hint : Remember that sets automatically remove duplicates. Count the unique elements.
Exercise 10 — Set Difference Result
What will be the result of this operation?
set_a = {'Aquachu', 'Pyrochu', 'Florachu'}
set_b = {'Pyrochu', 'Zap'}
result = set_a - set_b
print(result)
Options
• ```{'Aquachu', 'Florachu'}```
• ```{'Pyrochu'}```
• ```{'Zap'}```
• ```{'Aquachu', 'Florachu', 'Zap'}```
Hint : The difference operation (A - B) returns elements in A that are not in B.
Exercise 11 — Understanding pop() Behavior
What happens when you call
pop()
on a set?
Options
• It removes and returns an arbitrary element
• It removes the first element added
• It removes the last element added
• It removes all elements
Hint : Remember that sets are unordered, so there's no concept of 'first' or 'last' element.
Exercise 12 — Identifying Disjoint Sets
Which statement will return
True
?
set1 = {'Aquachu', 'Pyrochu'}
set2 = {'Florachu', 'Rockachu'}
set3 = {'Pyrochu', 'Rockachu'}
Options
• ```set1.isdisjoint(set2)```
• ```set1.isdisjoint(set3)```
• ```set2.isdisjoint(set3)```
• None of them
Hint : Disjoint sets have no elements in common. Check which pairs share no Codémon.
Exercise 13 — Symmetric Difference Output
What will this code output?
team_a = {'Aquachu', 'Pyrochu', 'Florachu'}
team_b = {'Pyrochu', 'Zap', 'Rockachu'}
result = team_a ^ team_b
print(result)
Options
• ```{'Aquachu', 'Florachu', 'Zap', 'Rockachu'}```
• ```{'Pyrochu'}```
• ```{'Aquachu', 'Pyrochu', 'Florachu', 'Zap', 'Rockachu'}```
• ```{'Aquachu', 'Florachu'}```
Hint : Symmetric difference returns elements that are in either set, but not in both.
Exercise 14 — What Does clear() Do?
What will be the output of this code?
team = {'Aquachu', 'Pyrochu', 'Florachu'}
team.clear()
print(len(team))
Options
• ```0```
• ```3```
• ```1```
• ```None```
Hint : The clear() method removes all elements from a set, leaving it empty.
Exercise 15 — Subset Relationship
starter = {'Aquachu', 'Pyrochu', 'Florachu'}
team = {'Aquachu', 'Pyrochu', 'Florachu', 'Zap', 'Rockachu'}
Options
• ```starter.issubset(team)```
• ```team.issubset(starter)```
• ```starter.isdisjoint(team)```
• None of them
Hint : A set is a subset of another if all its elements are contained in the other set.
Exercise 16 — Method Behavior Difference
What's the main difference between
remove()
and
discard()
?
Options
• ```remove()``` raises an error if element doesn't exist, ```discard()``` doesn't
• ```discard()``` is faster than ```remove()```
• ```remove()``` can remove multiple elements, ```discard()``` only one
• There is no difference
Hint : Think about error handling when trying to remove an element that doesn't exist.
Exercise 17 — Match Operators with Operations
Match each operator with its corresponding set operation:
Items to match
• |
• &
• -
• ^
Definitions
• Union - combines all elements
• Intersection - elements in both sets
• Difference - elements in first but not second
• Symmetric difference - elements in either but not both
Exercise 18 — Match Methods with Actions
Match each method with what it does:
Items to match
• add()
• update()
• pop()
• clear()
• discard()
Definitions
• Adds a single element to the set
• Adds multiple elements from an iterable
• Removes and returns an arbitrary element
• Removes all elements from the set
• Removes element without error if missing
Exercise 19 — Match Comparison Methods
Match each method with its meaning:
Items to match
• issubset()
• issuperset()
• isdisjoint()
• in
Definitions
• All elements of this set are in the other
• This set contains all elements of the other
• Sets have no elements in common
• Checks if an element exists in the set
Exercise 20 — Match Operations to Results
Given
A = {'Aquachu', 'Pyrochu'}
and
B = {'Pyrochu', 'Florachu'}
, match each operation to its result:
Items to match
• A | B
• A & B
• A - B
• A ^ B
Definitions
• {'Aquachu', 'Pyrochu', 'Florachu'}
• {'Pyrochu'}
• {'Aquachu'}
• {'Aquachu', 'Florachu'}
Exercise 21 — Set vs List Characteristics
Match each characteristic with the correct data structure:
Items to match
• Automatically removes duplicates
• Elements are ordered
• Very fast membership testing
• Can access elements by index
Definitions
• Set
• List
• Set
• List
Exercise 22 — Complex Team Analysis
Complete this analysis to find Codémon that are in your team OR your friend's team, but NOT in both:
my_team = {'Aquachu', 'Pyrochu', 'Florachu', 'Zap'}
friend_team = {'Pyrochu', 'Rockachu', 'Shadowchu', 'Zap'}
# Codémon unique to each team (not shared)
unique_codemon = my_team ____ friend_team
print('Unique to each team:', unique_codemon)
Options: ^ | & - + *
Hint : Think about which operation finds elements that are in either set but not in both sets.
Exercise 23 — Tournament Eligibility Check
Check if your team has all the required types for the tournament:
team_types = {'water', 'fire', 'grass', 'electric', 'ice'}
required_types = {'water', 'fire', 'grass'}
if team_types.____(required_types):
print('Your team is eligible!')
else:
print('Missing required types')
Options: issuperset issubset isdisjoint contains includes hasall
Hint : Think about which method checks if one set contains all elements of another set.
Exercise 24 — Finding Missing Codémon
Find which Codémon you still need to catch to complete your collection:
all_starters = {'Aquachu', 'Pyrochu', 'Florachu', 'Rockachu', 'Zap', 'Shadowchu'}
caught = {'Aquachu', 'Pyrochu', 'Zap'}
# Find starters you haven't caught yet
missing = all_starters ____ caught
print('Still need to catch:', missing)
Options: - & | ^ + /
Hint : Think about which operation gives you elements in the first set that are not in the second set.
Exercise 25 — Battle Team Validation
Validate that both teams have no Codémon in common before starting the battle:
player1_team = {'Aquachu', 'Pyrochu', 'Florachu'}
player2_team = {'Zap', 'Rockachu', 'Shadowchu'}
if player1_team.____(player2_team):
print('Battle can start! Teams are valid.')
else:
print('Error: Teams share some Codémon!')
Options: isdisjoint issubset issuperset different notequal separate
Hint : Think about which method checks if two sets have no elements in common.
Back to the course