From cce4967f0a20f4ae7749191de7679e9e09508497 Mon Sep 17 00:00:00 2001 From: Pavlo Stadnikov Date: Tue, 27 Sep 2016 16:37:48 +0300 Subject: [PATCH 1/4] add some build-in methods for list and dict --- sheet.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sheet.md b/sheet.md index 2a238b2..605b1cd 100644 --- a/sheet.md +++ b/sheet.md @@ -68,8 +68,12 @@ g = c[-1] # access last element h = ['re', 'bl'] + ['gr'] # list concatenation i = ['re'] * 5 # repeat a list ['re', 'bl'].index('re') # returns index of 're' +a.append('yellow') # add new element to end of list +a.extend(b) # add elements from list `b` to end of list `a` +a.insert(1, 'yellow') # insert element in specified position 're' in ['re', 'bl'] # true if 're' in list sorted([3, 2, 1]) # returns sorted list +a.pop(2) # remove and return item at index (default last) ``` ### Dictionaries @@ -80,6 +84,12 @@ b = a['red'] # translate item c = [value for key, value in a.items()] # loop through contents d = a.get('yellow', 'no translation found') # return default a.setdefault('extra', []).append('cyan') # init key with default +a.update({'green': 'vert', 'brown': 'brun'}) # update dictionary by data from another one +a.keys() # get list of keys +a.values() # get list of values +a.items() # get list of key-value pairs +del a['red'] # delete key and associated with it value +a.pop('blue') # remove specified key and return the corresponding value ``` ### Strings From 411980aa159ccd4f411700e48e7515fd3bf18ee6 Mon Sep 17 00:00:00 2001 From: Pavlo Stadnikov Date: Tue, 27 Sep 2016 17:11:18 +0300 Subject: [PATCH 2/4] add set section --- sheet.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/sheet.md b/sheet.md index 605b1cd..18fe906 100644 --- a/sheet.md +++ b/sheet.md @@ -8,6 +8,7 @@ Scientific Python Cheatsheet - [Types](#types) - [Lists](#lists) - [Dictionaries](#dictionaries) + - [Sets, Frozensets](#sets-frozensets) - [Strings](#strings) - [Operators](#operators) - [Control Flow](#control-flow) @@ -72,6 +73,7 @@ a.append('yellow') # add new element to end of list a.extend(b) # add elements from list `b` to end of list `a` a.insert(1, 'yellow') # insert element in specified position 're' in ['re', 'bl'] # true if 're' in list +'fi' not in ['re', 'bl'] # true if 'fi' not in list sorted([3, 2, 1]) # returns sorted list a.pop(2) # remove and return item at index (default last) ``` @@ -81,6 +83,7 @@ a.pop(2) # remove and return item at index (default la ```python a = {'red': 'rouge', 'blue': 'bleu'} # dictionary b = a['red'] # translate item +'red' in a # true if dictionary a contains key 'red' c = [value for key, value in a.items()] # loop through contents d = a.get('yellow', 'no translation found') # return default a.setdefault('extra', []).append('cyan') # init key with default @@ -92,6 +95,33 @@ del a['red'] # delete key and associated with it a.pop('blue') # remove specified key and return the corresponding value ``` + +### Sets, Frozensets + +```python +a = {1, 2, 3} # initialize manually +b = set(range(5)) # initialize from iteratable +a.add(13) # add new element to set +a.discard(13) # discard element from set +a.update([21, 22, 23]) # update set with elements from iterable +a.pop() # remove and return an arbitrary set element +2 in {1, 2, 3} # true if 2 in set +5 in {1, 2, 3} # true if 5 not in set +a.issubset(b) # test whether every element in a is in b +a <= b # issubset in operator form +a.issuperset(b) # test whether every element in b is in a +a >= b # issuperset in operator form +a.intersection(b) # return the intersection of two sets as a new set +a & b # intersection in operator form +a.differnce(b) # return the difference of two or more sets as a new set +a - b # difference in operator form +a.symmetric_difference(b) # return the symmetric difference of two sets as a new set +a ^ b # symmetric_difference in operator form +a.union(b) # return the union of sets as a new set +a | b # union in operator form +c = frozenset() # the same as set but immutable +``` + ### Strings ```python From ea494201c059156e93cd12e3367be15aa2c2b0e8 Mon Sep 17 00:00:00 2001 From: Pavlo Stadnikov Date: Tue, 27 Sep 2016 17:14:21 +0300 Subject: [PATCH 3/4] add logical operators --- sheet.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheet.md b/sheet.md index 18fe906..f66b02e 100644 --- a/sheet.md +++ b/sheet.md @@ -149,8 +149,11 @@ abs(a) # absolute value 2 < 1 # smaller 1 != 2 # not equal 1 != 2 and 2 < 3 # logical AND +a & b # logical AND 1 != 2 or 2 < 3 # logical OR +a | b # logical OR not 1 == 2 # logical NOT +a ^ b # logical XOR 'a' in b # test if a is in b a is b # test if objects point to the same memory (id) ``` From 0bc05c6b460e5075b7c4faba155b916d1f406e80 Mon Sep 17 00:00:00 2001 From: Pavlo Stadnikov Date: Tue, 11 Oct 2016 12:30:14 +0300 Subject: [PATCH 4/4] fixed issue #29 --- sheet.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sheet.md b/sheet.md index f66b02e..aac8fbe 100644 --- a/sheet.md +++ b/sheet.md @@ -8,7 +8,7 @@ Scientific Python Cheatsheet - [Types](#types) - [Lists](#lists) - [Dictionaries](#dictionaries) - - [Sets, Frozensets](#sets-frozensets) + - [Sets](#sets) - [Strings](#strings) - [Operators](#operators) - [Control Flow](#control-flow) @@ -96,7 +96,7 @@ a.pop('blue') # remove specified key and return t ``` -### Sets, Frozensets +### Sets ```python a = {1, 2, 3} # initialize manually @@ -112,13 +112,10 @@ a <= b # issubset in operator form a.issuperset(b) # test whether every element in b is in a a >= b # issuperset in operator form a.intersection(b) # return the intersection of two sets as a new set -a & b # intersection in operator form -a.differnce(b) # return the difference of two or more sets as a new set +a.difference(b) # return the difference of two or more sets as a new set a - b # difference in operator form a.symmetric_difference(b) # return the symmetric difference of two sets as a new set -a ^ b # symmetric_difference in operator form a.union(b) # return the union of sets as a new set -a | b # union in operator form c = frozenset() # the same as set but immutable ```