Sets Unique Elements and Set Operations#

⏳ Loading Pyodide…

Party Intro: Welcome to the Data Purge Party, You Duplicate-Dropping Disaster!#

Crank it up, you data-dumping DJ! You’ve crashed the Data Purge Party, where sets are ruthless spinners tossing out duplicate tracks like yesterday’s mixtape! I’m your host, screaming because you’re trying to play “phone” twice in a row—sets don’t mess with that noise. Sets are your VIP pass for unique products, customers, or tags, making lookups 1000x faster than lists and crushing Excel’s duplicate hell in one beat. Union, intersection, and difference are your dance moves for merging catalogs and segmenting customers. Every code snippet’s a banger with a zinger, and the exercises? Dance-offs that’ll roast your repetitive crap while you laugh. Grab your glowsticks (keyboard) and spin clean, or you’re getting ejected with a DuplicateError to the face!

Code Beats with Laughs: Sets Drop the Mic#

Create Sets: The Duplicate Purge Sets are like a DJ who only spins unique tracks—add duplicates, and they’re ghosted faster than a bad SoundCloud rapper.

Try adding “laptop” again? Set just shrugs, “Already spinning, you repetitive rookie!”

Fast Lookups: The VIP Check Sets check membership faster than a bouncer scanning wristbands—perfect for inventory or customer tags.

Check a bad item like 42? Set snickers, “Not on the list, you gatecrashing goof!”

Set Operations: The Dancefloor Merge Union, intersection, and difference are your moves to mix catalogs or segment crowds like a pro DJ.

Bad merge like {1} | ["phone"]? TypeError drops the mic, “Mixing types? You’re ruining the vibe!”

Segmentation: The Crowd Control Sets slice through customer data like a DJ curating a hype playlist—find unique groups in one spin.

Mess up the set? ValueError spins, “Your data’s offbeat—fix it, DJ dropout!”

Dance-Off Exercise: Spin a Set That Roasts Your Lame Tracks#

Time to drop a beat, you duplicate-dropping DJ! Build a Product Playlist Dashboard:

  1. Create your_products = ['laptop', 'phone', 'laptop', 'tablet', 'phone'] with duplicates.

  2. Use set() to clean it. If duplicates remain, print, “Duplicates detected? You’re spinning last week’s tracks, loser!”

  3. Define two sets: store_a = {'laptop', 'phone'} and store_b = {'phone', 'mouse'}. Merge with | and find exclusives with -.

  4. Write a check_stock(product, inventory) function. If item’s missing, print, “Not in stock? You’re off the guest list!”

  5. Bonus: Count shared items (&). If none, print, “No shared tracks? Your stores are a snooze-fest!”

Starter Code:

Test it, screenshot your dashboard for your GitHub party log, and don’t spin a dud, you track-mixing maestro!

Rant About Code Gatecrashers: You Rocked the Party, But Don’t Slack Off!#

Holy bangers, you survived the Data Purge Party, you duplicate-dropping DJ! You’ve cleaned catalogs, spun fast lookups, and mixed sets like a club legend. Sets are the secret weapon behind Walmart’s \(500B inventory and Netflix’s tag system—add one duplicate, and your data’s messier than a dancefloor at 3 AM. This is the shit that lands you \)140K+ data gigs while your buddies are de-duping Excel rows like suckers. Next up, tuples: Get ready to lock down your data, or I’ll kick you out with a TypeError to the speakers!

# Your code here
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)                      # show that duplicates have been removed

'orange' in basket                 # fast membership testing

'crabgrass' in basket


# Demonstrate set operations on unique letters from two words

a = set('abracadabra')
b = set('alacazam')
a                                  # unique letters in a

a - b                              # letters in a but not in b

a | b                              # letters in a or b or both

a & b                              # letters in both a and b

a ^ b                              # letters in a or b but not both