@@ -493,11 +493,11 @@ Format
493
493
Numbers
494
494
-------
495
495
``` python
496
- < int > = int (< float / str / bool > ) # Or: math.trunc(<float>)
497
- < float > = float (< int / str / bool > ) # Or: <int/ float>e±<int>
498
- < complex > = complex (real = 0 , imag = 0 ) # Or: <int/ float> ± <int/ float>j
499
- < Fraction> = fractions.Fraction(0 , 1 ) # Or: Fraction(numerator=0, denominator=1)
500
- < Decimal> = decimal.Decimal(< str / int > ) # Or: Decimal((sign, digits, exponent))
496
+ < int > = int (< float / str / bool > ) # Whole number of any size. Truncates floats.
497
+ < float > = float (< int / str / bool > ) # 64-bit decimal number. Also < float>e±<int>.
498
+ < complex > = complex (real = 0 , imag = 0 ) # Complex number. Also `< float> ± <float>j`.
499
+ < Fraction> = fractions.Fraction(< int > , < int > ) # E.g. `Fraction(1, 2) / 3 == Fraction(1, 6)`.
500
+ < Decimal> = decimal.Decimal(< str / int / tuple > ) # E.g. ` Decimal((1, (2, 3), 4)) == -230_000`.
501
501
```
502
502
* ** ` 'int(<str>)' ` and ` 'float(<str>)' ` raise ValueError on malformed strings.**
503
503
* ** Decimal numbers are stored exactly, unlike most floats where ` '1.1 + 2.2 != 3.3' ` .**
@@ -507,54 +507,55 @@ Numbers
507
507
508
508
### Built-in Functions
509
509
``` python
510
- < num> = pow (< num> , < num> ) # Or: <number> ** <number>
511
- < num> = abs (< num> ) # <float> = abs(< complex>)
512
- < num> = round (< num> [, ±ndigits]) # Also math.floor/ceil(<number>) .
513
- < num> = min (< collection> ) # Also max(<num>, <num> [, ...]).
514
- < num> = sum (< collection> ) # Also math.prod(<collection>).
510
+ < num> = pow (< num> , < num> ) # E.g. `pow(2, 3) == 2 ** 3 == 8`.
511
+ < num> = abs (< num> ) # E.g. ` abs(complex(3, 4)) == 5`.
512
+ < num> = round (< num> [, ±ndigits]) # E.g. `round(123, -1) == 120` .
513
+ < num> = min (< collection> ) # Also max(<num>, <num> [, ...]).
514
+ < num> = sum (< collection> ) # Also math.prod(<collection>).
515
515
```
516
516
517
517
### Math
518
518
``` python
519
- from math import pi, inf, nan, isnan # `inf*0` and `nan+1` return nan.
520
- from math import sqrt, factorial # `sqrt(-1)` raises ValueError.
521
- from math import sin, cos, tan # Also: asin, degrees, radians.
522
- from math import log, log10, log2 # Log accepts base as second arg.
519
+ from math import floor, ceil, trunc # They convert floats into integers.
520
+ from math import pi, inf, nan, isnan # `inf * 0` and `nan + 1` return nan.
521
+ from math import sqrt, factorial # `sqrt(-1)` will raise ValueError.
522
+ from math import sin, cos, tan # Also: asin, acos, degrees, radians.
523
+ from math import log, log10, log2 # Log accepts base as second argument.
523
524
```
524
525
525
526
### Statistics
526
527
``` python
527
- from statistics import mean, median, mode # Also: variance, stdev, quantiles.
528
+ from statistics import mean, median, mode # Mode returns the most common item.
529
+ from statistics import variance, stdev # Also: pvariance, pstdev, quantiles.
528
530
```
529
531
530
532
### Random
531
533
``` python
532
- from random import random, randint, uniform # Also: gauss, choice, shuffle, seed.
534
+ from random import random, randint, uniform # Also: gauss, choice, shuffle, seed.
533
535
```
534
536
535
537
``` python
536
- < float > = random() # Returns a float inside [0, 1).
537
- < num> = randint/ uniform(a, b) # Returns an int/float inside [a, b].
538
- < float > = gauss(mean, stdev) # Also triangular(low, high, mode).
539
- < el> = choice(< sequence> ) # Keeps it intact. Also sample(pop, k ).
540
- shuffle(< list > ) # Shuffles the list in place .
538
+ < float > = random() # Returns a float inside [0, 1).
539
+ < num> = randint/ uniform(a, b) # Returns an int/float inside [a, b].
540
+ < float > = gauss(mean, stdev) # Also triangular(low, high, mode).
541
+ < el> = choice(< sequence> ) # Keeps it intact. Also sample(p, n ).
542
+ shuffle(< list > ) # Works on any mutable sequence .
541
543
```
542
544
543
545
### Hexadecimal Numbers
544
546
``` python
545
- < int > = ±0x < hex > # Or: ±0b<bin>
546
- < int > = int (' ±<hex>' , 16 ) # Or: int('±<bin>', 2)
547
- < int > = int (' ±0x<hex>' , 0 ) # Or: int('±0b<bin>', 0)
548
- < str > = hex (< int > ) # Returns '[-]0x<hex>'. Also bin().
547
+ < int > = 0x < hex > # E.g. `0xFF == 255`. Also 0b<bin>.
548
+ < int > = int (' ±<hex>' , 16 ) # Also int('±0x<hex>/±0b<bin>', 0).
549
+ < str > = hex (< int > ) # Returns '[-]0x<hex>'. Also bin().
549
550
```
550
551
551
552
### Bitwise Operators
552
553
``` python
553
- < int > = < int > & < int > # And ( 0b1100 & 0b1010 == 0b1000) .
554
- < int > = < int > | < int > # Or ( 0b1100 | 0b1010 == 0b1110) .
555
- < int > = < int > ^ < int > # Xor ( 0b1100 ^ 0b1010 == 0b0110) .
556
- < int > = < int > << n_bits # Left shift. Use >> for right.
557
- < int > = ~ < int > # Not. Also -<int> - 1.
554
+ < int > = < int > & < int > # E.g. ` 0b1100 & 0b1010 == 0b1000` .
555
+ < int > = < int > | < int > # E.g. ` 0b1100 | 0b1010 == 0b1110` .
556
+ < int > = < int > ^ < int > # E.g. ` 0b1100 ^ 0b1010 == 0b0110` .
557
+ < int > = < int > << n_bits # Left shift. Use >> for right.
558
+ < int > = ~ < int > # Not. Same as ` -<int> - 1` .
558
559
```
559
560
560
561
@@ -747,10 +748,10 @@ Inline
747
748
748
749
### Comprehensions
749
750
``` python
750
- < list > = [i+ 1 for i in range (10 )] # Or: [1, 2, ..., 10]
751
- < iter > = (i for i in range (10 ) if i > 5 ) # Or: iter([6, 7, 8, 9])
752
- < set > = {i+ 5 for i in range (10 )} # Or: {5, 6, ..., 14}
753
- < dict > = {i: i* 2 for i in range (10 )} # Or: {0: 0, 1: 2, ..., 9: 18}
751
+ < list > = [i+ 1 for i in range (10 )] # Returns ` [1, 2, ..., 10]`.
752
+ < iter > = (i for i in range (10 ) if i > 5 ) # Returns ` iter([6, 7, 8, 9])`.
753
+ < set > = {i+ 5 for i in range (10 )} # Returns ` {5, 6, ..., 14}`.
754
+ < dict > = {i: i* 2 for i in range (10 )} # Returns ` {0: 0, 1: 2, ..., 9: 18}`.
754
755
```
755
756
756
757
``` python
@@ -764,9 +765,9 @@ from functools import reduce
764
765
```
765
766
766
767
``` python
767
- < iter > = map (lambda x : x + 1 , range (10 )) # Or: iter([1, 2, ..., 10])
768
- < iter > = filter (lambda x : x > 5 , range (10 )) # Or: iter([6, 7, 8, 9])
769
- < obj> = reduce (lambda out , x : out + x, range (10 )) # Or: 45
768
+ < iter > = map (lambda x : x + 1 , range (10 )) # Returns ` iter([1, 2, ..., 10])`.
769
+ < iter > = filter (lambda x : x > 5 , range (10 )) # Returns ` iter([6, 7, 8, 9])`.
770
+ < obj> = reduce (lambda out , x : out + x, range (10 )) # Returns `45`.
770
771
```
771
772
772
773
### Any, All
@@ -860,8 +861,7 @@ from functools import partial
860
861
>> > multiply_by_3(10 )
861
862
30
862
863
```
863
- * ** Partial is also useful in cases when a function needs to be passed as an argument because it enables us to set its arguments beforehand.**
864
- * ** A few examples being: ` 'defaultdict(<func>)' ` , ` 'iter(<func>, to_exc)' ` and dataclass's ` 'field(default_factory=<func>)' ` .**
864
+ * ** Partial is also useful in cases when a function needs to be passed as an argument because it enables us to set its arguments beforehand (` 'collections.defaultdict(<func>)' ` , ` 'iter(<func>, to_exc)' ` and ` 'dataclasses.field(default_factory=<func>)' ` ).**
865
865
866
866
### Non-Local
867
867
** If variable is being assigned to anywhere in the scope, it is regarded as a local variable, unless it is declared as a 'global' or a 'nonlocal'.**
0 commit comments