Introduction
A palindrome is a number (or word) that reads the same forwards as it does backwards. For instance, numbers like 121
, 1331
, and 12321
are palindromes, while 123
is not. In this program, we’ll write a Python code to check whether a given number is a palindrome or not. We’ll also explain each line of the code so you can understand how it works.
# Function to check if a number is a palindrome
def is_palindrome(n):
# Convert the number to a string
original_number = str(n)
# Reverse the string using slicing
reversed_number = original_number[::-1]
# Check if the original number is equal to the reversed number
if original_number == reversed_number:
return True
else:
return False
# Input: Get the number from the user
number = int(input("Enter a number: "))
# Check if the number is a palindrome and print the result
if is_palindrome(number):
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")
PythonLine-by-Line Explanation
def is_palindrome(n):
- We define a function named
is_palindrome
that takes one argument,n
. This function is responsible for checking if the given number is a palindrome. n
is the number that the user inputs and the function will returnTrue
if the number is a palindrome andFalse
otherwise.
- We define a function named
original_number = str(n)
- This line converts the number
n
into a string format. Since it’s easier to work with strings for reversal, this step is necessary. - Example: If
n
is121
, it is converted to the string"121"
.
- This line converts the number
reversed_number = original_number[::-1]
- This line uses Python’s slicing technique to reverse the string.
[::-1]
is a common way to reverse a string in Python. - Example: If
original_number
is"121"
, the reversed string will still be"121"
.
- This line uses Python’s slicing technique to reverse the string.
if original_number == reversed_number:
- This condition checks if the original string and the reversed string are equal.
- If they are equal, the number is a palindrome, and the function returns
True
.
return True
andreturn False
- If the condition in the previous line is
True
, the function returnsTrue
, indicating the number is a palindrome. - Otherwise, it returns
False
, indicating the number is not a palindrome.
- If the condition in the previous line is
number = int(input("Enter a number: "))
- Here, we ask the user to input a number using the
input()
function. This input is then converted into an integer usingint()
. - Example: If the user inputs
121
, it will be stored as an integer121
.
- Here, we ask the user to input a number using the
if is_palindrome(number):
- This line calls the
is_palindrome()
function, passing the input numbernumber
as an argument. - The function will return either
True
orFalse
based on whether the number is a palindrome.
- This line calls the
print(f"{number} is a palindrome.")
andprint(f"{number} is not a palindrome.")
- If the number is a palindrome, the program prints a message indicating that the number is a palindrome.
- Otherwise, it prints a message saying the number is not a palindrome.
Example Output
Enter a number: 121
121 is a palindrome.
Enter a number: 123
123 is not a palindrome.
PythonConclusion
This Python program checks whether a given number is a palindrome by comparing the original number with its reverse. The use of Python’s slicing feature simplifies the process of reversing the number. By breaking down each line of code, you can understand the functionality and purpose of each part of the program.
This simple yet useful program demonstrates how logic and string manipulation in Python can be combined to solve problems efficiently.
4o