Add info about sets
This commit is contained in:
parent
0de1c28104
commit
271887f837
|
@ -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
|
||||
|
@ -73,4 +74,27 @@ print(f"Color tuple: R={color[0]} G={color[1]} B={color[2]}")
|
|||
# strings: "Cheese"
|
||||
# integers: 44
|
||||
# lists: ['a', 'b', 'c']
|
||||
# objects: MyClass(param1, param2)
|
||||
# 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.
|
Loading…
Reference in New Issue
Block a user