In Python, how can I get a whole number without decimals from math.sqrt?

Advertisements
import math

a = math.sqrt(25)

print(a)

My output is 5.0, how can I get a 5 (whole number) instead?

>Solution :

In a reduced manner, you can use a 1 line if operator to assign an integer value to the result of sqrt if both integer and decimal values are the same:

import math

a = math.sqrt(25)
a = int(a) if int(a)==a else a

print(a)

Leave a ReplyCancel reply