<p>Python's <a href="https://docs.python.org/3/library/fractions.html" rel="nofollow noreferrer"><code>fractions</code></a> module and its class, <code>Fraction</code>, implement arithmetic with rational numbers. The <code>Fraction</code> class doesn't implement a square root operation, because most square roots are irrational numbers. However, it can be used to approximate a square root with arbitrary accuracy, because a <code>Fraction</code>'s numerator and denominator are arbitrary-precision integers.</p>
<p>The following method takes a positive number <code>x</code> and a number of iterations, and returns upper and lower bounds for the square root of <code>x</code>.</p>
<pre><code>from fractions import Fraction

def sqrt(x, n):
    x = x if isinstance(x, Fraction) else Fraction(x)
    upper = x + 1
    for i in range(0, n):
        upper = (upper + x/upper) / 2
    lower = x / upper
    if lower &gt; upper:
        raise ValueError(&quot;Sanity check failed&quot;)
    return (lower, upper)
</code></pre>
<p>See the reference below for details on this operation's implementation. It also shows how to implement other operations with upper and lower bounds (although there is apparently at least one error with the <code>log</code> operation there).</p>
<ul>
<li>Daumas, M., Lester, D., Muñoz, C., &quot;Verified Real Number Calculations: A Library for Interval Arithmetic&quot;, arXiv:0708.3721 [cs.MS], 2007.</li>
</ul>
<p>Alternatively, using Python's <code>math.isqrt</code>, we can calculate a square root to arbitrary precision:</p>
<ul>
<li>Square root of <code>i</code> within 1/2<sup><em>n</em></sup> of the correct value, where <code>i</code> is an integer:<code>Fraction(math.isqrt(i * 2**(n*2)), 2**n)</code>.</li>
<li>Square root of <code>i</code> within 1/10<sup><em>n</em></sup> of the correct value, where <code>i</code> is an integer:<code>Fraction(math.isqrt(i * 10**(n*2)), 10**n)</code>.</li>
<li>Square root of <code>x</code> within 1/2<sup><em>n</em></sup> of the correct value, where <code>x</code> is a multiple of 1/2<sup><em>n</em></sup>:<code>Fraction(math.isqrt(x * 2**(n)), 2**n)</code>.</li>
<li>Square root of <code>x</code> within 1/10<sup><em>n</em></sup> of the correct value, where <code>x</code> is a multiple of 1/10<sup><em>n</em></sup>:<code>Fraction(math.isqrt(x * 10**(n)), 10**n)</code>.</li>
</ul>
<p>In the foregoing, <code>i</code> or <code>x</code> must be 0 or greater.</p>
How do I calculate square root in Python?
<p>I need to calculate the square root of some numbers, for example <code>√9 = 3</code> and <code>√2 = 1.4142</code>. How can I do it in Python?</p>
<p>The inputs will probably be all positive integers, and relatively small (say less than a billion), but just in case they're not, is there anything that might break?</p>
<hr />
<p><strong>Related</strong></p>
<ul>
<li><a href="https://stackoverflow.com/questions/15390807/integer-square-root-in-python">Integer square root in python</a>
<ul>
<li><a href="https://stackoverflow.com/questions/15978781/how-to-find-integer-nth-roots">How to find integer nth roots?</a></li>
</ul>
</li>
<li><a href="https://stackoverflow.com/questions/19255120/is-there-a-short-hand-for-nth-root-of-x-in-python">Is there a short-hand for nth root of x in Python?</a></li>
<li><a href="https://stackoverflow.com/questions/33684948/difference-between-1-2-math-sqrt-and-cmath-sqrt">Difference between **(1/2), math.sqrt and cmath.sqrt?</a></li>
<li><a href="https://stackoverflow.com/questions/41551529/why-is-math-sqrt-incorrect-for-large-numbers">Why is math.sqrt() incorrect for large numbers?</a></li>
<li><a href="https://stackoverflow.com/questions/28239978/python-sqrt-limit">Python sqrt limit for very large numbers?</a></li>
<li><a href="https://stackoverflow.com/questions/327002/which-is-faster-in-python-x-5-or-math-sqrtx">Which is faster in Python: x**.5 or math.sqrt(x)?</a></li>
<li><a href="https://stackoverflow.com/questions/9595135/why-does-python-give-the-wrong-answer-for-square-root">Why does Python give the &quot;wrong&quot; answer for square root?</a> (specific to Python 2)</li>
<li><a href="https://stackoverflow.com/questions/15424123/calculating-n-th-roots-using-python-3s-decimal-module">calculating n-th roots using Python 3&#39;s decimal module</a></li>
<li><a href="https://stackoverflow.com/questions/17766774/how-can-i-take-the-square-root-of-1-using-python">How can I take the square root of -1 using python?</a> (focused on NumPy)</li>
<li><a href="https://stackoverflow.com/questions/10725522/arbitrary-precision-of-square-roots">Arbitrary precision of square roots</a></li>
</ul>
<p><em><sub><strong>Note</strong>: This is an attempt at a <a href="https://meta.stackoverflow.com/q/291992/4518341">canonical question</a> after <a href="https://meta.stackoverflow.com/questions/415385/how-can-we-handle-a-question-where-the-problem-in-the-title-is-different-from-th">a discussion on Meta</a> about <a href="https://stackoverflow.com/questions/9595135/why-does-python-give-the-wrong-answer-for-square-root">an existing question with the same title</a>.</sub></em></p>