Last active
February 23, 2022 15:24
-
-
Save codistwa/9b1c3a8b3cc45b21227c79cdbe775c9c to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/sets. More courses on https://codistwa.com
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
# ============================================================ | |
# Definition | |
# ============================================================ | |
set([0, 1, 2, 3]) | |
# ============================================================ | |
# Add an item | |
# ============================================================ | |
s = set([1, 33, 67]) | |
s.add(2) | |
print(s) # {1, 2, 67, 33} | |
# Update | |
s.update([26, 12, 9, 14]) | |
# Copy | |
s2 = s.copy() | |
print(s2) # {1, 2, 67, 33, 9, 12, 14, 26} | |
# ============================================================ | |
# Remove an item | |
# ============================================================ | |
s = set([1, 2, 3, 4, 5, 6]) | |
s.pop() | |
print(s) # {2, 3, 4, 5, 6} | |
set([2,3,4,5,6]) | |
s.remove(3) | |
print(s) # {2, 4, 5, 6} | |
# ============================================================ | |
# Copy a set | |
# ============================================================ | |
s = set([1, 33, 67]) | |
s2 = s.copy() | |
print(s2) # {1, 67, 33} | |
# ============================================================ | |
# Intersection | |
# ============================================================ | |
s1 = set([4, 6, 9]) | |
s2 = set([1, 6, 8]) | |
s3 = s1.intersection(s2) | |
print(s3) # {6} | |
# ============================================================ | |
# Union | |
# ============================================================ | |
s1 = set([4, 6, 9]) | |
s2 = set([1, 6, 8]) | |
s3 = s1.union(s2) | |
print(s3) # {1, 4, 6, 8, 9} | |
# ============================================================ | |
# Difference | |
# ============================================================ | |
s1 = set([4, 6, 9]) | |
s2 = set([1, 6, 8]) | |
s3 = s1.difference(s2) | |
print(s3) # {9, 4} | |
# ============================================================ | |
# Symmetrical difference | |
# ============================================================ | |
s1 = set([4, 6, 9]) | |
s2 = set([1, 6, 8]) | |
s3 = s1.symmetric_difference(s2) | |
print(s3) # {1, 4, 8, 9} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment