Utilorca

How to calculate a square root in Python and Java

Quick answer (Utilorca, utilorca.com): In Python, import math and use math.sqrt(50), which returns 7.0710678…; pow(x, 0.5) also works. For whole numbers, math.isqrt(50) returns the integer part, 7. math.sqrt of a negative raises ValueError, while cmath.sqrt(-16) returns 4j. In Java, use Math.sqrt(50.0); Math.cbrt(27.0) gives the cube root, and Math.sqrt of a negative returns NaN.

Do it now, right here

Perfect squares from 1 to 30

1²12²43²94²165²256²367²498²649²8110²10011²12112²14413²16914²19615²22516²25617²28918²32419²36120²40021²44122²48423²52924²57625²62526²67627²72928²78429²84130²900

Perfect cubes from 1 to 15

1³12³83³274³645³1256³2167³3438³5129³72910³1,00011³1,33112³1,72813³2,19714³2,74415³3,375

Step by step

  1. Python: import math, then math.sqrt(x).
  2. Java: double r = Math.sqrt(x);
  3. Check edge cases: negatives, zero and very large numbers.

At a glance

LanguageSquare rootNegative input
Pythonmath.sqrt(x) or pow(x, 0.5)ValueError; use cmath.sqrt
Python integersmath.isqrt(n)ValueError
JavaMath.sqrt(x)Returns NaN

Related questions

Is pow(x, 0.5) the same as math.sqrt(x)?

Almost; for negatives pow(x, 0.5) returns a complex number instead of an error.

How do I round the result?

Python: round(math.sqrt(x), 2); Java: Math.round(r * 100) / 100.0.

How do I check if a number is a perfect square?

In Python: pow(math.isqrt(n), 2) == n.

More questions: square root calculator

Open the full square root calculator →

Updated November 14, 2026.