Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 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))
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

Which statement is True?

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:

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.

Back to the course