To create an empty set in Python, you can use either the set()
constructor or the curly braces {}
notation.
In this example,
set()
constructor to create an empty set in Python.# Create an empty set using set() constructor
empty_set = set()
# empty_set is now ready to be used as an empty set
# Example usage:
# Add elements to the set
empty_set.add(5)
# Remove elements from the set
empty_set.remove(5)
# Check if an element is present in the set
contains_element = 5 in empty_set
print(f'Is element 5 present in the set: {contains_element}')
Is element 5 present in the set: False
In this example,
{}
notation.set()
constructor.# Create an empty set using curly braces
another_empty_set = {}
# another_empty_set is now ready to be used as an empty set
# Example usage:
# Add elements to the set
another_empty_set.add('apple')
# Remove elements from the set (if it exists)
another_empty_set.discard('apple')
# Check if an element is present in the set
contains_element = 'apple' in another_empty_set
print(f'Is element 'apple' present in the set: {contains_element}')
Is element 'apple' present in the set: False
In this tutorial, we learned How to create an Empty Set in Python language with well detailed examples.