Check Palindrome Number in Python
What you’ll learn
- What a palindrome number is.
- How to reverse digits using
% 10and// 10. - How to print palindrome numbers from
100to200.
Overview
A palindrome number has mirrored digits. If reversing the digits gives the same number, it is a palindrome.
Single check
Check values like 121 and 12321.
Range scan
Print all palindromes between 100 and 200.
Live preview
Test a nonnegative integer instantly.
Prerequisites
while loops, integer arithmetic, and modulo operation.
- Basics of Python functions with
def. - Knowing that
% 10gives last digit and// 10removes it.
The idea
Take the last digit, append it to a reversed value, and remove it from the original number. Repeat until no digits remain.
If reversed value equals original starting value, the number is palindrome.
Live preview
Enter a nonnegative integer and check whether it is palindrome.
Algorithm
Goal: check whether decimal digits of a number are symmetric.
Save original number
Keep input value before modifying it.
Reverse digits with loop
Use digit = n % 10, then rev = rev * 10 + digit, and n //= 10.
Compare
If original == reversed, it is palindrome.
📜 Pseudocode
function is_palindrome(n):
original = n
reversed = 0
while n != 0:
reversed = reversed * 10 + (n mod 10)
n = floor(n / 10)
return original == reversed Check one number
Use arithmetic reversal and compare with original.
def is_palindrome(number: int) -> bool:
original_number = number
reversed_number = 0
while number != 0:
remainder = number % 10
reversed_number = reversed_number * 10 + remainder
number //= 10
return original_number == reversed_number
number = 121
if is_palindrome(number):
print(f"{number} is a palindrome number.")
else:
print(f"{number} is not a palindrome number.") Explanation
The loop reconstructs the number in reverse order and compares it with the original saved value.
Palindromes from 100 to 200
Reuse the same helper inside an inclusive range loop.
def is_palindrome(num: int) -> bool:
original_num = num
reversed_num = 0
while num != 0:
remainder = num % 10
reversed_num = reversed_num * 10 + remainder
num //= 10
return original_num == reversed_num
print("Palindrome numbers in the range 100 to 200:")
for i in range(100, 201):
if is_palindrome(i):
print(i, end=" ") Explanation
Python range uses exclusive end, so use 201 to include 200.
Notes
No overflow issue in Python ints. Python integers can grow beyond 32-bit limits.
String alternative. You can also check palindrome using text comparison, but arithmetic method is a classic interview approach.
❓ FAQ
🔄 Input / output examples
Try these values with the single-value helper.
| Value | Palindrome? |
|---|---|
0 | Yes |
7 | Yes |
121 | Yes |
123 | No (321) |
Edge cases
120 and 21
Reversal removes leading zeros, so 120 is not palindrome.
n < 10
Always palindrome for nonnegative numbers.
Define behavior clearly
This page rejects negative values in live preview for beginner clarity.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
is_palindrome(n) | O(d) | O(1) |
Range [a, b] | O((b-a+1) * d) | O(1) |
Summary
- Core trick: reverse digits and compare with original.
- Range scan: reuse same helper for each candidate.
- Watch-outs: trailing zeros and negative input rules.
The word palindrome also describes words like “radar” or “level”. For integers, we compare digits only, so single-digit values like 7 always pass.
8 people found this page helpful
