Add info about sets

This commit is contained in:
Junior 2024-06-09 18:49:25 -04:00
parent 0de1c28104
commit 271887f837

View File

@ -1,5 +1,6 @@
# Data Structures
# Data structures hold sets of data in various formats
# We will discuss: list, dictionary (dict), tuple, set
# List
# Lists have collections of data
@ -74,3 +75,26 @@ print(f"Color tuple: R={color[0]} G={color[1]} B={color[2]}")
# integers: 44
# lists: ['a', 'b', 'c']
# objects: MyClass(param1, param2)
# Set
# Sets have collections of data
# set elements must be unique in the set
# Usually of the same type because uniqueness makes less sense otherwise
# with elements separated by commas
# where elements are unordered (i.e. can't reference specific element, slice, or index)
# are contained within curly braces
# Note: Once a set has been created its values cannot be changed,
# and elements cannot be added/removed
# Declare our set
dice = {"1", "2", "3", "4", "5", "6"}
# Testing if an element exists is done with "in"
if "3" in dice:
print("The dice set contains a 3!")
# Iterate over a set
for die in dice:
print(f"dice element: {die}")
# NOTE!! You will see that the printed elements are in an arbitrary order.
# Because sets are unordered you cannot rely on which element comes
# before or after another element and will likely not match the order
# created inside the set declaration.