Question 34: Write a Python program to find maximum and the minimum value in a set & find the length of a set. Download hole Program / Project code, by clicking following link: 👉 Click Here: How to create, read, update and remove set values in Python
Describe the various set operations ? In Python, sets provide several built-in operations for performing set-related operations. These operations allow you to manipulate and compare sets in various ways. Here are the key set operations available in Python: - Union (| or union() method): The union of two sets returns a new set containing all unique elements that appear in either of the two sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # or set1.union(set2)
# union_set will be {1, 2, 3, 4, 5} - Intersection (& or intersection() method): The intersection of two sets returns a new set containing elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2 # or set1.intersection(set2)
# intersection_set will be {3} - Difference (- or difference() method): The difference of two sets returns a new set containing elements from the first set that are not in the second set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2 # or set1.difference(set2)
# difference_set will be {1, 2} - Symmetric Difference (^ or symmetric_difference() method): The symmetric difference of two sets returns a new set containing elements that are in either of the sets, but not in their intersection.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2 # or set1.symmetric_difference(set2)
# symmetric_difference_set will be {1, 2, 4, 5} - Subset (issubset() method): You can check if one set is a subset of another set using the issubset() method. It returns True if the set is a subset; otherwise, it returns False.
set1 = {1, 2, 3}
set2 = {1, 2}
is_subset = set2.issubset(set1) # is_subset will be True - Superset (issuperset() method): You can check if one set is a superset of another set using the issuperset() method. It returns True if the set is a superset; otherwise, it returns False.
set1 = {1, 2, 3}
set2 = {1, 2}
is_superset = set1.issuperset(set2) # is_superset will be True - Disjoint Sets (isdisjoint() method): You can check if two sets have no elements in common using the isdisjoint() method. It returns True if the sets have no common elements; otherwise, it returns False.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
is_disjoint = set1.isdisjoint(set2) # is_disjoint will be True
These set operations are quite useful when working with data that needs to be compared, combined, or analyzed based on set principles. Programming Code: Following code write in: BP_P34.py # Find min & max value in a set
# also find length of set
# create set
set1 = set([5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
print("set1 is: ", set1)
print("Maximum value of set1 is: ", max(set1)) # using max() function
print("Minimum value of set1 is: ", min(set1)) # using min() function
print("Length of set1 is: ", len(set1)) # using len() function
# Thanks for Reading.
Output:
Describe the various set operations ?
In Python, sets provide several built-in operations for performing set-related operations. These operations allow you to manipulate and compare sets in various ways. Here are the key set operations available in Python:
- Union (| or union() method): The union of two sets returns a new set containing all unique elements that appear in either of the two sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # or set1.union(set2)
# union_set will be {1, 2, 3, 4, 5} - Intersection (& or intersection() method): The intersection of two sets returns a new set containing elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2 # or set1.intersection(set2)
# intersection_set will be {3} - Difference (- or difference() method): The difference of two sets returns a new set containing elements from the first set that are not in the second set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2 # or set1.difference(set2)
# difference_set will be {1, 2} - Symmetric Difference (^ or symmetric_difference() method): The symmetric difference of two sets returns a new set containing elements that are in either of the sets, but not in their intersection.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2 # or set1.symmetric_difference(set2)
# symmetric_difference_set will be {1, 2, 4, 5} - Subset (issubset() method): You can check if one set is a subset of another set using the issubset() method. It returns True if the set is a subset; otherwise, it returns False.
set1 = {1, 2, 3}
set2 = {1, 2}
is_subset = set2.issubset(set1) # is_subset will be True - Superset (issuperset() method): You can check if one set is a superset of another set using the issuperset() method. It returns True if the set is a superset; otherwise, it returns False.
set1 = {1, 2, 3}
set2 = {1, 2}
is_superset = set1.issuperset(set2) # is_superset will be True - Disjoint Sets (isdisjoint() method): You can check if two sets have no elements in common using the isdisjoint() method. It returns True if the sets have no common elements; otherwise, it returns False.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
is_disjoint = set1.isdisjoint(set2) # is_disjoint will be True
These set operations are quite useful when working with data that needs to be compared, combined, or analyzed based on set principles.
# Find min & max value in a set
# also find length of set
# create set
set1 = set([5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
print("set1 is: ", set1)
print("Maximum value of set1 is: ", max(set1)) # using max() function
print("Minimum value of set1 is: ", min(set1)) # using min() function
print("Length of set1 is: ", len(set1)) # using len() function
# Thanks for Reading.
Output:
0 Comments