- Популярные видео
- Авто
- Видео-блоги
- ДТП, аварии
- Для маленьких
- Еда, напитки
- Животные
- Закон и право
- Знаменитости
- Игры
- Искусство
- Комедии
- Красота, мода
- Кулинария, рецепты
- Люди
- Мото
- Музыка
- Мультфильмы
- Наука, технологии
- Новости
- Образование
- Политика
- Праздники
- Приколы
- Природа
- Происшествия
- Путешествия
- Развлечения
- Ржач
- Семья
- Сериалы
- Спорт
- Стиль жизни
- ТВ передачи
- Танцы
- Технологии
- Товары
- Ужасы
- Фильмы
- Шоу-бизнес
- Юмор
Symmetric difference of two sets in python | #pythonsets
Symmetric Difference of Two Sets in Python
In Python, the symmetric difference of two sets means the elements that are present in either of the sets, but not in both. In simple words, it returns the elements that are unique to each set and removes the common elements.
In mathematics, symmetric difference is written as:
It can also be expressed as:
That means:
Take elements in A but not in B
Take elements in B but not in A
Combine both results
🔹 Using symmetric_difference() Method
Python provides a built-in method called symmetric_difference() for sets.
Copy code
Python
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.symmetric_difference(set2)
print(result)
Output:
Copy code
{1, 2, 5, 6}
Explanation:
Common elements are 3 and 4 (removed)
Unique elements in set1 → 1, 2
Unique elements in set2 → 5, 6
🔹 Using ^ Operator
We can also use the ^ operator to find symmetric difference
Python
set1 = {"apple", "banana", "mango"}
set2 = {"banana", "grapes", "orange"}
result = set1 ^ set2
print(result)
Output:
{'apple', 'mango', 'grapes', 'orange'}
Here, "banana" is common in both sets, so it is removed.
🔹 Key Points
Symmetric difference works only with sets.
It removes duplicate values automatically.
Order of elements does not matter in sets.
It returns a new set.
🔹 Real-Life Example
Suppose:
Set A = Students who play Cricket
Set B = Students who play Football
Symmetric difference gives students who play only one game (Cricket or Football), but not both.
Видео Symmetric difference of two sets in python | #pythonsets канала The Coding Professor
In Python, the symmetric difference of two sets means the elements that are present in either of the sets, but not in both. In simple words, it returns the elements that are unique to each set and removes the common elements.
In mathematics, symmetric difference is written as:
It can also be expressed as:
That means:
Take elements in A but not in B
Take elements in B but not in A
Combine both results
🔹 Using symmetric_difference() Method
Python provides a built-in method called symmetric_difference() for sets.
Copy code
Python
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.symmetric_difference(set2)
print(result)
Output:
Copy code
{1, 2, 5, 6}
Explanation:
Common elements are 3 and 4 (removed)
Unique elements in set1 → 1, 2
Unique elements in set2 → 5, 6
🔹 Using ^ Operator
We can also use the ^ operator to find symmetric difference
Python
set1 = {"apple", "banana", "mango"}
set2 = {"banana", "grapes", "orange"}
result = set1 ^ set2
print(result)
Output:
{'apple', 'mango', 'grapes', 'orange'}
Here, "banana" is common in both sets, so it is removed.
🔹 Key Points
Symmetric difference works only with sets.
It removes duplicate values automatically.
Order of elements does not matter in sets.
It returns a new set.
🔹 Real-Life Example
Suppose:
Set A = Students who play Cricket
Set B = Students who play Football
Symmetric difference gives students who play only one game (Cricket or Football), but not both.
Видео Symmetric difference of two sets in python | #pythonsets канала The Coding Professor
Комментарии отсутствуют
Информация о видео
20 февраля 2026 г. 13:03:56
00:01:16
Другие видео канала





















