👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial Series – Lesson 8: Sets in Python

Python Tutorial for Beginners (Lesson 8): Learn Python sets with examples, operations, and exercises – unique collections, union, intersection, difference, and real-world use cases.

Python Tutorial Series – Lesson 8: Sets in Python

Welcome back to the Python Tutorial Series for Beginners! 🎉

In Lesson 7, we explored dictionaries – collections of key-value pairs.

Now in Lesson 8, we’re diving into another powerful data structure: sets.

By the end of this lesson, you’ll understand:

  • What sets are
  • How to create and use them
  • Common set operations (union, intersection, difference)
  • Set methods
  • When to use sets in real-world problems
  • Beginner exercises

🔹 What is a Set?

A set in Python is:

  • An unordered collection of unique items (no duplicates)
  • Defined using curly braces {} or the set() function
  • Useful for eliminating duplicates and performing mathematical set operations

👉 Think of a set like a real-world collection:

  • A basket of fruits where each fruit appears only once.
 
# Example of a set
fruits = {"apple", "banana", "cherry"}

print(fruits)  

🔹 1. Creating a Set

 
# Empty set (must use set() not {})
my_set = set()

# Set with items
numbers = {1, 2, 3, 4, 5}

# Duplicates are ignored
nums = {1, 2, 2, 3, 3, 4}

print(nums)  # {1, 2, 3, 4}

🔹 2. Adding & Removing Items

 
fruits = {"apple", "banana"}

# Add item
fruits.add("cherry")

# Add multiple items
fruits.update(["orange", "grape"])

# Remove item
fruits.remove("banana")   # Throws error if not found
fruits.discard("mango")   # Safer, no error if not found

# Remove random item
fruits.pop()

🔹 3. Set Operations (Very Powerful 🚀)

Sets are great for math-like operations:

 
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

print(a | b)   # Union: {1, 2, 3, 4, 5, 6}
print(a & b)   # Intersection: {3, 4}
print(a - b)   # Difference: {1, 2}
print(a ^ b)   # Symmetric Difference: {1, 2, 5, 6}

🔹 4. Checking Membership

 
fruits = {"apple", "banana", "cherry"}

print("apple" in fruits)   # True
print("mango" not in fruits)  # True

🔹 5. Useful Set Methods

 
numbers = {1, 2, 3}

numbers.add(4)          # Add element
numbers.update([5, 6])  # Add multiple elements
numbers.remove(2)       # Remove element
numbers.clear()         # Empty the set

🔹 6. Real-World Use Case

✅ Removing duplicates from a list:

 
names = ["John", "Alice", "John", "Mary", "Alice"]
unique_names = set(names)

print(unique_names)  # {'Mary', 'John', 'Alice'}

✅ Checking common interests between users:

 
user1 = {"music", "movies", "sports"}
user2 = {"sports", "travel", "music"}

print(user1 & user2)  # {'music', 'sports'}

🔹 Exercises for Beginners

Try these in a new file (lesson8.py):

  1. Create a set of 5 fruits and print it.
  2. Add a new fruit to the set.
  3. Remove one fruit using .discard().
  4. Create two sets of numbers and find their union, intersection, and difference.
  5. Challenge: Convert a list with duplicates into a set to get unique values.

🎯 Recap

In this lesson, you learned:
✅ Sets store unique, unordered items
✅ You can add, remove, and update items
✅ Sets support powerful operations like union, intersection, and difference
✅ Useful for removing duplicates and comparing collections

Next up: Lesson 9 – Conditional Statements in Python 🟢 (where programs make decisions).

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

Python, Python tutorials, Python Beginners series, Python Step-byStep
3 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy