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:
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:
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:
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:
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:
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:
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:
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:
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))3524Hint : 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){'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?
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'}set1.isdisjoint(set2)set1.isdisjoint(set3)set2.isdisjoint(set3)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){'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))031NoneHint : The clear() method removes all elements from a set, leaving it empty.
Exercise 15 — Subset Relationship
Which statement is True?
starter = {'Aquachu', 'Pyrochu', 'Florachu'}
team = {'Aquachu', 'Pyrochu', 'Florachu', 'Zap', 'Rockachu'}starter.issubset(team)team.issubset(starter)starter.isdisjoint(team)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()?
remove() raises an error if element doesn't exist, discard() doesn'tdiscard() is faster than remove()remove() can remove multiple elements, discard() only oneHint : 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:
|&-^Exercise 18 — Match Methods with Actions
Match each method with what it does:
add()update()pop()clear()discard()Exercise 19 — Match Comparison Methods
Match each method with its meaning:
issubset()issuperset()isdisjoint()inExercise 20 — Match Operations to Results
Given A = {'Aquachu', 'Pyrochu'} and B = {'Pyrochu', 'Florachu'}, match each operation to its result:
A | BA & BA - BA ^ B{'Aquachu', 'Pyrochu', 'Florachu'}{'Pyrochu'}{'Aquachu'}{'Aquachu', 'Florachu'}Exercise 21 — Set vs List Characteristics
Match each characteristic with the correct data structure:
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:
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:
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:
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:
Hint : Think about which method checks if two sets have no elements in common.