# .css-df1pn7{display:block;width:16rem;}     # How you can use the Python function to check whether the number is palindrome or not?

Vipul kunwar
·Mar 10, 2022·

Subscribe to my newsletter and never miss my upcoming articles

• Python function for palindrome
• Benefits of the Python function
• Conclusion:

Everybody in the world has some functionality. The function gives credibility.
Likewise, in Python function executes a block of code in one call. That means you can execute a bunch of statements at one time.
Today, I’m going to show you a Python program for palindrome.

A palindrome is a word or numbers that are the same from starting and end.

From the end, I’ve remembered that I’ve checked the palindrome word using Python for loop in the previous blog.

In this blog, you’re going to see how to check number is palindrome or not using the Python function.
Here python function will execute statements such as, reversing the number and comparing the number is palindrome or not.

Not only we’ll reverse the number but also check for palindrome.

## Python function for palindrome

You can create a function using the def keyword.
The def with function name make a function. For example,

``````def reverse(num):
``````

In the above example, num is an argument that we’re going to pass as a number.
The number will give by the user.
To reverse the number we need to execute a set of statements inside the Python function.

The first statement is `rev = 0` which shows we haven’t reversed the number until.
Second, I’m storing user_input to the temp variable because the number is going to change.
But we need to compare the original number with the reversed number.

### Create a while loop to reverse the string

Third to reverse the number I’m going to execute the while loop.
While loop executes until a certain condition is true.
Inside the while loop, we’ll get the last digit of the number.

``````while num > 0:
# Getting the last digit
digit = num % 10
``````

After getting the last digit we’ll move it to 10s place using the below line of code.

``````       rev = rev*10+digit
``````

When we’ll move the last digit to the 10s place then the user number will move from a bigger place to a smaller place. You can code using `num = num//10.` So, let’s see how our Python while loop looks:

``````while num > 0:
# Getting the last digit
digit = num % 10
# Move the place of digit to get reverse number
rev = rev*10+digit
# number places will decrease
num = num//10
``````

So, we’ve reversed the user number.

### Compare the number with reverse number

Next thing is that we need to compare it using an if-else statement. Here’s how:

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

We’ve created the Python function to execute the set of statements.
To run a program we need to call it.

Before calling the function I’m going to take a number as user input.

You can do this with the input() method. This number is going to pass through the function.
Here’s how:

``````
user_input = int(input("Enter a number: "))
reverse(user_input)
``````

### Run the Python program

Let’s see the whole code for the Python.

``````
def reverse(num):
# rev variable. It's a reverse number
rev = 0
temp = user_input

while num > 0:
# Getting the last digit
digit = num % 10
# Move the place of digit to get reverse number
rev = rev*10+digit
# number places will decrease
num = num//10

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

user_input = int(input("Enter a number: "))
reverse(user_input)
``````

When you’ll run the program you’ll get the Output something as below:

``````>>> %Run num.py
Enter a number: 121
121 is a palindrome!
``````

From the above example, you can see that I’ve put the number 121 and Python show us that number is a palindrome.
You can check the different numbers such as 123.
Here’s the example:

``````>>> %Run num.py
Enter a number: 123
123 isn't a palindrome!
``````

In the above example, you can see that 123 isn’t a palindrome because it’s not the same from starting and end.
At last, we’ve finished the palindrome with the python function.

## Benefits of the Python function

Using Python function, you can:--

• Execute the set of tasks in one run.
• Save your time to find specific expressions.
• Get rid of the infinite execution
• Pass the different values.

Thus, you can use the Python function to make your program easy and sequence. The sequence will help you to find the specific variable and entity in the program.

## Conclusion:

Finally, you’ve seen the Python program to check whether the number is palindrome or not. Not only you’ve seen the palindrome program but also executed the palindrome program.

In the palindrome program, I’ve used the Python function to execute the set of instructions.

This set of instructions is used to reverse the number and check whether a number is a palindrome or not.

First I’ve used a while loop to reverse the number.
Second, used the if-else statement to check whether a number is equal to the reverse number or not!

Both steps are inside the function.

Next, you need to call the Python function with user input.

The input() method takes the number as user input. When python runs your program it’ll give the output whether the number is palindrome or not!