Palindrome Number Program in Python
Below are the following ways by which we can check if the number is palindrome or not in Python in one line:
- Using math.log() + recursion + list comprehension
- Using str() + string slicing
- Using user input + string slicing
- Using all() and zip()
Palindrome Program using math.log() + recursion + list comprehension
In this example, we are using math.log(), recursion, and list comprehension to check if the number is palindrome or not. Here, the logs function extracts the number of digits which is powered by 10 to get the number for that iteration for comparison and then recursion test for palindrome.
Python3
import math def rev(num): return int (num ! = 0 ) and ((num % 10 ) * \ ( 10 * * int (math.log(num, 10 ))) + \ rev(num / / 10 )) test_number = 9669669 print ( "The original number is : " + str (test_number)) res = test_number = = rev(test_number) print ( "Is the number palindrome ? : " + str (res)) |
The original number is : 9669669 Is the number palindrome ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Python Check Palindrome Using str() + string slicing
In this example, we are converting the number into a string and then reversing it using the string slicing method and comparing it whether the number is palindrome or not
Python3
# initializing number test_number = 9669669 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 ] print ( "Is the number palindrome ? : " + str (res)) |
The original number is : 9669669 Is the number palindrome ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Python Palindrome Number using user input + string slicing
In this example, we are taking user input in string and then check if the number is palindrome or not.
Python3
num = input ( "Enter a number" ) if num = = num[:: - 1 ]: print ( "Yes its a palindrome" ) else : print ( "No, its not a palindrome" ) |
Time Complexity: O(n)
Auxiliary Space: O(1)
Palindrome Program in Python Using all() and zip()
In this example, we are using a generator expression with the zip() function and the all() function to check whether the number is palindrome or not. The generator expression (a == b for a, b in zip(str(12321), reversed(str(12321)))) generates a generator that returns True if the elements a and b are equal, and False otherwise. The all function returns True if all of the elements in the generator are True, and False otherwise. In this case, it would return True because all of the elements in the generator are True.
Python3
# Using the all function and a generator # expression to check if a number is a palindrome print ( all (a = = b for a, b in zip ( str ( 12321 ), reversed ( str ( 12321 ))))) # prints True print ( all (a = = b for a, b in zip ( str ( 1234 ), reversed ( str ( 1234 ))))) # prints False |
True False
Time complexity: O(n), where n is the length of the number.
Space complexity: O(1)
Python program to check if number is palindrome (one-liner)
In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it’s a Palindrome number otherwise it would be False. Let’s discuss how to find whether a number is palindrome or not in this article.
Input1: test_number = 12321
Output1: True
Input2: test_number = 1234
Output2: False