Python Set - Construction of python sets using curly braces

Overview

A python set object can be constructed by specifying the elements of the sets separated by 

commas inside two curly braces. Such a set object is a mutable object. That is elements
can be added to or removed from the set. Using curly braces only non-frozen, mutable sets
can be created. 

Since a mutable set object does not have a hash value, they can not be used as dictionary key.
Neither they can be used as an element of another set.

Example:

>>> containers = {'list', 'tuple', 'range', 'str', 'bytes', 'bytearray', 'memoryview', 'jar', 'tin'}

>>> containers.add('set')

>>> containers.add('frozenset')

>>> containers.add('dict')

>>> containers.remove('jar')

>>> containers.remove('tin')

>>> print(containers)

{'frozenset', 'set', 'list', 'bytearray', 'tuple', 'bytes', 'str', 'dict', 'range', 'memoryview'}


Copyright 2024 © pythontic.com