@@ -200,23 +200,23 @@ Iterator
200
200
201
201
### Itertools
202
202
``` python
203
- from itertools import count, repeat, cycle, chain, islice
203
+ import itertools as it
204
204
```
205
205
206
206
``` 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.
210
210
```
211
211
212
212
``` 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.
215
215
```
216
216
217
217
``` 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.
220
220
```
221
221
222
222
@@ -486,11 +486,11 @@ Format
486
486
Numbers
487
487
-------
488
488
``` 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))
494
494
```
495
495
* ** ` 'int(<str>)' ` and ` 'float(<str>)' ` raise ValueError on malformed strings.**
496
496
* ** Decimal numbers are stored exactly, unlike most floats where ` '1.1 + 2.2 != 3.3' ` .**
@@ -499,47 +499,46 @@ Numbers
499
499
500
500
### Basic Functions
501
501
``` 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`
505
505
```
506
506
507
507
### Math
508
508
``` 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.
512
512
```
513
513
514
514
### Statistics
515
515
``` python
516
- from statistics import mean, median, variance, stdev, quantiles, groupby
516
+ from statistics import mean, median, variance # Also: stdev, quantiles, groupby.
517
517
```
518
518
519
519
### Random
520
520
``` 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.
526
525
```
527
526
528
527
### Bin, Hex
529
528
``` 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>'.
534
533
```
535
534
536
535
### Bitwise Operators
537
536
``` 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.
543
542
```
544
543
545
544
@@ -549,39 +548,40 @@ Combinatorics
549
548
* ** If you want to print the iterator, you need to pass it to the list() function first!**
550
549
551
550
``` python
552
- from itertools import product, combinations, combinations_with_replacement, permutations
551
+ import itertools as it
553
552
```
554
553
555
554
``` 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 )]
558
558
```
559
559
560
560
``` 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
565
565
```
566
566
567
567
``` 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
571
571
```
572
572
573
573
``` 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
578
578
```
579
579
580
580
``` 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 .
585
585
```
586
586
587
587
@@ -1701,34 +1701,34 @@ import os, shutil, subprocess
1701
1701
```
1702
1702
1703
1703
``` python
1704
- os.chdir(< path> ) # Changes the current working directory.
1705
- os.mkdir(< path> , mode = 0o 777 ) # Creates a directory. Mode is in octal.
1706
- os.makedirs(< path> , mode = 0o 777 ) # Creates all path's dirs. Also: `exist_ok=False`.
1704
+ os.chdir(< path> ) # Changes the current working directory.
1705
+ os.mkdir(< path> , mode = 0o 777 ) # Creates a directory. Permissions are in octal.
1706
+ os.makedirs(< path> , mode = 0o 777 ) # Creates all path's dirs. Also: `exist_ok=False`.
1707
1707
```
1708
1708
1709
1709
``` 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.
1712
1712
```
1713
1713
1714
1714
``` 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.
1717
1717
```
1718
1718
1719
1719
``` 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.
1723
1723
```
1724
1724
* ** Paths can be either strings, Paths or DirEntry objects.**
1725
1725
* ** Functions report OS related errors by raising either OSError or one of its [ subclasses] ( #exceptions-1 ) .**
1726
1726
1727
1727
### Shell Commands
1728
1728
``` 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.
1732
1732
```
1733
1733
1734
1734
#### Sends '1 + 1' to the basic calculator and captures its output:
@@ -1754,8 +1754,8 @@ JSON
1754
1754
1755
1755
``` python
1756
1756
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.
1759
1759
```
1760
1760
1761
1761
### Read Object from JSON File
@@ -1779,8 +1779,8 @@ Pickle
1779
1779
1780
1780
``` python
1781
1781
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.
1784
1784
```
1785
1785
1786
1786
### Read Object from File
0 commit comments