A palindrome number in Python is a number that remains the same when its digits are reversed. This guide explains how to check a palindrome number using all popular methods with clear examples, optimized explanations, and interview-ready code.
Example of palindrome numbers: 121, 131, 2442
Example of non-palindrome numbers: 123, 456
If the reversed number is equal to the original number, it is called a palindrome number.
Table of Contents
Palindrome Number Formula
The palindrome number formula follows these steps:
- Store the original number
- Reverse the number digit by digit
- Compare the reversed number with the original number
- If both are equal, the number is a palindrome
This formula works for all palindrome number programs in Python.
Palindrome Number in Python Using While Loop
This is the most commonly used method in exams and interviews.
num = 121
original = num
reverse = 0
while num > 0:
reverse = reverse * 10 + num % 10
num //= 10
if original == reverse:
print("Palindrome number")
else:
print("Not a palindrome number")
- Uses arithmetic logic
- Does not convert number to string
Palindrome Number in Python Using For Loop
This converts the number into a string and reverses it manually.
num = 12321
s = str(num)
reverse = ""
for ch in s:
reverse = ch + reverse
if s == reverse:
print("Palindrome number")
else:
print("Not a palindrome number")
Palindrome Number in Python Using Function
Using a function is the cleanest way to write a palindrome number program in Python.
def is_palindrome(num):
return str(num) == str(num)[::-1]
print(is_palindrome(1331))
- Reusable code
- Best for projects
Palindrome Number in Python Without Using Function
This relies only on loops and conditions.
num = 1441
rev = 0
n = num
while n > 0:
rev = rev * 10 + n % 10
n //= 10
if num == rev:
print("Palindrome number")
else:
print("Not a palindrome number")
Palindrome Number in Python Using If Else
num = 101
if str(num) == str(num)[::-1]:
print("Palindrome number")
else:
print("Not a palindrome number")
Using if else is simple and beginner-friendly.
Palindrome Number in Python Using Slicing
Using slicing is the fastest and most Pythonic method.
num = 9889
if str(num) == str(num)[::-1]:
print("Palindrome number")
else:
print("Not a palindrome number")
Palindrome Number in Python With User Input
User input allows dynamic input from the user.
num = int(input("Enter a number: "))
original = num
reverse = 0
while num > 0:
reverse = reverse * 10 + num % 10
num //= 10
if original == reverse:
print("Palindrome number")
else:
print("Not a palindrome number")
Simple Palindrome Number Program in Python
n = 121
print("Palindrome" if str(n) == str(n)[::-1] else "Not Palindrome")
This is a simple palindrome number program in Python.
Time and Space Complexity of Palindrome Number in Python
| Method | Time Complexity | Space Complexity |
|---|---|---|
| While loop | O(n) | O(1) |
| For loop | O(n) | O(n) |
| Slicing | O(n) | O(n) |
| Function | O(n) | O(n) |
Edge Cases in Palindrome Number Program
- Negative numbers ❌ (not palindrome)
- Zero (0) ✅ palindrome
- Single-digit numbers ✅ palindrome
Common Mistakes While Writing Palindrome Number Program in Python
Even though checking a palindrome number in Python looks simple, beginners often make mistakes. Avoiding these errors improves accuracy and interview performance.
1. Forgetting to Store the Original Number
When using a while loop, the original number gets modified. Always store it before reversing.
2. Incorrect Handling of Negative Numbers
Negative numbers are not palindrome numbers because the minus sign is not mirrored. Many beginners forget to handle this case.
3. Mixing String and Integer Logic
Some programs partially convert to string and partially use arithmetic logic, causing bugs. Choose one approach.
4. Ignoring Edge Cases
Always consider:
- 0
- Single-digit numbers
- Very large numbers
Palindrome Number in Python Without Converting to String (Advanced)
Interviewers sometimes ask for a palindrome number program in Python without string conversion.
def is_palindrome_number(num):
if num < 0:
return False
original = num
reverse = 0
while num > 0:
reverse = reverse * 10 + num % 10
num //= 10
return original == reverse
print(is_palindrome_number(1221))
This solution is:
- Interview-safe
- Efficient
- Based purely on arithmetic logic
Palindrome Number in Python for Competitive Programming
In competitive programming, efficiency matters. The while loop method is preferred because it:
- Uses O(1) extra space
- Avoids string conversion
- Works well for large inputs
Example:
num = int(input())
rev = 0
n = num
while n > 0:
rev = rev * 10 + n % 10
n //= 10
print("YES" if num == rev else "NO")
Difference Between Palindrome Number and Palindrome String in Python
| Feature | Palindrome Number | Palindrome String |
|---|---|---|
| Data type | int | str |
| Logic | Arithmetic or string | String only |
| Example | 121 | “madam” |
Understanding this difference helps in exams and interviews.
Real Interview Question Example
Question: Write a Python program to check whether a number is palindrome or not using a while loop.
Answer:
num = int(input("Enter number: "))
original = num
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
if original == rev:
print("Palindrome number")
else:
print("Not a palindrome number")
This is one of the most frequently asked Python interview questions.
100+ Python Commands List With Examples PDF (Ultimate Guide)
Frequently Asked Questions
What is the easiest way to check a palindrome number in Python?
The easiest way is using string slicing:
str(num) == str(num)[::-1]
Can a negative number be a palindrome in Python?
No, negative numbers are not considered palindrome numbers.
Is palindrome number important for Python interviews?
Yes, palindrome number problems are frequently asked in Python interviews.
Can zero be a palindrome number?
Yes, 0 is considered a palindrome number.
Is palindrome number important for coding interviews?
Yes, it is a very common beginner-level and screening interview question.
