Python | Program to Find Square Root

Square root of a specific number is defined as some other number which when multiplied with itself results in that specific number. For example – Square root of b will be some number a such that b = a * a. Do note that Square Roots only exists for positive numbers, complex numbers but not for negative numbers.
In this article, I’ll discuss How this logic around Mathematical Square Root can be implemented as Python Code.
There are multiple ways which can be used for doing this –

  • Using sqrt function from Math Module
  • Using sqrt function form CMath Module(Only For Complex Number)
  • Using Basic Mathematical Expression
  • Using pow() Python Function

Let’s discuss each one of these Code Designs for Finding Square Root of a Number in Python.

Finding Square Root of a Number using sqrt function from Math Module

  1. Import sqrt function from Python’s Math Module
  2. Ask user for a number whose Square Root to be calculated
  3. If number entered by user is not positive, prompt user to enter positive number
  4. Otherwise pass user entered number to sqrt() function
  5. sqrt() function will return Square Root of Number

Python Code for Finding Square Root of Number

# Python Program for finding Square Root of Number

from math import sqrt        # From Python's Math Module import sqrt function

# Define Number whose Square Root to be calculated
number = int(input("Enter a Number:"))

if(number >= 0):
	sqrt_of_number = sqrt(number)
	print("Square Root of Number",number,"is =>",sqrt_of_number)
else:
	print("Please Enter a Positive Number")

Output of Above Code

Enter a Number:16
Square Root of Number 16 is => 4.0

Finding Square Root of a Complex Number using CMath Module

  1. Import sqrt function from Python’s CMath Module
  2. Define a complex number like complex_number = 19 + 27j
  3. Pass complex number to sqrt function which returns Square Root of Complex Number

Python Code for Finding Square Root of Complex Number

# Python Program for finding Square Root of Number

from cmath import sqrt        # From Python's Math Module import sqrt function

complex_number = 10 + 3j     # A Complex Number in Python  

sqrt_of_complex_number = sqrt(complex_number)

print("Square Root of Complex Number",complex_number,"is =>",sqrt_of_complex_number)

Output of Above Code

Square Root of Complex Number (10+3j) is => (3.19689744196702+0.46920491733918884j)

Finding Square Root of a Number using Basic Mathematical Expression

  1. Just define a number like number = 10
  2. Multiply number with 0.5, that’s it resulting value will be square root of number

Python Code for Finding Square Root of Number

# Python Program for finding Square Root of Number

number = 10                            # Define a Number

square_root_of_number = number ** 0.5

print("Square Root of", number, "is =>",square_root_of_number)

Output of Above Code

Square Root of 10 is => 3.1622776601683795

Finding Square Root of Number using pow() Python Function

  1. Import pow function from Python’s Math Module
  2. Define a number like number = 12
  3. Pass defined number and 0.5 to pow() function, which will return square root of defined number.

Python Code for Finding Square Root of Number

# Python Program for finding Square Root of Number

# Import function pow from Python's Math Module
from math import pow

number = 12                            # Define a number

square_root_of_number = pow(number, 0.5)

print("Square Root of", number, "is =>",square_root_of_number)

Output of Above Code

Square Root of 12 is => 3.4641016151377544

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts