Python Practice Example 30
Problem:A 5-digit number, determine whether it is a palindrome. That is, 12321 is a palindrome, the ones digit is the same as the ten-thousands digit, and the tens digit is the same as the thousands digit.
Program analysis:None.
Program source code:
Example (Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = int(raw_input("Please enter a number:\n"))
x = str(a)
flag = True
for i in range(len(x)/2):
if x[i] != x[-i - 1]:
flag = False
break
if flag:
print "%d is a palindrome!" % a
else:
print "%d is not a palindrome!" % a
Example (Python 3.0+)
#!/usr/bin/python3
a = int(input("Please enter a number:\n"))
x = str(a)
flag = True
for i in range(len(x)//2):
if x[i] != x[-i - 1]:
flag = False
break
if flag:
print ("%d is a palindrome!" % a)
else:
print ("%d is not a palindrome!" % a)
The output of the above example is:
请输入一个数字: 12321 12321 是一个回文数!Other extensions
Python 100 Examples