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:
parent
28c5f62de1
commit
320c38959e
10
python.md
10
python.md
|
@ -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()`.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue