From 271887f83791d3515907ec76108d2b3fc4a83b22 Mon Sep 17 00:00:00 2001 From: Junior Date: Sun, 9 Jun 2024 18:49:25 -0400 Subject: [PATCH] Add info about sets --- Python/019_datastructures.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Python/019_datastructures.py b/Python/019_datastructures.py index f2fb10c..e4f5f81 100644 --- a/Python/019_datastructures.py +++ b/Python/019_datastructures.py @@ -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) \ No newline at end of file +# 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. \ No newline at end of file