Built with Tutify.io

    AIStudyLabs

    Check Palindrome Number

    Check Palindrome Number

    Introduction

    You will learn about palindrome numbers and how to identify them using Python. A number is considered a palindrome if it remains the same when its digits are reversed. This lesson will build on your existing knowledge of loops, string manipulation, and functions to create a program that checks if a given number is a palindrome.

    Understanding Palindrome Numbers

    A palindrome number is symmetric and reads the same forwards and backwards. For example, 121 and 12321 are palindrome numbers, while 123 and 45 are not.

    Properties of Palindrome Numbers

    String Manipulation for Palindrome Checks

    Using strings in Python allows us to easily reverse a number and compare it. Recall that you can convert integers to strings using the str() function, and strings can be reversed using slicing techniques.

    Example: String Reversal

    number = 121
    str_number = str(number)
    reversed_str = str_number[::-1]
    

    In this snippet, str_number[::-1] reverses the string representation of the number.

    Implementing Loops for Number Reversal

    While string manipulation is useful, understanding the underlying numeric reversal using loops provides a deeper insight into algorithmic thinking. Below is an example of reversing a number using a loop:

    Example: Loop-Based Number Reversal

    def reverse_number(num):
        reversed_num = 0
        while num > 0:
            digit = num % 10
            reversed_num = reversed_num * 10 + digit
            num = num // 10
        return reversed_num
    

    In this example, the loop extracts each digit using modulus and integer division, progressing to form the reversed number.

    Developing a Function to Automate Checks

    We can encapsulate the palindrome check in a function that takes a number as input and returns a boolean indicating whether or not it is a palindrome.

    Example: Palindrome Check Function

    def is_palindrome(num):
        if num < 0:
            return False  # Negative numbers cannot be palindromes by definition
        
        original_num = num
        reversed_num = reverse_number(num)
        return original_num == reversed_num
    

    This function first checks for negative numbers, which cannot be palindromes by definition. It then uses reverse_number to determine if the reversed number is equal to the original.

    Testing the Function

    print(is_palindrome(121))  # Output: True
    print(is_palindrome(-121)) # Output: False
    print(is_palindrome(123))  # Output: False
    

    These examples demonstrate the function's usage to verify if numbers are palindromes.

    Summary / Key Takeaways

    In this lesson, you have learned:

    Master Python by Writing 50 Programs — Number-Based Programs

    Python Basics Programs

    Number-Based Programs

    Loops and Mathematical Programs

    String-Based Programs

    List-Based Programs

    Dictionary and Set Programs

    Chapter 7: File Handling and Real Programs

    Tutorial Not Found