How to take the square root of a number in Python

Using the Sqrt() function from the math module

  1. Import the math module using import math at the beginning of the program

  2. Find out the square root of the number using math.sqrt(number) method

  3. Note that it throws an error if a negative number is passed

import math
print(math.sqrt(19))
# Throws an error if a negative number is passed
#print(math.sqrt(-19))

Writing a function

  1. Create a function that takes one argument

  2. Then, if the number is negative, return nothing. (Imaginary numbers are another topic for another shot)

  3. Now, as a square root of a number is simply the number raised to the power 0.5, raise the given number to the power of 0.5 using the following syntax:

numberexponent=number^{exponent} =

                         number**exponent
  1. This will give us the square root of the number; return it

  2. Finally, print out the result to the user

def sqrt(n):
if n < 0:
return
else:
return n**0.5
print(sqrt(19))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved