Sets are a type of data structure in Python which are used to store unique elements. They are unordered and do not allow duplicates.
To create a set in Python, use the set() function. For example:
my_set = set([1, 2, 3, 4, 5])
This will create a set containing the numbers 1, 2, 3, 4, and 5.
To add elements to a set, use the add() method. For example:
This will add the number 6 to the set.
To remove elements from a set, use the remove() method. For example:
This will remove the number 3 from the set.
To check if an element is present in a set, use the in keyword. For example:
if 5 in my_set:
print(„5 is present in the set”)
This will print the message „5 is present in the set” if the number 5 is present in the set.
To get the number of elements in a set, use the len() function. For example:
num_elements = len(my_set)
This will store the number of elements in the set (in this case, 5) in the variable num_elements.