Python Palindrome Number Program

Write a Palindrome Number Program in Python using While Loop, Functions, Recursion, lists, list comprehensions, and lambda expression code. We also print the list of palindrome numbers from 1 to N and 1 to 100. Any number could be Palindrome if it remained the same when we reversed it. For example, 131, 959, and 45654 because they remain the same after reversing them.

The common program approach to check for the Palindrome Number in Python is to reverse a given number. Compare the original value with the reverse value. If they exactly matched, then it is a Palindrome number. Otherwise, it is not.

In Python programming language, there is a wide array of approaches to reverse the digits in a number, including the iterative approach, functions, and recursive approach. In these programs, we can use the Python If Else statement to compare the original with the reverse and check or identify the palindrome number. Remember to preserve the original number by allocating it to the temporary value and reversing the temp value.

Python Palindrome Number Program using While Loop

This program allows the user to enter any integer value. Next, this Python program code uses While Loop to check whether a given number is Palindrome or Not.

# using While Loop
number = int(input("Please Enter any Value: "))

reverse = 0
temp = number

while(temp > 0):
    Reminder = temp % 10
    reverse = (reverse * 10) + Reminder
    temp = temp //10
 
print("Reverse of it is = %d" %reverse)

if(number == reverse):
    print("%d is a Palindrome" %number)
else:
    print("%d is Not" %number)
Palindrome Number Program in Python using While loop

User Entered values in this palindrome program in Python code are Number = 191 and Reverse = 0
Temp = Number
Temp = 191

While Loop First Iteration
Reminder = Temp %10
Reminder = 191 % 10 = 1

Reverse = Reverse *10 + Reminder => 0 * 10 + 1 = 1

Temp = Temp //10 = 191 /10
Temp = 19

Second Iteration: From the first while loop Iteration, the values changed as Temp = 19 and Reverse = 1

Reminder = 19 % 10 = 9

Reverse = 1 * 10 + 9 = 19

Temp = 19 /10 = 1

Third Iteration: From the Second Iteration of the program, the values of Temp = 1 and Reverse = 19

Reminder = 1 % 10 = 1

Reverse = 19 * 10 + 1 = 191

Temp = 1/10 = 0

Here Num = 0. So, the Python while loop condition fails.

if ( Number == Reverse ) – condition checks whether the user entered number is exactly equal to the Reverse or not. If this condition is True, then it is. Else it is not.

Python Program to Check Palindrome Number using for loop

This program is the same as the while loop. However, the for loop iterates the number by converting it to a string.

num = int(input("Enter any Value: "))
rev = 0
temp = num

for _ in range(len(str(num))):
    rem = num % 10
    rev = (rev * 10) + rem
    num //= 10
if temp == rev:
    print('Palindrome')
else:
    print("Not")
Python Palindrome Number Program using for loop

Python Program for Palindrome Number using string slice code

In this program, str(num) converts the user-entered number to a string and [::-1] slices or reverses the string. Next, int(str(num)[::-1]) will convert the reserved string to a number.

n = int(input("Enter any Value: "))
rev = int(str(n)[::-1])

if n == rev:
    print('Palindrome')
else:
    print("Not")
Enter any Value: 145541
Palindrome

Alternatively, you can compare the strings. First, convert the number to a string and then compare it with the reverse of it.

num = int(input("Enter any Value: "))

strNum = str(num)

if strNum == strNum[::-1]:
    print('Palindrome')
else:
    print("Not") 
Enter any Value: 1243
Not

Enter any Value: 19791
Palindrome

Python Program to Check Palindrome Number Using Functions

In this program, we defined an isPalindrome function. Within that function, we used the while loop. Next, the If Else statement is used to check the actual equal inverse.

# using Functions
def isPalindrome(val):
    inverse = 0
    
    while(val > 0):
        Remi = val % 10
        inverse = (inverse * 10) + Remi
        val = val // 10
    return inverse

val = int(input("Please Enter any Num: "))

rev = isPalindrome(val)
print("Inverse = %d" %rev)

if(val == rev):
    print("%d is a Palindrome" %val)
else:
    print("%d is not" %val)
Please Enter any Num: 1441
Inverse = 1441
1441 is a Palindrome

Please Enter any Num: 1202
Inverse = 2021
1202 is not

Python Program to Check Palindrome Number using Recursion

In this program code, we check whether a given number is Palindrome or Not using the Recursive Functions concept.

# using Recursive Functions
rev = 0
def isPalindrome(num):
    global rev
    
    if(num > 0):
        Reminder = num % 10
        rev = (rev * 10) + Reminder
        isPalindrome(num // 10)
    return rev


num = int(input("Please Enter any Num: "))

rev = isPalindrome(num)
print("Reverse = %d" %rev)

if(num == rev):
    print("%d is a Palindrome" %num)
else:
    print("%d is not" %num)
Python Palindrome Number Program using recursion

In this program, within the checks using recursion function declaration,

integer_rev (Num//10) – it helps to call the function Recursively with the updated value. If you miss this statement, it terminates after completing the first line. For example, Num = 191, then the output is 1

Let’s see the If condition.

if (num > 0) check whether it is greater than 0 or not. For Recursive functions, placing a condition before using the function recursively is very important. Otherwise, we end up in infinite execution (Same as an infinite Loop).

Python Palindrome Number program using Lists

This example creates an empty list. For each iteration, it appends the last digit to a list.

num = int(input("Please Enter any Value: "))

numList = []
temp = num

while temp > 0:
    numList.append(temp % 10)
    temp //= 10

reverse = numList[::-1]

if num == reverse:
    print(num, " is a Palindrome.")
else:
    print(num, "is Not.")
Enter any Value: 19791
Palindrome

Python Program for Palindrome Number using List Comprehension

This program uses list comprehension.

  1. str(num) – The str() function converts the num integer to a string.  
  2. [int(i) for i in str(num)] – The list comprehension reads each individual digit and creates a list of those digits. Here, int(i) converts each character back to a digit.
  3. digits[::-1] – It reverses the list using the slicing technique. You can also try the list reverse function.
  4. The if statement checks whether each digit equals the reverse of it.
num = int(input("Enter any Num: "))

digits = [int(i) for i in str(num)]

if digits == digits[::-1]:
    print('Palindrome')
else:
    print("Not")

Python Program to Check Palindrome Number using lambda function

This code uses the lambda expression and str() function.

num = int(input("Enter any Value: "))

isPalindrome = lambda num: str(num) == str(num)[::-1]

if isPalindrome(num):
    print('Palindrome')
else:
    print("Not")
Python Program to Check Palindrome Number using lambda function

Python program to print Palindrome Numbers from 1 to N

This section explains how to write a Python Program to print Palindrome numbers from 1 to 100 or 1 to n or minimum to maximum with an example.

This program allows you to enter the maximum value and prints the palindrome numbers from 1 to that number.

maximum = int(input(" Please Enter the Maximum Value : "))

print("Between 1 and %d are : " %maximum)
for num in range(1, maximum + 1):
    temp = num
    rv = 0
    
    while(temp > 0):
        Reminder = temp % 10
        rv = (rv * 10) + Reminder
        temp = temp //10

    if(num == rv):
        print("%d " %num, end = '  ')
 Please Enter the Maximum Value : 4554
Between 1 and 4554 are : 
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   202   212   222   232   242   252   262   272   282   292   303   313   323   333   343   353   363   373   383   393   404   414   424   434   444   454   464   474   484   494   505   515   525   535   545   555   565   575   585   595   606   616   626   636   646   656   666   676   686   696   707   717   727   737   747   757   767   777   787   797   808   818   828   838   848   858   868   878   888   898   909   919   929   939   949   959   969   979   989   999   1001   1111   1221   1331   1441   1551   1661   1771   1881   1991   2002   2112   2222   2332   2442   2552   2662   2772   2882   2992   3003   3113   3223   3333   3443   3553   3663   3773   3883   3993   4004   4114   4224   4334   4444   4554 

Python Program to print Palindrome numbers from 1 to 100 using While Loop

This Python program allows the user to enter the maximum limit value. Next, this program prints palindrome numbers from 1 to the user-entered value. First, we used For Loop to iterate a loop between 1 and the maximum value. within the for loop,

  • We used While Loop to reverse the given number.
  • The if statement checks whether the given number is a Palindrome Number or Not. For this, it compares the original value with the reverse number.

TIP: I suggest you refer to the Reverse a Number article to understand this Python program logic.

maximum = int(input(" Please Enter the Maximum Value : "))

print("Palindrome Numbers between 1 and %d are : " %(maximum))
for num in range(1, maximum + 1):
    temp = num
    reverse = 0
    
    while(temp > 0):
        Reminder = temp % 10
        reverse = (reverse * 10) + Reminder
        temp = temp //10

    if(num == reverse):
        print("%d " %num, end = '  ')

Palindrome numbers from 1 to 1000 output

 Please Enter the Maximum Value : 1000
Palindrome Numbers between 1 and 1000 are : 
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   202   212   222   232   242   252   262   272   282   292   303   313   323   333   343   353   363   373   383   393   404   414   424   434   444   454   464   474   484   494   505   515   525   535   545   555   565   575   585   595   606   616   626   636   646   656   666   676   686   696   707   717   727   737   747   757   767   777   787   797   808   818   828   838   848   858   868   878   888   898   909   919   929   939   949   959   969   979   989   999  

Python Program to Display Palindrome Numbers from 1 to N Using Functions

In this Python program, we allow users to enter the minimum and maximum values. Next, it prints palindrome numbers between the minimum and maximum values.

minimum = int(input(" Please Enter the Minimum Value : "))
maximum = int(input(" Please Enter the Maximum Value : "))

print("Palindrome Numbers between %d and %d are : " %(minimum, maximum))
for num in range(minimum, maximum + 1):
    temp = num
    reverse = 0
    
    while(temp > 0):
        Reminder = temp % 10
        reverse = (reverse * 10) + Reminder
        temp = temp //10

    if(num == reverse):
        print("%d " %num, end = '  ')
Python Program to print Palindrome numbers from 1 to 100 N