Unit 11.2A · Term 2

Creating Sets

A set is an unordered collection of unique elements. Sets automatically remove duplicates and provide powerful operations for comparing and combining groups of data.

Learning Objectives

  • 11.2.1.1 Determine the elements of the set
  • 11.2.1.2 Apply methods of creating sets

Lesson Presentation

11.2A-lesson-09-sets-create.pdf · Slides for classroom use

Conceptual Anchor

The Club Membership Analogy

A set is like a club membership list — each member appears only once (no duplicates), and the order doesn't matter (it's not a queue). You can check who's in both clubs (intersection), who's in either club (union), or who's in one but not the other (difference).

Rules & Theory

Set Properties

Property Description Example
Unordered No index, no defined order Cannot do my_set[0]
Unique No duplicate elements {1, 2, 2, 3}{1, 2, 3}
Mutable Can add/remove elements my_set.add(4)
Immutable elements Elements must be hashable Strings, ints, tuples ✓ Lists ✗

Creating Sets

# Method 1: Curly braces with values fruits = {"apple", "banana", "cherry"} numbers = {1, 2, 3, 4, 5} # Method 2: set() constructor from a list colors = set(["red", "green", "blue"]) # Method 3: set() from a string (each character becomes an element) letters = set("hello") print(letters) # {'h', 'e', 'l', 'o'} — note: only one 'l'! # Method 4: Empty set — MUST use set(), NOT {} empty_set = set() # ✓ Empty set empty_dict = {} # ✗ This is an empty DICTIONARY! # Method 5: set() from range even = set(range(0, 11, 2)) print(even) # {0, 2, 4, 6, 8, 10} # Duplicates are automatically removed nums = {1, 2, 2, 3, 3, 3, 4} print(nums) # {1, 2, 3, 4}

Set Methods

Method Description Example
.add(x) Add element x s.add(5)
.remove(x) Remove x (error if not found) s.remove(3)
.discard(x) Remove x (no error if missing) s.discard(99)
.pop() Remove and return arbitrary element s.pop()
.clear() Remove all elements s.clear()
len(s) Number of elements len({1,2,3}) → 3
x in s Membership test 5 in {1,2,3} → False

Worked Examples

1 Removing Duplicates from Input

names = [] for i in range(5): name = input(f"Enter name {i+1}: ") names.append(name) unique_names = set(names) print("Unique names:", unique_names) print("Duplicates removed:", len(names) - len(unique_names))

2 Building a Set Dynamically

vowels = set() # Empty set word = input("Enter a word: ").lower() # e.g., "programming" for char in word: if char in "aeiou": vowels.add(char) print("Vowels found:", vowels) print("Number of unique vowels:", len(vowels)) # Input: "programming" # Output: Vowels found: {'a', 'i', 'o'} # Number of unique vowels: 3

3 Iterating Over a Set

subjects = {"Math", "Physics", "CS", "English"} # You CAN iterate (order may vary) for subject in subjects: print(f"📚 {subject}") # You CANNOT access by index # print(subjects[0]) ← TypeError!

Pitfalls & Common Errors

{} Creates a Dictionary, Not a Set!

x = {} creates an empty dictionary. For an empty set, use x = set().

No Indexing

Sets are unordered — my_set[0] raises a TypeError. To access elements, iterate or convert to a list.

remove() vs discard()

s.remove(99) raises KeyError if 99 is not in the set. s.discard(99) silently does nothing. Use discard() when you're not sure if the element exists.

Pro-Tips for Exams

Set Tips

  • Sets automatically eliminate duplicates — great for "find unique values" questions
  • in membership testing is very fast for sets (O(1) vs O(n) for lists)
  • Remember: {} = dict, set() = empty set
  • Set elements must be immutable (strings, numbers, tuples — NOT lists)

Graded Tasks

Remember

List 4 properties of a Python set.

Understand

Explain why {1, 2, 2, 3, 3} becomes {1, 2, 3}.

Apply

Write a program that reads 10 numbers from the user and prints only the unique ones using a set.

Apply

Create a set of all consonants in a user-input sentence.

Create

Write a lottery number generator: generate a set of 6 unique random numbers between 1 and 49.

Self-Check Quiz

1. How do you create an empty set?
Click to reveal answer
2. What does set("banana") produce?
Click to reveal answer
3. Can a set contain a list?
Click to reveal answer
4. What is the difference between remove() and discard()?
Click to reveal answer
5. Can you access set elements by index?
Click to reveal answer