8000 Big changes in Numbers, Inline, Closure · gto76/python-cheatsheet@79fca70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 79fca70

Browse files
committed
Big changes in Numbers, Inline, Closure
1 parent ee85425 commit 79fca70

File tree

3 files changed

+84
-85
lines changed

3 files changed

+84
-85
lines changed

README.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,11 @@ Format
493493
Numbers
494494
-------
495495
```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`.
501501
```
502502
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
503503
* **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.**
@@ -507,54 +507,55 @@ Numbers
507507

508508
### Built-in Functions
509509
```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>).
515515
```
516516

517517
### Math
518518
```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.
523524
```
524525

525526
### Statistics
526527
```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.
528530
```
529531

530532
### Random
531533
```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.
533535
```
534536

535537
```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.
541543
```
542544

543545
### Hexadecimal Numbers
544546
```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().
549550
```
550551

551552
### Bitwise Operators
552553
```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`.
558559
```
559560

560561

@@ -747,10 +748,10 @@ Inline
747748

748749
### Comprehensions
749750
```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}`.
754755
```
755756

756757
```python
@@ -764,9 +765,9 @@ from functools import reduce
764765
```
765766

766767
```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`.
770771
```
771772

772773
### Any, All
@@ -860,8 +861,7 @@ from functools import partial
860861
>>> multiply_by_3(10)
861862
30
862863
```
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>)'`).**
865865

866866
### Non-Local
867867
**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

Comments
 (0)
0