• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Python Square Root Without Math Module – ** or Newton’s Method

Python Square Root Without Math Module – ** or Newton’s Method

January 29, 2022 Leave a Comment

In Python, the easiest way to find the square root of a number without the math module is with the built in exponentiation operator **.

sqrt_of_10 = 10**(1/2)

When working with numeric data in Python, one calculation which is valuable is finding the square root of a number.

We can find the square root of a number easily with the math module, but sometimes we don’t want to import modules to our code.

We can also use the built in ** to find exponents in Python. To find a square root with the ** operator, we just put “(1/2)” after **.

Below are some examples of how to use the Python built in ** operator to find square roots.

import math

print(4**(1/2))
print(9**(1/2))
print(13**(1/2))
print(90**(1/2))
print(2182**(1/2))

#Output:
2.0
3.0
3.605551275463989
9.486832980505138
46.71188285650665

Finding the Square Root of a Number Without the Python math Module

We can also estimate the square root of a number without the Python math module. To compute the square root in Python without the Python math module, we can employ the help of Newton’s Method.

Newton’s method is a root finding algorithm which can help us find an approximation of a function.

We can use Newton’s method to find the square root of a number in Python.

Below is a function which you can use to utilize Newton’s method to find an approximation for the square root of a number to precision level “a”. For a comparison, we will also use the sqrt() function from the Python math module.

import math

def newton_sqrt(n,a):
    x = n
    while(True):
        root = 0.5*(x+(n/x))
        if (abs(root-x) < a):
            break
        x = root
    return root

print(math.sqrt(13))
print(newton_sqrt(13,0.1))

print(math.sqrt(50))
print(newton_sqrt(50,0.0001))

print(math.sqrt(100))
print(newton_sqrt(100,0.000001))

print(math.sqrt(313))
print(newton_sqrt(313,0.00000001))

#Output:
3.605551275463989
3.6063454894655185

7.071067811865477
7.0710678118654755

10.0
10.0

17.69180601295413
17.69180601295413

As shown above, Newton’s method allows us to get a pretty good approximation of the square root of a number without using the math module.

Hopefully this article has been beneficial for you to learn how to find the square root of a number without the math module in Python.

Other Articles You'll Also Like:

  • 1.  Read Last Line of File Using Python
  • 2.  Write Inline If and Inline If Else Statements in Python
  • 3.  math gcd Python – Find Greatest Common Divisor with math.gcd() Function
  • 4.  Remove Leading Zeros from String with lstrip() in Python
  • 5.  Using Python turtle Module to Draw Square
  • 6.  Python Round to Nearest 10 With round() Function
  • 7.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 8.  Swap Two Values of Variables in Python
  • 9.  Python Replace Space with Underscore Using String replace() Function
  • 10.  islower Python – Check if All Letters in String Are Lowercase

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy