回文数是指正读(从左往右)和反读(从右往左)都一样的一类数字,例如:12321、1221等。小数不是回文数。Python有很多方法判断一个数是不是回文数,现在只介绍其中两种。

"""
判断一个数是不是回文数,列表切片
"""
def is_palindrome(num):
n = list(str(num))
tmp = int("".join(n[::-1]))
#print("num = %d, tmp = %d" %(num, tmp))
return num == tmp
"""
判断一个数是不是回文数,整数取余取整
"""
def is_palindrome(num):
temp = num
total = 0
while temp > 0:
total = total * 10 + temp % 10
temp //= 10
# print(num, total)
return total == num

if __name__ == "__main__":
num = int(input("请输入一个正整数,num = "))
if is_palindrome(num):
print("%d 是回文数!" % num)
else:
print("%d 不是回文数!" % num)
posted on 2019-05-15 10:43  懒惰的Grace  阅读(13193)  评论(0编辑  收藏  举报