Skip to content
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
40 changes: 40 additions & 0 deletions sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Scientific Python Cheatsheet
- [Types](#types)
- [Lists](#lists)
- [Dictionaries](#dictionaries)
- [Sets](#sets)
- [Strings](#strings)
- [Operators](#operators)
- [Control Flow](#control-flow)
Expand Down Expand Up @@ -68,18 +69,54 @@ 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
'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)
```

### Dictionaries

```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
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
```


### Sets

```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.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.union(b) # return the union of sets as a new set
c = frozenset() # the same as set but immutable
```

### Strings
Expand Down Expand Up @@ -109,8 +146,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)
```
Expand Down