|
552 | 552 | {<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'</span>
|
553 | 553 | </code></pre></div>
|
554 | 554 |
|
555 |
| -<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs"><int> = int(<float/str/bool>) <span class="hljs-comment"># Or: math.floor(<float>)</span> |
| 555 | +<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><div><h3 id="builtintypes">Built-in Types</h3><pre><code class="python language-python hljs"><int> = int(<float/str/bool>) <span class="hljs-comment"># Or: math.floor(<float>)</span> |
556 | 556 | <float> = float(<int/str/bool>)
|
557 | 557 | <complex> = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: <real> + <real>j</span>
|
558 |
| -<Fraction> = fractions.Fraction(numerator=<span class="hljs-number">0</span>, denominator=<span class="hljs-number">1</span>) |
559 |
| -</code></pre></div> |
| 558 | +</code></pre></div></div> |
| 559 | + |
560 | 560 |
|
561 | 561 | <ul>
|
562 | 562 | <li><strong><code class="python hljs"><span class="hljs-string">'int(<str>)'</span></code> and <code class="python hljs"><span class="hljs-string">'float(<str>)'</span></code> raise ValueError on malformed strings.</strong></li>
|
563 | 563 | </ul>
|
564 |
| -<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs"><num> = pow(<num>, <num>) <span class="hljs-comment"># Or: <num> ** <num></span> |
565 |
| -<real> = abs(<num>) |
566 |
| -<int> = round(<real>) |
567 |
| -<real> = round(<real>, ±ndigits) <span class="hljs-comment"># `round(126, -1) == 130`</span> |
| 564 | +<div><h3 id="specialtypes">Special Types</h3><pre><code class="python language-python hljs"><Fraction> = fractions.Fraction(numerator=<span class="hljs-number">0</span>, denominator=<span class="hljs-number">1</span>) |
| 565 | +<Decimal> = decimal.Decimal(<int/float/str>) |
| 566 | +</code></pre></div> |
| 567 | + |
| 568 | +<ul> |
| 569 | +<li><strong>Decimal numbers can be represented exactly, unlike floats where <code class="python hljs"><span class="hljs-string">'1.1 + 2.2 == 3.3000000000000003'</span></code>.</strong></li> |
| 570 | +<li><strong>Their precision can be adjusted with <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = <int>'</span></code>.</strong></li> |
| 571 | +</ul> |
| 572 | +<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs"><num> = pow(<num>, <num>) <span class="hljs-comment"># Or: <num> ** <num></span> |
| 573 | +<num> = abs(<num>) |
| 574 | +<int> = round(<num>) |
| 575 | +<num> = round(<num>, ±ndigits) <span class="hljs-comment"># `round(126, -1) == 130`</span> |
568 | 576 | </code></pre></div>
|
569 | 577 |
|
570 | 578 | <div><h3 id="math">Math</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> e, pi, inf, nan
|
|
0 commit comments