Expanded the cheat-sheet (#1694)

Expanded the cheat-sheet with:
- *, ** as expansion arguments for lists and dicts./
- A Tuple section
- 'mutable' and 'immutable' notation behind the Tuple and Lists header.
This commit is contained in:
yeknomedoc 2021-09-18 15:46:50 +02:00 committed by GitHub
parent 28c5f62de1
commit 320c38959e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -3,12 +3,17 @@ title: Python
category: Python category: Python
--- ---
### Lists ### Tuples (immutable)
tuple = ()
### Lists (mutable)
list = [] list = []
list[i:j] # returns list subset list[i:j] # returns list subset
list[-1] # returns last element list[-1] # returns last element
list[:-1] # returns all but the last element list[:-1] # returns all but the last element
*list # expands all elements in place
list[i] = val list[i] = val
list[i:j] = otherlist # replace ith to jth-1 elements with otherlist list[i:j] = otherlist # replace ith to jth-1 elements with otherlist
@ -34,12 +39,14 @@ category: Python
### Dict ### Dict
dict = {}
dict.keys() dict.keys()
dict.values() dict.values()
"key" in dict # let's say this returns False, then... "key" in dict # let's say this returns False, then...
dict["key"] # ...this raises KeyError dict["key"] # ...this raises KeyError
dict.get("key") # ...this returns None dict.get("key") # ...this returns None
dict.setdefault("key", 1) dict.setdefault("key", 1)
**dict # expands all k/v pairs in place
### Iteration ### Iteration
@ -158,4 +165,3 @@ with open("welcome.txt", "r") as file:
# It closes the file automatically at the end of scope, no need for `file.close()`. # It closes the file automatically at the end of scope, no need for `file.close()`.
``` ```