Python program to check if number is palindrome (one-liner)
Sometimes, we have an application of checking a number is palindrome or not and it’s quite common while day-day programming or competitive programming, it’s easy to reverse a number and check it but sometimes for readability and reducing lines of code, we require to perform this in one-liner logic. Let’s discuss certain ways in which this can be achieved.
Input : test_number = 12321
Output : TrueInput : test_number = 1234
Output : False
Method #1 : Using math.log() + recursion + list comprehension
The combination of above three functions can perform this particular task easily, the logs function extracts the number of digits which is powered by 10 to get the number for that iteration for comparison. The process is recurred to test for palindrome.
# Python3 code to demonstrate # checking a number is palindrome # using math.log() + recursion + list comprehension import math # the recursive function to reverse def rev(num): return int(num != 0) and ((num % 10) * \ (10**int(math.log(num, 10))) + \ rev(num // 10)) # initializing number test_number = 9669669 # printing the original number print ("The original number is : " + str(test_number)) # using math.log() + recursion + list comprehension # for checking a number is palindrome res = test_number == rev(test_number) # printing result print ("Is the number palindrome ? : " + str(res)) |
The original number is : 9669669 Is the number palindrome ? : True
Method #2 : Using str() + string slicing
This can also be done by converting the number into a string and then reversing it using the string slicing method and comparing it, truth of which returns the answer to it.
# Python3 code to demonstrate # checking a number is palindrome # using str() + string slicing # initializing number test_number = 9669669 # printing the original number print ("The original number is : " + str(test_number)) # using str() + string slicing # for checking a number is palindrome res = str(test_number) == str(test_number)[::-1] # printing result print ("Is the number palindrome ? : " + str(res)) |
The original number is : 9669669 Is the number palindrome ? : True
Recommended Posts:
- Python program to check if a string is palindrome or not
- Python program to check if given string is vowel Palindrome
- Python Program for How to check if a given number is Fibonacci number?
- Python Program to check Armstrong Number
- Python Program to Check Prime Number
- Python program to check whether a number is Prime or not
- Python Program for Check if all digits of a number divide it
- Python | Check if binary representation is palindrome
- Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Python Program for Efficient program to print all prime factors of a given number
- Python program to check if string is empty or not
- Python program to check if a given string is Keyword or not
- Python Program to check if given array is Monotonic
- Python program to check the validity of a Password
- Python program to check if given string is pangram
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



