Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Palindrome Number

A palindrome is a word that has the same meaning when read backwards as when read from the front. Examples of palindromes are: Anna, madam, level, racecar and many more.
Mathematics also knows palindromes. Here we talk about palindromes if the numbers do not change when the sequence of numbers is reversed, for example 22 or 141.

Check Palindrome number

This code checks if a given number is a palindrome or not. A palindrome number is a number that remains the same when its digits are reversed. For example, 121 is a palindrome number because it remains the same after its digits are reversed.

Code Example 1

# Initialize the reversed number to 0
rev = 0

# Prompt the user to enter a number and convert the input to an integer
x = int(input("Enter number to check palindrome or not: "))

# Store the original number in a temporary variable
temp = x

# Loop to reverse the digits of the number
while x > 0:
    # Extract the last digit of the number
    r = x % 10
    
    # Append the last digit to the reversed number
    rev = rev * 10 + r
    
    # Remove the last digit from the original number
    x = x // 10

# Check if the original number is equal to the reversed number
if temp == rev:
    # If true, print that the number is a palindrome
    print(str(temp) + " is palindrome number")
else:
    # If false, print that the number is not a palindrome
    print(str(temp) + " is not palindrome number")

Output

Enter number to check palindrome or not: 262
262 is palindrome number

Enter number to check palindrome or not: 13
13 is not palindrome number

Code Explanation

Initialize Variables

rev = 0
x = int(input("Enter number to check palindrome or not: "))
temp = x
  1. Initialize rev: rev = 0 initializes a variable rev to 0. This variable will store the reversed number.
  2. Input Number: x = int(input("Enter number to check palindrome or not: ")) prompts the user to enter a number, converts the input to an integer, and assigns it to the variable x.
  3. Store Original Number: temp = x stores the original value of x in a temporary variable temp. This is necessary to compare the original number with its reversed version later.

Reverse the Number

while x > 0:
    r = x % 10
    rev = rev * 10 + r
    x = x // 10
  1. While Loop: while x > 0: initiates a loop that continues as long as x is greater than 0. This loop is used to reverse the digits of x.
  2. Extract Last Digit: r = x % 10 extracts the last digit of x by taking the remainder of x divided by 10. This digit is stored in r.
  3. Build Reversed Number: rev = rev * 10 + r appends the extracted digit r to the reversed number rev. Initially, rev is 0, so the first digit becomes r. In subsequent iterations, the current rev is multiplied by 10 (shifting its digits to the left) and the new digit r is added to it.
  4. Remove Last Digit: x = x // 10 removes the last digit from x by performing integer division by 10. This effectively shifts all digits of x one place to the right.

Check for Palindrome

if temp == rev:
    print(str(temp) + " is palindrome number")
else:
    print(str(temp) + " is not palindrome number")
  1. Comparison: if temp == rev: checks if the original number temp is equal to the reversed number rev.
  2. Palindrome Check:
    • If temp is equal to rev, the original number is a palindrome. The message "<number> is palindrome number" is printed.
    • Otherwise, if temp is not equal to rev, the original number is not a palindrome. The message "<number> is not palindrome number" is printed.

Check Palindrome string

Code Example 1

This code checks if a given string s is a palindrome or not. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

# Assign the string "anna" to the variable s
s = "anna"

# Check if the string s is equal to its reverse
if s == s[::-1]:
    # If true, print that the string is a palindrome
    print(s + " is a palindrome")
else:
    # If false, print that the string is not a palindrome
    print(s + " is not a palindrome")

Output

anna is a palindrome

Code Explanation

Initialize String

s = "anna"

s = "anna" assigns the string “anna” to the variable s. This is the string we will check to determine if it is a palindrome.

    Check for Palindrome

    if s == s[::-1]:
        print(s + " is a palindrome")
    else:
        print(s + " is not a palindrome")
    • if s == s[::-1]: checks if the string s is equal to its reverse.
    • s[::-1] is a slicing operation that reverses the string s. Here’s how slicing works:
      • s[start:stop:step]: By default, start is 0, stop is the length of the string, and step is 1.
      • s[::-1] means “start from the end of the string and move backwards with a step of -1”, effectively reversing the string.
    • If s is equal to its reverse (s[::-1]), then s is a palindrome.
    • If s is not equal to its reverse, then s is not a palindrome.

    Print Result

    1. Print Result:
      • If the condition s == s[::-1] is True, the code executes print(s + " is a palindrome"), printing that the string is a palindrome.
      • If the condition s == s[::-1] is False, the code executes print(s + " is not a palindrome"), printing that the string is not a palindrome.