DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 46 of 77
←PreviousPrevNextNext→

Python Sets

What is a Set?

A set is an unordered collection of unique elements. Sets are mutable, but the items they hold must be immutable (e.g., numbers, strings, tuples).

You can create sets using curly braces `{}` or with the `set()` constructor.

Example
# Creating sets
fruits = {"apple", "banana", "cherry"}
numbers = set([1, 2, 3, 4, 5])

print(fruits)
print(numbers)
Output
{'cherry', 'banana', 'apple'}
{1, 2, 3, 4, 5}

Set Characteristics

  • Unordered: Elements have no guaranteed order
  • Unindexed: Elements cannot be accessed by index or position
  • Unique: Duplicate elements are automatically removed
  • Mutable: Sets can be updated (add/remove items), but all elements must be immutable types

Basic Set Operations

Sets support common mathematical operations like union, intersection, and difference.

Example
# Set 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}
Output
{1, 2, 3, 4, 5, 6}
{3, 4}
{1, 2}
{1, 2, 5, 6}

Set Methods

Python provides many built-in methods to work with sets.

MethodDescriptionExample
add()Add a single elementfruits.add('orange')
update()Add multiple elementsfruits.update(['kiwi', 'melon'])
remove()Remove an element (KeyError if missing)fruits.remove('banana')
discard()Remove an element (no error if missing)fruits.discard('pear')
pop()Remove and return a random elementfruits.pop()
clear()Remove all elementsfruits.clear()
Test your knowledge: Python Sets
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 46 of 77
←PreviousPrevNextNext→