• 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
  • Ruby
  • 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.  Changing Python Turtle Speed with speed() Function
  • 2.  Python Get First Word in String
  • 3.  Count Spaces in String in Python
  • 4.  How to Capitalize the First Letter of Every Word in Python
  • 5.  pandas round – Round Numbers in Series or DataFrame
  • 6.  Truncate String in Python with String Slicing
  • 7.  Sum Columns Dynamically with pandas in Python
  • 8.  Create Symbolic Link with Python os.symlink() Function
  • 9.  Remove Every Nth Element from List in Python
  • 10.  Squaring in Python – Square a Number Using Python math.pow() Function

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy