summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam <adam@adambard.com>2013-06-28 21:15:33 -0700
committerAdam <adam@adambard.com>2013-06-28 21:15:50 -0700
commit04cedf98446f124d4d0bc15b9a7341015d2707ca (patch)
tree12d980a9998adcc515c2b1701b45d9fa3db8c2f2
parente1424579e76958cdb7eda4efb5ceaa214d322681 (diff)
Fixes #15: Set literals
-rw-r--r--python.html.markdown17
1 files changed, 11 insertions, 6 deletions
diff --git a/python.html.markdown b/python.html.markdown
index 8c56a3a9..3b5075ae 100644
--- a/python.html.markdown
+++ b/python.html.markdown
@@ -217,18 +217,23 @@ filled_dict.setdefault("five", 6) #filled_dict["five"] is still 5
# Sets store ... well sets
empty_set = set()
# Initialize a set with a bunch of values
-filled_set = set([1,2,2,3,4]) # filled_set is now set([1, 2, 3, 4])
+some_set = set([1,2,2,3,4]) # filled_set is now set([1, 2, 3, 4])
+
+# Since Python 2.7, {} can be used to declare a set
+filled_set = {1 2 2 3 4} # => {1 2 3 4}
# Add more items to a set
-filled_set.add(5) # filled_set is now set([1, 2, 3, 4, 5])
+filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}
# Do set intersection with &
-other_set = set([3, 4, 5 ,6])
-filled_set & other_set #=> set([3, 4, 5])
+other_set = set{3, 4, 5, 6}
+filled_set & other_set #=> {3, 4, 5}
+
# Do set union with |
-filled_set | other_set #=> set([1, 2, 3, 4, 5, 6])
+filled_set | other_set #=> {1, 2, 3, 4, 5, 6}
+
# Do set difference with -
-set([1,2,3,4]) - set([2,3,5]) #=> set([1, 4])
+{1,2,3,4} - {2,3,5} #=> {1, 4}
# Check for existence in a set with in
2 in filled_set #=> True