From 1fa8ae160f58b12c7a2e7f01eb8e3e1e827cce66 Mon Sep 17 00:00:00 2001 From: Nick Korostelev Date: Tue, 24 Sep 2024 06:23:27 -0700 Subject: [PATCH] Python: add Set and modify Dict (#2163) --- python.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/python.md b/python.md index 46a4236380..9211ebc758 100644 --- a/python.md +++ b/python.md @@ -46,15 +46,28 @@ sorted(list) # returns sorted copy of list ```py dict = {} +dict = {'a': 1, 'b': 2} +dict['c'] = 3 +del dict['a'] # Remove key-value pair with key 'c' dict.keys() dict.values() "key" in dict # let's say this returns False, then... -dict["key"] # ...this raises KeyError -dict.get("key") # ...this returns None +dict["key"] # if "key" not in dict raises KeyError +dict.get("key") # if "key" not in dict returns None +dict.get("key", "optional return value if no key") dict.setdefault("key", 1) **dict # expands all k/v pairs in place ``` +### Set + +```py +set = set() +set = {1, 2, 3} +set.add(4) +set.remove(2) +``` + ### Iteration ```py