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 51 of 77
←PreviousPrevNextNext→

Join Sets

Joining Sets

Sets support several operations to combine or compare their elements. These operations return new sets or modify existing ones:

- **union()**: Returns a new set containing all items from both sets

- **update()**: Adds all items from another set into the current set

- **intersection()**: Returns a set with elements common to both sets

- **intersection_update()**: Keeps only items present in both sets (in-place)

- **difference()**: Returns items only in the first set

- **difference_update()**: Removes items found in another set (in-place)

- **symmetric_difference()**: Returns elements in either set, but not in both

- **symmetric_difference_update()**: Updates a set with the symmetric difference

Example
# Joining sets examples
set1 = {"a", "b", "c"}
set2 = {"c", "d", "e"}

# Union
union_set = set1.union(set2)
print("Union:", union_set)

# Intersection
intersection_set = set1.intersection(set2)
print("Intersection:", intersection_set)

# Difference
difference_set = set1.difference(set2)
print("Difference:", difference_set)

# Symmetric difference
symmetric_set = set1.symmetric_difference(set2)
print("Symmetric Difference:", symmetric_set)
Output
Union: {'a', 'b', 'c', 'd', 'e'}
Intersection: {'c'}
Difference: {'a', 'b'}
Symmetric Difference: {'a', 'b', 'd', 'e'}
Test your knowledge: Join Sets
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 51 of 77
←PreviousPrevNextNext→