From 320c38959eb5706940ad1856bc206114d7dd45c4 Mon Sep 17 00:00:00 2001 From: yeknomedoc <0909812@outlook.com> Date: Sat, 18 Sep 2021 15:46:50 +0200 Subject: [PATCH] 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. --- python.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python.md b/python.md index 222cc6b1..8750057b 100644 --- a/python.md +++ b/python.md @@ -3,12 +3,17 @@ title: Python category: Python --- -### Lists +### Tuples (immutable) + + tuple = () + +### Lists (mutable) list = [] list[i:j] # returns list subset list[-1] # returns last element list[:-1] # returns all but the last element + *list # expands all elements in place list[i] = val list[i:j] = otherlist # replace ith to jth-1 elements with otherlist @@ -34,12 +39,14 @@ category: Python ### Dict + dict = {} 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.setdefault("key", 1) + **dict # expands all k/v pairs in place ### 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()`. ``` -