Finding square root without using math.sqrt()?
<p>I recently was working on finding the square root of a number in <code>python</code> without using <code>sqrt()</code>. I came across this code and am having difficulty in understanding the logic in this code:</p>

<pre><code>def sqrt(x):
    last_guess= x/2.0
    while True:
        guess= (last_guess + x/last_guess)/2
        if abs(guess - last_guess) &lt; .000001: # example threshold
            return guess
        last_guess= guess
</code></pre>

<p>More specifically the logic behind calculating <code>guess</code> in above code. Can anyone help me understanding the logic?</p>

<p>You can always use the power (<code>**</code>) operator with an inverse exponent:</p>

<pre><code>a = 2 ** (1.0 / 2)
a
&gt; 1.41421...
</code></pre>

python square-root