Python program to check the number is palindrome or not for the absolute beginner!

Python program to check the number is palindrome or not for the absolute beginner!

I know you ❤️ the numbers that are the same from starting and end.
Many dates which are starting and ending with the same numbers are epic.

a111.png For example,
22/02/2022 is a perfect palindrome number.

In palindrome for words, you’d seen how to check the word is a palindrome or not?
Here, you’re going to see how to check the number is a palindrome or not using Python?
If you’re thinking to understand this program you need some python basics.
So, I’m sure you’re going to learn 20% of the python basics in this blog post.

Before starting this let’s see what's a palindrome in numbers?

What is a Palindrome in numbers?

As you know, a palindrome is a word or numbers that are similar from starting and end.
For example, numbers 121 and 242 are palindrome numbers. Because they are the same from forward and backwards.
The amazing date 22/02/2022 is a palindrome number. It's forward and backward are the same.

So, in this program I’m going to give different numbers and check whether they are palindrome or not using Python.

How to check the palindrome number with Python?

aaa.png

First of all, to check the number is a palindrome or not? You need a number.
You can give the number using the input() method. But as you know, the input() method treat the user input as a string data type. So, for numbers, we’ll convert them into the integer using the int(input()) function.

num = int(input("Enter the number:"))

Second, we’ll create the temp variable because after reversing the number the num value will change. But we need to compare the original number with the reverse number. So, I’m going to store the num variable into temp variable using below code

temp = num

Note: Variables are container that holds the values in Python. The value could be any data-type (string, integer and float)
Third, we’ve to reverse the number. You can reverse a number using a while loop. But the initial reverse number will be 0.

Let’s create a rev variable and store 0 into it.

rev = 0

When we’ll use the while loop to reverse the number, then the reverse value will increase from 0.

Use Python while loop to reverse the number

Python while loop executes until the condition's satisfied.

For example, you can execute a while loop until the number is less than 10.
While loop will terminate when number becomes 10 or greater than 10. As you know, we’ve to reverse the number. That means until the number is greater than 0 while loop reverses the number.

After reversing the number the while loop will finish.
Let’s write some python code for while loop.

while num > 0:
    # get the last--->second_last-->third_last
    digit = num % 10     # digit every time ==2
    rev = rev*10+digit   # 2, 22, 222, 2222, 22222 
    num = num//10        # num = 22222, 2222, 222, 22, 2

    # when num = 0 while loop will terminate

I’ve written the Python while loop. It’s time to understand it step-by-step.

The first step is while num > 0. This line shows that until the number is greater than 0 while loop executes.
I’ve given indentation which is space in another line of the code. The indentation in the code shows that another line belongs to the while loop.
So, the second indented line which is digit = num % 10.

It’ll give us the last then second then third last digit in the reverse number.
The (%)modulus operator in Python gives the remainder which is the last digit of the number.
Third step in while loop is rev = rev*10+digit # 2, 22, 222, 2222, 22222.

rev*10 show us that number is moving from one’s place to tens place and likewise to another place. After moving the number, we need to find the digit that will come in that place. So, we added that digit to rev*10.

Note: * in Python is for Multiplication sign. For example, (2*2 = 4) shows the multiplication of the numbers.

So, we’ve reversed the number and moved one place to another.
We need to perform the decrease operation in the num variable. Because the digits are fixed. The reverse number increase and the number will decrease in the digits.

Here’s how you can do this:

num = num//10        # num = 22222, 2222, 222, 22, 2

The num variable is decreasing every digit place by 10.

Note: 5/2 = 2.5 but 5//2 = 2. The // (double division) remove the float number in Python.

So, we’ve coded the while loop. The while loop reverses the number.

Compare the number with the reverse number

To check the number is palindrome or not? You can use an if-else statement.
Here’s how I’ve done it:

if(temp == rev):
    print(temp, "is a palindrome number!")
else:
    print(temp,"isn't a palindrome number!")

In the above example, you can see that if a given number is equal to the reverse number then if-block executes. Otherwise, else block executes.
Let’s run the program and check whether the number is a palindrome or not!

Let’s run the program!


num = int(input("Enter the number:"))

# we'll create temp variable so, that we can compare original
# number with reverse number
temp = num

# reverse of the num to check the palindrome
rev = 0   

while num > 0:
     # get the last--->second_last-->third_last
     digit = num % 10     # digit every time ==2
     rev = rev*10+digit   # 2, 22, 222, 2222, 22222 
     num = num//10        # num = 22222, 2222, 222, 22, 2

    # when num = 0 while loop will terminate

# we can't compare num with rev because num going to become 0
# when reverse is moving
if(temp == rev):
    print(temp, "is a palindrome number!")
else:
    print(temp,"isn't a palindrome number!")

Output:

Enter the number:121
121 is a palindrome number

I know you're thinking what step-by-step while loop doing? Here's what while loop doing:

aex.png I’m going to check with another number

Enter the number:122
122 isn't a palindrome number!

From the above output, 121 is a palindrome but 122 isn’t a palindrome. So, you’ve seen the Python program to check whether the number is a palindrome or not!
Let’s wrap it all together.

Conclusion:

In this blog, you had seen what is a palindrome number?
Meanwhile, I’ve written a Python program to check it.

First, I’ve taken the user input as a string then convert this string into an integer.
Second, I’d stored this number into the temp variable.

Third, I’ve used a while loop to reverse the number to compare with the number.
Using the if-else statement I’ve compared the number and reversed number.

If both numbers are equal that means they are palindrome. Otherwise, it isn’t.
To check the program, I’ve passed the two numbers 121 and 122.
After running program, 121 is a palindrome and 122 isn’t a palindrome number. This is the ending of the palindrome program.

So, what other way you can do it? Drop below. I’m curious to know.
Do you know I’m producing micro-content on Twitter?
You can get micro-content every day. Check out me on Twitter
image.png

Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!