5.2.2 Adding Elements with add()

Unit: Unit 5: Dictionaries and Sets • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Add elements dynamically to a set
active_roles = {"viewer", "editor"}

# 1. Add a new role
active_roles.add("admin")

# 2. Add an already existing role (idempotent - no duplicates added)
active_roles.add("viewer")

print("Active roles sorted:", sorted(active_roles))
print("Total unique roles:", len(active_roles))
Open in Live Interactive Python Sandbox →