runtric-logo
Python Basic 2 - DataSets: uniqueness, operations (union, intersection) (4/5)
Chapter 4
Python Basic 2 - Data

Sets: uniqueness, operations (union, intersection)

7 minutes read

Beginner friendly

Includes practice

Imagine a sticker album where each design appears only once—no matter how many duplicates you try to shove in.

That’s exactly how a Python set behaves: it keeps one copy of every item and forgets the rest.

Runtic's Learning Tips

  • If “set syntax” feels odd, that’s normal—your brain is wiring new paths right now.

  • Ask why duplicates disappear; curiosity glues the concept in place stronger than rote copying.

What is a Set?

• An unordered collection wrapped in curly braces { }.
Unique items only—duplicates vanish automatically.
• Elements must be immutable (numbers, strings, tuples).

See it in action:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Uniqueness in Practice

Create a set with duplicates, then count the unique items.

Practice Challenge

Interactive Code Editor
📝 Your Task:

Create a set named animalscontaining "cat","dog","dog",and "bird".Then, use the len()function to count how many unique items are in the set, store that number in unique_count,and print it to verify.

python

Use a set literal like {"cat", "dog"} and len() to count unique items.

Adding and Removing Elements

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Union (merge without duplicates)

Think of combining two sticker piles but keeping only one of each design.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Now you try:

Practice Challenge

Interactive Code Editor
📝 Your Task:

You are given two sets aand b.Create a new set named union_resultthat contains all unique elements from both sets. Use either the |operator or the .union()method. Finally, print the result to verify.

python

Use the | operator or .union() to combine two sets.

Intersection (what both sets share)

Sticker designs that appear in both piles.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Your turn:

Practice Challenge

Interactive Code Editor
📝 Your Task:

Given two sets basket1and basket2,find the fruits that appear in both sets. Store the result in intersection_resultusing either the &operator or the .intersection()method. Finally, print the result to verify.

python

Use & or .intersection() to find the common elements.

Quick Practice

3 minutes

Quick check: choose the best answer!

Which statement about Python sets is TRUE?

Given a = {1,2,3} and b = {3,4}, what is a & b?

Which call returns a NEW set while keeping originals unchanged?

Quick Summary

  • Sets store unique, unordered items—duplicates disappear.

  • Union (|) merges sets without repetition.

  • Intersection (&) keeps only items common to both sets.

Great work exploring the power of uniqueness!

Lesson Complete!
100%
Progress
What You've Learned:
  • Built and inspected Python sets, observing automatic duplicate removal
  • Practiced union (|) to merge sets without repetition
  • Practiced intersection (&) to extract shared elements
  • Strengthened understanding through quizzes and hands-on code edits—your set skills are officially unlocked!

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.