Skip to content

Commit b393c21

Browse files
committed
Itertools, Numbers, Combinatorics, OS Commands
1 parent f616831 commit b393c21

File tree

3 files changed

+144
-144
lines changed

3 files changed

+144
-144
lines changed

README.md

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,23 @@ Iterator
200200

201201
### Itertools
202202
```python
203-
from itertools import count, repeat, cycle, chain, islice
203+
import itertools as it
204204
```
205205

206206
```python
207-
<iter> = count(start=0, step=1) # Returns updated value endlessly. Accepts floats.
208-
<iter> = repeat(<el> [, times]) # Returns element endlessly or 'times' times.
209-
<iter> = cycle(<collection>) # Repeats the sequence endlessly.
207+
<iter> = it.count(start=0, step=1) # Returns updated value endlessly. Accepts floats.
208+
<iter> = it.repeat(<el> [, times]) # Returns element endlessly or 'times' times.
209+
<iter> = it.cycle(<collection>) # Repeats the sequence endlessly.
210210
```
211211

212212
```python
213-
<iter> = chain(<coll_1>, <coll_2> [, ...]) # Empties collections in order (figuratively).
214-
<iter> = chain.from_iterable(<coll>) # Empties collections inside a collection in order.
213+
<iter> = it.chain(<coll>, <coll> [, ...]) # Empties collections in order (figuratively).
214+
<iter> = it.chain.from_iterable(<coll>) # Empties collections inside a collection in order.
215215
```
216216

217217
```python
218-
<iter> = islice(<coll>, to_exclusive) # Only returns first 'to_exclusive' elements.
219-
<iter> = islice(<coll>, from_inclusive, …) # `to_exclusive, +step_size`. Indices can be None.
218+
<iter> = it.islice(<coll>, to_exclusive) # Only returns first 'to_exclusive' elements.
219+
<iter> = it.islice(<coll>, from_inc, …) # `to_exclusive, +step_size`. Indices can be None.
220220
```
221221

222222

@@ -486,11 +486,11 @@ Format
486486
Numbers
487487
-------
488488
```python
489-
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
490-
<float> = float(<int/str/bool>) # Or: <real>e±<int>
491-
<complex> = complex(real=0, imag=0) # Or: <real> ± <real>j
492-
<Fraction> = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1)
493-
<Decimal> = decimal.Decimal(<str/int>) # Or: Decimal((sign, digits, exponent))
489+
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
490+
<float> = float(<int/str/bool>) # Or: <real>e±<int>
491+
<complex> = complex(real=0, imag=0) # Or: <real> ± <real>j
492+
<Fraction> = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1)
493+
<Decimal> = decimal.Decimal(<str/int>) # Or: Decimal((sign, digits, exponent))
494494
```
495495
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
496496
* **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.**
@@ -499,47 +499,46 @@ Numbers
499499

500500
### Basic Functions
501501
```python
502-
<num> = pow(<num>, <num>) # Or: <num> ** <num>
503-
<num> = abs(<num>) # <float> = abs(<complex>)
504-
<num> = round(<num> [, ±ndigits]) # `round(126, -1) == 130`
502+
<num> = pow(<num>, <num>) # Or: <num> ** <num>
503+
<num> = abs(<num>) # <float> = abs(<complex>)
504+
<num> = round(<num> [, ±ndigits]) # `round(126, -1) == 130`
505505
```
506506

507507
### Math
508508
```python
509-
from math import e, pi, inf, nan, isinf, isnan
510-
from math import sin, cos, tan, asin, acos, atan, degrees, radians
511-
from math import log, log10, log2
509+
from math import e, pi, inf, nan, isinf, isnan # `<el> == nan` is always False.
510+
from math import sin, cos, tan, asin, acos, atan # Also: degrees, radians.
511+
from math import log, log10, log2 # Log can accept base as second arg.
512512
```
513513

514514
### Statistics
515515
```python
516-
from statistics import mean, median, variance, stdev, quantiles, groupby
516+
from statistics import mean, median, variance # Also: stdev, quantiles, groupby.
517517
```
518518

519519
### Random
520520
```python
521-
from random import random, randint, choice, shuffle, gauss, seed
522-
523-
<float> = random() # A float inside [0, 1).
524-
<int> = randint(from_inc, to_inc) # An int inside [from_inc, to_inc].
525-
<el> = choice(<sequence>) # Keeps the sequence intact.
521+
from random import random, randint, choice # Also shuffle, gauss, triangular, seed.
522+
<float> = random() # A float inside [0, 1).
523+
<int> = randint(from_inc, to_inc) # An int inside [from_inc, to_inc].
524+
<el> = choice(<sequence>) # Keeps the sequence intact.
526525
```
527526

528527
### Bin, Hex
529528
```python
530-
<int> = ±0b<bin> # Or: ±0x<hex>
531-
<int> = int('±<bin>', 2) # Or: int('±<hex>', 16)
532-
<int> = int('±0b<bin>', 0) # Or: int('±0x<hex>', 0)
533-
<str> = bin(<int>) # Returns '[-]0b<bin>'.
529+
<int> = ±0b<bin> # Or: ±0x<hex>
530+
<int> = int('±<bin>', 2) # Or: int('±<hex>', 16)
531+
<int> = int('±0b<bin>', 0) # Or: int('±0x<hex>', 0)
532+
<str> = bin(<int>) # Returns '[-]0b<bin>'.
534533
```
535534

536535
### Bitwise Operators
537536
```python
538-
<int> = <int> & <int> # And (0b1100 & 0b1010 == 0b1000).
539-
<int> = <int> | <int> # Or (0b1100 | 0b1010 == 0b1110).
540-
<int> = <int> ^ <int> # Xor (0b1100 ^ 0b1010 == 0b0110).
541-
<int> = <int> << n_bits # Left shift. Use >> for right.
542-
<int> = ~<int> # Not. Also -<int> - 1.
537+
<int> = <int> & <int> # And (0b1100 & 0b1010 == 0b1000).
538+
<int> = <int> | <int> # Or (0b1100 | 0b1010 == 0b1110).
539+
<int> = <int> ^ <int> # Xor (0b1100 ^ 0b1010 == 0b0110).
540+
<int> = <int> << n_bits # Left shift. Use >> for right.
541+
<int> = ~<int> # Not. Also -<int> - 1.
543542
```
544543

545544

@@ -549,39 +548,40 @@ Combinatorics
549548
* **If you want to print the iterator, you need to pass it to the list() function first!**
550549

551550
```python
552-
from itertools import product, combinations, combinations_with_replacement, permutations
551+
import itertools as it
553552
```
554553

555554
```python
556-
>>> product([0, 1], repeat=3)
557-
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ..., (1, 1, 1)]
555+
>>> it.product([0, 1], repeat=3)
556+
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
557+
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
558558
```
559559

560560
```python
561-
>>> product('abc', 'abc') # a b c
562-
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
563-
('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x
564-
('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x
561+
>>> it.product('abc', 'abc') # a b c
562+
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
563+
('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x
564+
('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x
565565
```
566566

567567
```python
568-
>>> combinations('abc', 2) # a b c
569-
[('a', 'b'), ('a', 'c'), # a . x x
570-
('b', 'c')] # b . . x
568+
>>> it.combinations('abc', 2) # a b c
569+
[('a', 'b'), ('a', 'c'), # a . x x
570+
('b', 'c')] # b . . x
571571
```
572572

573573
```python
574-
>>> combinations_with_replacement('abc', 2) # a b c
575-
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
576-
('b', 'b'), ('b', 'c'), # b . x x
577-
('c', 'c')] # c . . x
574+
>>> it.combinations_with_replacement('abc', 2) # a b c
575+
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
576+
('b', 'b'), ('b', 'c'), # b . x x
577+
('c', 'c')] # c . . x
578578
```
579579

580580
```python
581-
>>> permutations('abc', 2) # a b c
582-
[('a', 'b'), ('a', 'c'), # a . x x
583-
('b', 'a'), ('b', 'c'), # b x . x
584-
('c', 'a'), ('c', 'b')] # c x x .
581+
>>> it.permutations('abc', 2) # a b c
582+
[('a', 'b'), ('a', 'c'), # a . x x
583+
('b', 'a'), ('b', 'c'), # b x . x
584+
('c', 'a'), ('c', 'b')] # c x x .
585585
```
586586

587587

@@ -1701,34 +1701,34 @@ import os, shutil, subprocess
17011701
```
17021702

17031703
```python
1704-
os.chdir(<path>) # Changes the current working directory.
1705-
os.mkdir(<path>, mode=0o777) # Creates a directory. Mode is in octal.
1706-
os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also: `exist_ok=False`.
1704+
os.chdir(<path>) # Changes the current working directory.
1705+
os.mkdir(<path>, mode=0o777) # Creates a directory. Permissions are in octal.
1706+
os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also: `exist_ok=False`.
17071707
```
17081708

17091709
```python
1710-
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
1711-
shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
1710+
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
1711+
shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
17121712
```
17131713

17141714
```python
1715-
os.rename(from, to) # Renames/moves the file or directory.
1716-
os.replace(from, to) # Same, but overwrites 'to' if it exists.
1715+
os.rename(from, to) # Renames/moves the file or directory.
1716+
os.replace(from, to) # Same, but overwrites 'to' if it exists.
17171717
```
17181718

17191719
```python
1720-
os.remove(<path>) # Deletes the file.
1721-
os.rmdir(<path>) # Deletes the empty directory.
1722-
shutil.rmtree(<path>) # Deletes the directory.
1720+
os.remove(<path>) # Deletes the file.
1721+
os.rmdir(<path>) # Deletes the empty directory.
1722+
shutil.rmtree(<path>) # Deletes the directory.
17231723
```
17241724
* **Paths can be either strings, Paths or DirEntry objects.**
17251725
* **Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).**
17261726

17271727
### Shell Commands
17281728
```python
1729-
<pipe> = os.popen('<command>') # Executes command in sh/cmd and returns its stdout pipe.
1730-
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
1731-
<int> = <pipe>.close() # Closes the pipe. Returns None on success, int on error.
1729+
<pipe> = os.popen('<command>') # Executes command in sh/cmd. Returns its stdout pipe.
1730+
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
1731+
<int> = <pipe>.close() # Closes the pipe. Returns None on success.
17321732
```
17331733

17341734
#### Sends '1 + 1' to the basic calculator and captures its output:
@@ -1754,8 +1754,8 @@ JSON
17541754

17551755
```python
17561756
import json
1757-
<str> = json.dumps(<object>) # Converts object to JSON string.
1758-
<object> = json.loads(<str>) # Converts JSON string to object.
1757+
<str> = json.dumps(<object>) # Converts object to JSON string.
1758+
<object> = json.loads(<str>) # Converts JSON string to object.
17591759
```
17601760

17611761
### Read Object from JSON File
@@ -1779,8 +1779,8 @@ Pickle
17791779

17801780
```python
17811781
import pickle
1782-
<bytes> = pickle.dumps(<object>) # Converts object to bytes object.
1783-
<object> = pickle.loads(<bytes>) # Converts bytes object to object.
1782+
<bytes> = pickle.dumps(<object>) # Converts object to bytes object.
1783+
<object> = pickle.loads(<bytes>) # Converts bytes object to object.
17841784
```
17851785

17861786
### Read Object from File

0 commit comments

Comments
 (0)