Browse Source

Small updates

pull/28/head
Jure Šorn 6 years ago
parent
commit
6d4aa978a8
1 changed files with 28 additions and 47 deletions
  1. 75
      README.md

75
README.md

@ -390,32 +390,14 @@ Numbers
<real> = round(<real>, ±ndigits) <real> = round(<real>, ±ndigits)
``` ```
### Constants ### Math
```python ```python
from math import e, pi from math import e, pi
```
### Trigonometry
```python
from math import cos, acos, sin, asin, tan, atan, degrees, radians from math import cos, acos, sin, asin, tan, atan, degrees, radians
```
### Logarithm
```python
from math import log, log10, log2 from math import log, log10, log2
<float> = log(<real> [, base]) # Base e, if not specified.
```
### Infinity, nan
```python
from math import inf, nan, isinf, isnan from math import inf, nan, isinf, isnan
``` ```
#### Or:
```python
float('inf'), float('nan')
```
### Statistics ### Statistics
```python ```python
from statistics import mean, median, variance, pvariance, pstdev from statistics import mean, median, variance, pvariance, pstdev
@ -444,19 +426,27 @@ from itertools import product, combinations, combinations_with_replacement, perm
>>> product([0, 1], repeat=3) >>> product([0, 1], repeat=3)
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
```
```python
>>> product('ab', '12') >>> product('ab', '12')
[('a', '1'), ('a', '2'), [('a', '1'), ('a', '2'),
('b', '1'), ('b', '2')] ('b', '1'), ('b', '2')]
```
```python
>>> combinations('abc', 2) >>> combinations('abc', 2)
[('a', 'b'), ('a', 'c'), ('b', 'c')] [('a', 'b'), ('a', 'c'), ('b', 'c')]
```
```python
>>> combinations_with_replacement('abc', 2) >>> combinations_with_replacement('abc', 2)
[('a', 'a'), ('a', 'b'), ('a', 'c'), [('a', 'a'), ('a', 'b'), ('a', 'c'),
('b', 'b'), ('b', 'c'), ('b', 'b'), ('b', 'c'),
('c', 'c')] ('c', 'c')]
```
```python
>>> permutations('abc', 2) >>> permutations('abc', 2)
[('a', 'b'), ('a', 'c'), [('a', 'b'), ('a', 'c'),
('b', 'a'), ('b', 'c'), ('b', 'a'), ('b', 'c'),
@ -469,9 +459,9 @@ Datetime
```python ```python
from datetime import datetime from datetime import datetime
now = datetime.now() now = datetime.now()
now.month # 3 now.month # 3
now.strftime('%Y%m%d') # '20180315' now.strftime('%Y%m%d') # '20180315'
now.strftime('%Y%m%d%H%M%S') # '20180315002834' now.strftime('%Y%m%d%H%M%S') # '20180315002834'
<datetime> = datetime.strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M') <datetime> = datetime.strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M')
``` ```
@ -520,7 +510,7 @@ def add(*a):
6 6
``` ```
#### Legal argument combinations with calls: #### Legal argument combinations:
```python ```python
def f(*args): # f(1, 2, 3) def f(*args): # f(1, 2, 3)
def f(x, *args): # f(1, 2, 3) def f(x, *args): # f(1, 2, 3)
@ -542,15 +532,14 @@ def f(x, *args, z, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3
### Other Uses ### Other Uses
```python ```python
>>> a = (1, 2, 3) <list> = [*<collection> [, ...]]
>>> [*a] <set> = {*<collection> [, ...]}
[1, 2, 3] <tuple> = (*<collection>, [...])
<dict> = {**<dict> [, ...]}
``` ```
```python ```python
>>> head, *body, tail = [1, 2, 3, 4] head, *body, tail = <collection>
>>> body
[2, 3]
``` ```
@ -566,8 +555,8 @@ Inline
```python ```python
<list> = [i+1 for i in range(10)] # [1, 2, ..., 10] <list> = [i+1 for i in range(10)] # [1, 2, ..., 10]
<set> = {i for i in range(10) if i > 5} # {6, 7, 8, 9} <set> = {i for i in range(10) if i > 5} # {6, 7, 8, 9}
<dict> = {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18}
<iter> = (i+5 for i in range(10)) # (5, 6, ..., 14) <iter> = (i+5 for i in range(10)) # (5, 6, ..., 14)
<dict> = {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18}
``` ```
```python ```python
@ -665,11 +654,11 @@ from functools import partial
```python ```python
def get_counter(): def get_counter():
a = 0 i = 0
def out(): def out():
nonlocal a nonlocal i
a += 1 i += 1
return a return i
return out return out
``` ```
@ -721,6 +710,8 @@ def fib(n):
return n if n < 2 else fib(n-2) + fib(n-1) return n if n < 2 else fib(n-2) + fib(n-1)
``` ```
* **Recursion depth is limited to 1000 by default. To increase it use `'sys.setrecursionlimit(<depth>)'`.**
### Parametrized Decorator ### Parametrized Decorator
```python ```python
from functools import wraps from functools import wraps
@ -839,8 +830,8 @@ class Counter:
``` ```
```python ```python
>>> c = Counter() >>> counter = Counter()
>>> c(), c(), c() >>> counter(), counter(), counter()
(1, 2, 3) (1, 2, 3)
``` ```
@ -1115,16 +1106,6 @@ b'.\n..\nfile1.txt\nfile2.txt\n'
``` ```
Recursion Limit
---------------
```python
>>> import sys
>>> sys.getrecursionlimit()
1000
>>> sys.setrecursionlimit(5000)
```
CSV CSV
--- ---
```python ```python
@ -1633,7 +1614,7 @@ def get_border(screen):
from collections import namedtuple from collections import namedtuple
P = namedtuple('P', 'x y') P = namedtuple('P', 'x y')
height, width = screen.getmaxyx() height, width = screen.getmaxyx()
return P(width - 1, height - 1) return P(width-1, height-1)
``` ```

|||||||
100:0
Loading…
Cancel
Save