Palindrome program in python using function

Palindrome program in python using function : The palindrome in python program language is using the while loop function and the recursion function. As the consideration of any number in python language if the string remains is same when we reverse it is called the palindrome. The number is said as the palindrome number if the reverse is the same as the original number. The task in this program is to check the number is palindrome or not.
Solution ( Palindrome program in python using function )

  1. Reading the given input number using input () or the raw_input ().
  2. Then check value entered is the integer or not.
  3. We have to convert the number into the string as str (num).
  4. Then the slice operation is used as [start: end: step] and leave start and empty the step of -1 to reverse the string.
  5. Then check the reverse is equal to num.
  6. If the reverse is equal to num the number is palindrome.
  7. When reverse is equal to num the number is not a palindrome.

Program:-

num=input (‘Enter any number: ’)
try:
val=int (num)
if num==str (num) [::-1]:
print(‘The given number is PALINDROME’)
else:
print(‘The given number is NOT a PALINDROME’)
except ValueError:
print(“That’s not a valid number, Try Again!”)
Output:-
Enter any number: 595
The given number is PALINDROME
Enter any number: 549
The given number is NOT a PALINDROME

Explanation:-

  1. The first step is entering the value of integer and stores it in the variable.
  2. Then the value is stored in another temporary variable.
  3. The next step is that use while loop and last digit of numbers us obtained by modulus operator.
  4. Then the last digit is stored at 1s place and second last at the 10’s place and so on.
  5. The last digit is removed by dividing it with number (10).
  6. The loop is terminated when the value of the number is 0.
  7. The reverse number is compared with integer and stored in a temporary variable.
  8. Then if both are equal the number is said as the palindrome number.
  9. If both are not equal the number is said as not a palindrome number.
  10. The result is printed and then the program is exited.

1) Using the math.log () +recursion+list compression:-

The combination of 3 functions can perform the task easy and the function will extract the number of digits powered by 10 to get number for the iteration and the process is used to test the palindrome of number.
Example:-
import math
defrev (num):
return int(num!=0)and((num%10)*\(10**int(msth.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))
Output:-
The original number is: 9669669
Is the number palindrome?: True

2) Using the str() +string slicing:-

The string slicing is done by converting the number into string and then reverses it using the string slicing method then comparing it and return value.
Example:-
Test_number=9669669
Print(“The original number is:”+str (test_number))
Res=str (test_number) ==str (test_number) [::-1]
Print(“Is the number palindrome?:”+str (res))
Output:-
The original number is: 9669669
Is the number palindrome?: True

 

131 the number is considered as the palindrome number because of the number
Remains the same after reversing it.
We have the application to check the number whether it is palindrome or not.
The process of doing the reverse of the number is easy and checks it for the readable, reducing code to operate.
The palindrome is the number or the string that does not affect (unchanged) if it is reversed.
Example:-12321
Output:-
                  The answer is the palindrome number.
 Example:-RACECAR
Output:-
                   It is also the palindrome string.
The reverse string and grew with the string and called as the palindrome.

3) The program of palindrome using while loop:-

The easy way to find the palindrome of the program used by the while loops and checks the number or the string.
Example:-
n=int (input (“Enter a number”))
temp=n
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print(“The number is palindrome!”)
else:
print(“Not a palindrome!”)
Output:-
 Enter a number: 121
The number is palindrome!

4) Iterative method:-

The loop starts to length/2 and checks the first character to last of string and second to the second last one and so on.
Example:-
sef is palindrome (str):
for i in xrange (0, len (str)/2):
if str[i]! =str[len(str)-i-1]:
return false
return true
s=”malayalam”
ans=ispalindrome(s)
if(ans):
print(“yes”)
else:
print(“No”)
Output:-
Yes

5) Palindrome using the in-build function to reverse the string:-

The predefined function is “”.join (reversed (string)) is used here to reverse the string.
Example:-
def is palindrome(s):
rev=’’.join (reversed(s))
if(s==rev):
return True
return false
s=”malyalam”
ans=ispalindrome(s)
if(ans):
print(“yes”)
else:
print(“No”)
Output:-
                       Yes

6. Method using the extra variable:-

The method used to take the character of string one by one and then store it in an empty variable.
Then after storing characters the user will compare both strings and check is it palindrome or not.
Example:-
x=”Malayalam”
w=”’
for i in x:
w=i+w
if(x==w):
print(“YES”)
Output:-
Yes

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India