Square Root in Python

In this Python tutorial, we will learn about how to calculate a square root in python. We have created a project at the end of the blog. Also, we will cover these topics.

  1. Square Root in Python3
  2. Square Root in Python Without Math
  3. Square Root in Python With Math
  4. Square Root in Python With With SQRT
  5. Square Root in Python With Without SQRT
  6. Square Root in Python Program
  7. Square Root in Python Numpy
  8. Square Root in Python Example

Square Root in Python3

A square root is a factor of number when multiplied by itself, produces a original number. In this section, we will learn how to calculate Square Root in Python3.

  • Math module in Python provides sqrt() function using which we can calculate the square root of the number in python3.
  • Alternatively, we can also produce square using multiply operator (*) in python3. We can multiply the number with itself to produce square of that number. For example, 7*7=49.
  • Exponent (**) operator in python can also be used to calculate square root in python3. Any number having the power of 1/2 will produce the square root of that number. For example, 49**2=7.
  • In the further sections, we will discus each of these methods in detail.

Also read, Python Number Guessing Game

Square Root in Python Without Math

Math is a module in python which allows all the mathematical operations on the number using python. But in this section, we will cover how we can Square Root in Python without the Math module.

  • Math module provides function for all types of mathmatical operations like You can perform square root, exponent, log, theta, etc.
  • Since, square root is simply multiplication of number by itself so we can perform that even without using the math module.
  • We wil show you how you calcuate the square and then we will calculate the square root of that number.
    • Multiply Operator (*) is use to calculate square.
    • Exponent Operator (**) is use to calculate square root.

Using Multiply Operator

Multiply operator multiples two given numbers with each other. Square can be produced by multiplying a number by itself. In the below example, a square root of 7 is produced.

>>> a = 7
>>> b = 7
>>> square_root = a * b
>>> print(square_root)
49

Using Exponent Operator

Exponent operator (**) in python is used to raise a number to the power of exponent. Any number with power 0.5 or 1/2 will give the square root of that number. for example, 490.5 = 7.

>>> num = 7
>>> power = 2
>>> square_root = num ** 2
>>> print(square_root)
49

Read If not condition in python

Square Root in Python With Math

In this section, we will learn how to find Square Root in Python with Math module.

  • The math module provide functions for all kinds of mathematical operations from basics (addition, multiplication, square root, etc.) to advance (theta, ceil, factorial, log, etc.)
  • The mathematical operations supported in math module are defined by C language.
  • Inorder to use the math module you have to import it first and to see the available mathematical methods visit their official website.
  • Using math.isqrt(n) in python math module you can perform square root of any non-negative number.
  • In our example, we have calculated the square root of number 49 which should be 7.
import math
res = math.isqrt(49)
print('result: ', res)

result: 7

Read Matplotlib multiple bar chart

Square Root in Python Without SQRT

In this section, we will learn how to calculate square root in python without the sqrt method of the python math module.

  • Power of 2 applied on any number gives the square of that number. So to find the square root we can apply power of 1/2 on the number.
  • Python provide exponent operator (**) to add the power on the number. This operator accepts the number and power.
  • For example, to find the square root of 49 use the following code: 49**0.5. Here 0.5 is the power and 49 is the number.
  • So in this way we can find the square root in python without sqrt method. Here is the small demonstration.

Source Code:

In this code, we have calculated square root of 49 without using sqrt method.

num = 49
power = 0.5

res = num ** power
print(res)

Output:

In this output, square root of 49 is calculated and result is 7.

square root in  without sqrt
Square Root in Python Without SQRT

Read Python find index of element in list

Square Root in Python With SQRT

In this section, we will learn how to calculate square root in python with sqrt.

  • Math module provide a isqrt() function using which we can calculate the square root of the number.
  • The number must be positive inorder to calculate square root using this methon in python.
  • first step in the process is to import the math module in python.

Source Code:

In this code, using math module we have calculated square root of the number.

import math

res = math.isqrt(49)
print(res)

Output:

In this output, we are using sqrt method of python math module to calculate the square root of 49.

python square root in python  sqrt

Read Python Return Function

Square Root in Python Program

In this section, we will create a cli-based program to calculate square root in python. We will create a command-line based program that will accept a number from user and will display the result on the terminal or cli.

Source Code:

In this code, we are using math module to calculate the square root. User will enter a number then the square root of that number will be displayed.

import math

user_input = int(input('Enter Number: '))
res = math.isqrt(user_input)

print('Result: ', res)

Output:

In this output, user can input any number and the program will calculate it’s square root. In our case we have input 49 and the generated result is 7.

Square Root in Python Program
Python Square Root Program

Read Download zip file from URL using python

Square Root in Python Numpy

In this section, we will learn how to calculate square root using python Numpy.

  • Python NumPy is an advance library used to process data in an array format.
  • Using numpy.sqrt() method in python we can calculate the square root of each element in the array.
  • First step in the process is to import the numpy library. Incase, ‘no numpy module found’ error appeared then install it using following command.
pip install numpy
  • for example, if this array is [9, 64, 81] then the output will be [3, 8, 9].

Source Code:

Here is the code to calculate square root using NumPy. first, we have imported NumPy module, then using numpy.sqrt() the method we have calculated the square root of each element in the NumPy array in python.

import numpy as np

num = np.sqrt([
         [64, 49, 36],
         [81, 225, 100]
        ])

print(num)

Output:

In this output, using np.sqrt() the function of NumPy module is used to calculate square root in python.

Square Root in Python Numpy
Square Root in Python Numpy

Read Python NumPy 2d array

Square Root in Python Example

In this section, we will learn how to create square root calculator using python tkinter.

  • Tkinter module in python is used to create Graphical User interface (GUI) applications of the application.
  • We have use math.isqrt() function of math module in python. Using this we have calculated the square root of the number entered by user.

def sqrt_root():
    r = math.isqrt(var.get())
    res.set(f'Square Root of {var.get()} is {r}')
  • sqrt_root() is the function created by us to perform square root in python. In this code var.get() the input collected from the user.
  • this input is passed inside the math.isqrt() function and the result is stored in the variable ‘r’.
  • ‘r’ has the result of calculation now we have displayed it on the label.

Source Code:

Here is the complete source code of square root in python.

from tkinter import *
import math

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')

def sqrt_root():
    r = math.isqrt(var.get())
    res.set(f'Square Root of {var.get()} is {r}')
    

var = IntVar()
var.set(49)
res = StringVar()

Label(
    ws,
    text='Square Root Calculator',
    font=('sans-serif', 15),
    relief=SOLID,
    padx=10,
    pady=5
).pack(pady=(10, 15))

Entry(
    ws,
    textvariable=var,
    font=('sans-serif', 13),
).pack()

Button(
    ws,
    text='Calculate',
    command=sqrt_root,

).pack(pady=(10, 10))

Label(
    ws,
    textvariable=res,
    font=('sans-serif', 15),
    fg='green'
).pack()


ws.mainloop()

Output:

This is the example of Square Root in Python GUI based application. The user has to enter the positive number and click on calculate button. The result will be displayed in green text. In this case square root of 49 is 7.

Square Root in Python Example
Square Root in Python Example

You may like the following Python tutorials:

In this tutorial, we have learned how to calculate square root in python. Also, we have covered these topics.

  • Square Root in Python3
  • Square Root in Python Without Math
  • Square Root in Python With Math
  • Square Root in Python With With SQRT
  • Square Root in Python With Without SQRT
  • Square Root in Python Program
  • Square Root in Python Numpy
  • Square Root in Python Example