Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: add Set and modify Dict #2163

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down