A simple python program which checks whether a number is a palindrome or not.
Generally, a number is said to be a palindrome number if its reverse is same as the original number.
For Example, 121 is a palindrome as its reverse is also 121 whereas, 231 is not a palindrome as its reverse is 132.
You can watch the video on YouTube here
Palindrome number – Code Visualization
To check whether a number is palindrome or not
- Read an input number using
- Check whether the value entered is integer or not.
- We convert that integer number to string
- Now we use advanced slice operation
[start:end:step]leaving start and empty and giving step a value of -1, this slice operation reverses the string.
- Now check whether reverse is equal to num,
reverseis equal to
num, the number is palindrome
reverseis not equal to
num, it is not a palindrome
num = input('Enter any number : ')
val = int(num)
if num == str(num)[::-1]:
print('The given number is PALINDROME')
print('The given number is NOT a palindrome')
print("That's not a valid number, Try Again !")
Want to be strong at OOP in Python? If yes, I would suggest you to take the course below.
Object Oriented Programming in Python