Python · Sets
Set Methods
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Checking Membership with 'in'
The
in
operator allows you to check if an element exists in a set. It returns
True
if the element is found,
False
otherwise.
water_types = {'Aquachu', 'Hydro', 'Wave'}
print('Aquachu' in water_types)
print('Pyrochu' in water_types)
print('Hydro' in water_types)
The
in
operator is very fast with sets because sets use hash tables internally. This makes membership testing much more efficient than with lists.
Sets vs Lists for Membership Testing
When you need to frequently check if elements exist in a collection, sets are much faster than lists. With lists, Python has to check each element one by one. With sets, it can find elements almost instantly.
Exercise 2 — What Does 'in' Return?
Given the following code, what will be printed?
fire_types = {'Pyrochu', 'Flame', 'Blaze'}
result = 'Aquachu' in fire_types
print(result)
Options
• ```False```
• ```True```
• ```None```
• ```'Aquachu'```
Hint : The ```in``` operator returns a boolean value: ```True``` if the element is in the set, ```False``` if it's not.
Exercise 3 — Check if Codémon Can Battle
Complete the code to check if a Codémon is in the team and can battle.
team = {'Aquachu', 'Pyrochu', 'Florachu'}
codemon = 'Aquachu'
if codemon ____ team:
print(f'{codemon} is ready to battle!')
else:
print(f'{codemon} is not in your team')
Options: in not in == is
Hint : Use the ```in``` operator to check if an element exists in a set.
Exercise 4 — Understanding Disjoint Sets
Two sets are disjoint if they have no elements in common. The
isdisjoint()
method returns
True
if the sets share no elements,
False
otherwise.
water_types = {'Aquachu', 'Hydrochu', 'Wavechu'}
fire_types = {'Pyrochu', 'Flamechu', 'Blazechu'}
mixed_types = {'Aquachu', 'Pyrochu', 'Grasschu'}
print(water_types.isdisjoint(fire_types)) # Water and fire types are completely different
print(water_types.isdisjoint(mixed_types)) # Water and mixed have Aquachu in common
Disjoint sets are useful when you want to ensure two collections have nothing in common, like checking if a team has duplicate Codémon types or if two players have completely different teams.
Empty Sets Are Disjoint
An empty set is disjoint with any other set (including another empty set) because it has no elements to share. For example:
set().isdisjoint({1, 2, 3})
returns
True
.
Exercise 5 — Are These Sets Disjoint?
Which pair of sets is disjoint (shares no common elements)?
Options
• ```set1 = {'Aquachu', 'Hydro'}```
```set2 = {'Pyrochu', 'Flame'}```
• ```set1 = {'Aquachu', 'Pyrochu'}```
```set2 = {'Pyrochu', 'Flame'}```
• ```set1 = {'Aquachu', 'Hydro', 'Wave'}```
```set2 = {'Aquachu', 'Pyrochu'}```
• ```set1 = {'Pyrochu', 'Flame'}```
```set2 = {'Flame', 'Blaze'}```
Hint : Check each pair carefully to see if they share any elements. Disjoint sets have absolutely no elements in common.
Exercise 6 — Check If Teams Are Different
Complete the code to check if two teams have completely different Codémon (no duplicates).
team1 = {'Aquachu', 'Pyrochu', 'Florachu'}
team2 = {'Zap', 'Rockachu'}
if team1.____(team2):
print('Teams have no common Codémon!')
else:
print('Teams share some Codémon')
Options: isdisjoint issubset issuperset intersection difference
Hint : Use ```isdisjoint()``` to check if two sets have no elements in common.
Exercise 7 — Understanding Subsets and Supersets
A set A is a subset of set B if all elements of A are in B. Conversely, B is a superset of A. Python provides
issubset()
and
issuperset()
methods to check these relationships.
main_types = {'water', 'fire', 'grass', 'electric', 'rock'}
starter_types = {'water', 'fire', 'grass'}
rare_types = {'dragon', 'psychic'}
print(starter_types.issubset(main_types)) # Starter types are a subset of all types
print(main_types.issuperset(starter_types)) # All types is a superset of starter types
print(rare_types.issubset(main_types)) # Rare types are not a subset of all types
Important: A set is always a subset and superset of itself. For example,
{1, 2}.issubset({1, 2})
returns
True
.
Subset Operators
You can also use operators for subset checking:
-
<=
for subset (same as
issubset()
)
-
<
for proper subset (subset but not equal)
-
>=
for superset (same as
issuperset()
)
-
>
for proper superset (superset but not equal)
Exercise 8 — Identifying Subset Relationships
Given the following sets, which statement is
True
?
setA = {'Aquachu', 'Pyrochu'}
setB = {'Aquachu', 'Pyrochu', 'Florachu'}
setC = {'Zap', 'Rockachu'}
Options
• ```setA.issubset(setB)```
• ```setB.issubset(setA)```
• ```setC.issubset(setB)```
• ```setA.issubset(setC)```
Hint : For setA to be a subset of setB, all elements of setA must be in setB. Check which option satisfies this condition.
Exercise 9 — Match Methods with Descriptions
Match each set method with its correct description:
Items to match
• in
• isdisjoint()
• issubset()
• issuperset()
Definitions
• Checks if an element exists in a set
• Checks if two sets have no common elements
• Checks if all elements are in another set
• Checks if a set contains all elements of another set
Exercise 10 — Check If Team Has All Required Types
Complete the code to check if a team has all the required Codémon types for a tournament.
team_types = {'water', 'fire', 'grass', 'electric'}
required_types = {'water', 'fire', 'grass'}
if team_types.____(required_types):
print('Your team is eligible for the tournament!')
else:
print('Your team is missing some required types')
Options: issuperset issubset isdisjoint union intersection
Hint : Think about which set should contain all elements of the other. The team should have all required types, so team_types should be a superset of required_types.
Exercise 11 — Understanding Set Relationships
What will be the output of the following code?
set1 = {'Aquachu'}
set2 = {'Aquachu', 'Pyrochu'}
set3 = {'Florachu'}
print(set1.issubset(set2) and set3.isdisjoint(set2))
Options
• ```True```
• ```False```
• ```None```
• An error will occur
Hint : Evaluate each part separately: Is set1 a subset of set2? Are set3 and set2 disjoint? Then combine with ```and```.
Exercise 12 — Validate Tournament Team
Complete the code to check if a team is valid for a tournament. The team must:
1. Have all required types
2. Not have any banned types
team = {'water', 'fire', 'grass', 'electric'}
required = {'water', 'fire'}
banned = {'dark', 'ghost'}
has_required = team.____(required)
no_banned = team.____(banned)
if has_required and no_banned:
print('Team is valid!')
else:
print('Team is invalid')
Options: issuperset issubset isdisjoint intersection union difference
Hint : Think about which method checks if one set contains all elements of another, and which checks if two sets share no elements.
Exercise 13 — Tournament Registration System
You're managing tournament registrations and need to validate teams!
1. Check if 'Aquachu' is in team_a
2. Check if 'Shadowchu' is in team_b
3. Check if team_a and team_b have no Codémon in common
4. Check if team_a contains all the starter Codémon (Aquachu, Pyrochu, Florachu)
5. Display all validation results
Starter code
team_a = {'Aquachu', 'Pyrochu', 'Florachu'}
team_b = {'Rockachu', 'Zap', 'Shadowchu'}
starter_codemon = {'Aquachu', 'Pyrochu', 'Florachu'}
print("=== TOURNAMENT REGISTRATION VALIDATION ===")
print("Team A:", team_a)
print("Team B:", team_b)
# 1. Check if Aquachu is in team_a
# 2. Check if Shadowchu is in team_b
# 3. Check if teams have no common Codémon
# 4. Check if team_a has all starter Codémon
# 5. Display results
print("\n=== VALIDATION RESULTS ===")
print(f"Aquachu in Team A: {has_aquachu}")
print(f"Shadowchu in Team B: {has_shadowchu}")
print(f"Teams are completely different: {teams_different}")
print(f"Team A has all starters: {has_all_starters}")
Back to the course