Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Data Structures – Sets | Python Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Python Fundamentals

Lesson 16 of 55

·Python

Data Structures – Sets

20 minutes

Course progress29%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Sets are unordered collections of unique elements.
  • Supported operations: union (`|`), intersection (`&`), difference (`-`).
  • Fast membership tests (`in`), average O(1) time.
  • Use `add()`, `update()`, and `remove()` to modify.

Data Structures – Sets

Sets store unique, unordered items and support mathematical operations like union, intersection, and difference.

primes = {2, 3, 5, 7}
primes.add(11)
primes.update([13, 17])
print('All primes:', primes)

# Set operations
even = {2, 4, 6, 8}
print('Intersection:', primes & even)   # {2}
print('Union:', primes | even)         # all unique numbers

Because sets discard duplicates automatically, they’re great for membership testing (x in my_set).

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Python FundamentalsBrowse activities
Previous lessonData Structures – TuplesContinue to lesson 17Data Structures – DictionariesBack to Python Fundamentals curriculum