How to reverse a number in Python

In this Python Tutorial, we will learn how to reverse a number in Python using different methods with examples. In addition, we will learn to reverse numbers in Python using for loop.

Reverse Number in Python

Here, we will explore various methods to find the reverse of a number in Python:

Reversing a Number Using a While Loop

A traditional approach to reverse a number involves using a while loop in Python. The steps involved in this process are:

  1. Take the number to be reversed as input.
  2. Initialize a variable to store the reversed number, usually with a value of 0.
  3. In a loop, extract the last digit of the number and append it to the reversed number.
  4. Remove the last digit from the original number.
  5. Continue the loop until the number becomes 0.

Here’s a simple Python code snippet that implements these steps:

def reverse_number(n):
    reversed_number = 0
    
    while n > 0:
        last_digit = n % 10
        reversed_number = reversed_number * 10 + last_digit
        n = n // 10

    return reversed_number

number = int(input("Enter a number: "))
print("Reversed number: ", reverse_number(number))

In this code, the Python function reverse_number uses a while loop to continuously divide the input number by 10 (using floor division to remove the last digit) and append the last digit (found using modulus operation) to reversed_number.

Output:

How to reverse a number in Python

Using Python’s Slicing Technique

Another method to reverse a number involves converting the number to a string and then using Python’s slicing technique. The steps involved in this process are:

  1. Convert the number into a string.
  2. Use slicing to reverse the string.
  3. Convert the reversed string back into a number.
READ:  How to Check Armstrong Number in Python [with 5 Methods]

Here’s a simple Python code snippet that implements these steps:

def reverse_number(n):
    return int(str(n)[::-1])

number = int(input("Enter a number: "))
print("Reversed number: ", reverse_number(number))

In this code, the function reverse_number uses Python’s built-in functions str and int to convert the input number into a string and then back into an integer. The slicing operation [::-1] is used to reverse the string.

Output:

Reverse Number in Python

Reverse Number in Python Using For Loop

Let’s start with the method of reversing a number using a for loop in Python. Here’s the process:

  1. Take the number to be reversed as an input.
  2. Initialize a variable to store the reversed number, usually with a value of 0.
  3. Extract the last digit of the number.
  4. Append the extracted digit to the reversed number.
  5. Remove the last digit from the number.
  6. Repeat steps 3-5 until the number is 0.

Here’s the Python code that follows the above steps:

def reverse_number(n):
    reversed_number = 0

    # Convert the number into a string and find its length
    number_str = str(n)
    length = len(number_str)

    # Use a for loop to reverse the number
    for i in range(length-1, -1, -1):
        last_digit = int(number_str[i])
        reversed_number = reversed_number * 10 + last_digit

    return reversed_number

number = int(input("Enter a number: "))
print("Reversed number: ", reverse_number(number))
  • In the code above, we convert the input number to a string. We then run a Python loop that goes from the last index of the string (which is length-1) to the first index (which is 0), with a step of -1 (going backwards).
  • In each Python iteration, we get the digit at the current index, convert it back to an integer, and append it to the reversed number by multiplying the reversed number by 10 and adding the last digit.
  • The reason for multiplying the reversed number by 10 is to shift the digits of the reversed number one position to the left when a new digit is added. After the Python for loop finishes executing, the function returns the reversed number.
READ:  Python program to print pattern (10 Examples)

Note: You can now input any number to see its reversed form. Remember, this will not work properly with numbers that end with 0, as the leading zeros in the reversed number will be ignored.

Output:

Reverse Number in Python Using For Loop

Conclusion

Python provides multiple ways to reverse a number, each with its own strengths. The traditional while loop method gives you a greater understanding of the arithmetic and logic involved in the process. On the other hand, Python’s slicing technique is a testament to Python’s convenience and flexibility as a programming language

Reversing a number in Python can be done using a variety of techniques, and the for loop method is one of them. This approach is not only useful for reversing numbers, but also forms a fundamental building block for solving more complex problems.

Also, take a look at some more Python tutorials.