Python frozenset symmetric difference: Function and example
Python Frozenset Views Data Type: Exercise-3 with Solution
Write a Python function that calculates the symmetric difference between two frozenset instances.
Sample Solution:
Code:
def symmetric_difference(frozenset_x, frozenset_y):
return frozenset_x.symmetric_difference(frozenset_y)
def main():
frozenset_a = frozenset([1, 2, 3, 4, 5, 6])
frozenset_b = frozenset([4, 5, 6, 7, 8])
print("Original frozensets:")
print(frozenset_a)
print(frozenset_b)
sym_diff_result = symmetric_difference(frozenset_a, frozenset_b)
print("\nSymmetric Difference of said two frozenset:", sym_diff_result)
if __name__ == "__main__":
main()
Output:
Original frozensets: frozenset({1, 2, 3, 4, 5, 6}) frozenset({4, 5, 6, 7, 8}) Symmetric Difference of said two frozenset: frozenset({1, 2, 3, 7, 8})
Flowchart:
Previous: Python frozen set and set conversion: Differences and comparisons.
Next: Python program: Generating power set of a frozenset.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/extended-data-types/python-extended-data-types-index-frozenset-views-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics