Skip to content

Instantly share code, notes, and snippets.

@AhmedZahid098
Created May 15, 2018 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AhmedZahid098/0dcd0b84cb44e099fc76c6e6157960f4 to your computer and use it in GitHub Desktop.
Save AhmedZahid098/0dcd0b84cb44e099fc76c6e6157960f4 to your computer and use it in GitHub Desktop.
# Sets are unordered collections of unique and immutable objects, sets are useful for common tasks
# such as filtering out duplicates, isolating differences, and performing order-neutral
# equality tests without sorting in lists, strings, and all other iterable objects
# Creating sets --> set('elements') or {'elements'}
X = set("ahmed")
print(X)
Y = {'z', 'a', 'h', 'i', 'd'}
print(Y)
# Intersection --> set1 & set2
print(X & Y)
# Union --> set1 | set2
print(X | Y)
# Difference --> set1 - set2
print (X - Y)
print (Y - X)
# Superset --> set1 > set2
print(X > Y)
print(Y > X)
# Sets filter out the duplicate elements
print({1, 2, 3, 1, 2, 3})
# Ordered neutral eqaulity test
print({'spam'} == {'aspm'})
# Membership test
print('a' in X)
print ('z' in Y)
print('p' in set('spam'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment