EASY WAY TO CHECK THE NUMBER IS PALIN DROME OR NOT IN PYTHON

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.")
Python

Line-by-Line Explanation

  1. 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 return True if the number is a palindrome and False otherwise.
  2. 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 is 121, it is converted to the string "121".
  3. 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".
  4. 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.
  5. return True and return False
    • If the condition in the previous line is True, the function returns True, indicating the number is a palindrome.
    • Otherwise, it returns False, indicating the number is not a palindrome.
  6. 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 using int().
    • Example: If the user inputs 121, it will be stored as an integer 121.
  7. if is_palindrome(number):
    • This line calls the is_palindrome() function, passing the input number number as an argument.
    • The function will return either True or False based on whether the number is a palindrome.
  8. print(f"{number} is a palindrome.") and print(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.
Python

Conclusion

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top