Python例子

Python例子-求1~100中平方根是整数的数

Python例子-求1~100中平方根是整数的数,有两种方法:

# 求出1~100中平方根是整数的数:
# 方法一
import math
def is_sqr(x):
    num = math.sqrt(x)%1==0
    return num
num_list = filter(is_sqr,range(1,100))
print(list(num_list)) # [1, 4, 9, 16, 25, 36, 49, 64, 81]

# 方法二
num = range(1,100)
num_list = []
for x in num:
    for z in num:
        if(x*x <= 100) and (z == x*x):
            num_list.append(z)
print(num_list) # [1, 4, 9, 16, 25, 36, 49, 64, 81]


最后修改:2020年3月20日 21:57