方法一:
def is_palindrome(n):
s = str(n)
l = int(len(s)/2)+1
for m in range(l):
if s[m] != s[len(s) - m - 1]:
return False
else:
return True
output = filter(is_palindrome, range(1, 200))
print('1~200:', list(output))
if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:
print('测试成功!')
else:
print('测试失败!')
方法二:
def is_palindrome(n):
return str(n) == str(n)[::-1]
str(n)[::-1]
可以表示从右向左读字符串,刚好就是反过来的数