Python | 程序打印给定列表中的回文数

这篇博客介绍了如何使用Python找出给定列表中的回文数。回文数是指正读反读都一样的数字,如0、1、2、11、22等。输入格式是先给出列表大小n,再给出n个数字。通过将数字转化为字符串并利用Python的内置函数reversed(),可以检查并打印出列表中的所有回文数。
摘要由CSDN通过智能技术生成

First few palindrome numbers are 0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 101 , 111 , 121 , ... and so on.

前几个回文数为0、1、2、3、4、5、6、7、8、9、11、22、33、44、55、66、77、88、99、101、111、121,.... .. 等等。

Input format: Given a number n, size of list then next line contains space separated n numbers.

输入格式:给定数字n ,列表的大小 ,然后下一行包含空格分隔的n个数字。

Logic: We will simply convert the number into string and then using reversed(string) predefined function in python ,we will check whether the reversed string is same as the number or not.

逻辑:我们将简单地将数字转换为字符串,然后在python中使用反向(string)预定义函数,我们将检查反向字符串是否与数字相同。

Program:

程序:

# Give size of list
n=int(input())

# Give list of numbers having size n
l=list(map(int,input().strip().split(' ')))

print("Palindrome numbers are:")
# check through the list to check 
# number is palindrome or not
for i in l:
    num=str(i)
    if("".join(reversed(num))==num):
        print(i)

Output

输出量

Python program to print palindrome number

翻译自: https://www.includehelp.com/python/print-palindrome-numbers-from-the-given-list.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值