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 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)
Python Palindrome Number Program using While loop

User Entered values in this palindrome program 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")
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

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)
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).

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")

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")