Python Practice › Basic Programs › Program 38

Square root of a number

EasyBasic Programs · 38 of 40

Write a Python program to find the square root of a number using the math library.

Advertisement
Input
144, 2
Output
12.0
1.4142
12
4.0

Approach

  1. Use math.sqrt(), or math.isqrt() for a whole-number root.

Solution

Python
import math print(math.sqrt(144))print(round(math.sqrt(2), 4))print(math.isqrt(150))print(16 ** 0.5)
Output
12.0
1.4142
12
4.0

Line by line

Line 3

sqrt always returns a float.

Line 5

isqrt returns the whole-number part of the root (12 for 150).

Line 6

Raising to the power 0.5 also gives the square root.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Leave a Comment