Last active
December 17, 2021 16:56
-
-
Save msyvr/364732600186ef2d20c947539a04e772 to your computer and use it in GitHub Desktop.
Python sets: unique, unsorted, unchangeable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def setpy(): | |
""" | |
python sets are collections of objects which are: | |
unique | |
unordered | |
unchangeable* | |
* where unchangeable means individual items can't be replaced in place, but items can be removed and new items added | |
""" | |
# unique and unsorted | |
a = {4, 4, 2, 3} | |
print(a) | |
''' | |
prints to stdout: | |
{2, 3, 4} | |
nb: duplicate values have been omitted and items presented in ascending order, not the original ordering | |
''' | |
print(a.sorted()) | |
''' | |
generates error message: | |
AttributeError: 'set' object has no attribute 'sorted' | |
''' | |
# unchangeable | |
a[2] = 7 | |
print(a) | |
''' | |
generates error message: | |
TypeError: 'set' object does not support item assignment | |
''' | |
if __name__ == "__main__": | |
# function inputs - optional | |
# call function | |
setpy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment