Hướng dẫn reverse for loop python

Example 1: Reverse a Number using a while loop

num = 1234
reversed_num = 0

while num != 0:
    digit = num % 10
    reversed_num = reversed_num * 10 + digit
    num //= 10

print("Reversed Number: " + str(reversed_num))

Output

Nội dung chính

  • Example 1: Reverse a Number using a while loop
  • Example 2: Using String slicing
  • Reverse a Number in Python using For Loop
  • Python Program to Reverse a Number using For Loop
  • Program to Reverse a Number using For Loop in Python
  • How do you print a reverse number in for loop in Python?
  • How do I print a list of numbers in reverse order in Python?
  • How do you print a list in reverse order in Python while loop?

4321

In this program, while loop is used to reverse a number as given in the following steps:

  1. First, the remainder of the num divided by 10 is stored in the variable digit. Now, the digit contains the last digit of num, i.e. 4.
    digit is then added to the variable reversed after multiplying it by 10. Multiplication by 10 adds a new place in the reversed number. One-th place multiplied by 10 gives you tenth place, tenth gives you hundredth, and so on. In this case, reversed_num contains 0 * 10 + 4 = 4.
    num is then divided by 10 so that now it only contains the first three digits: 123.
  2. After second iteration, digit equals 3, reversed equals 4 * 10 + 3 = 43 and num = 12.
  3. After third iteration, digit equals 2, reversed equals 43 * 10 + 2 = 432 and num = 1.
  4. After fourth iteration, digit equals 1, reversed equals 432 * 10 + 1 = 4321 and num = 0.
  5. Now num = 0, so the test expression num != 0 fails and while loop exits. reversed already contains the reversed number 4321.

Example 2: Using String slicing

num = 123456
print(str(num)[::-1])

Output

654321

Using the string slicing concept, you can get reverse the string. ::-1 corresponds to start:stop:step. When you pass -1 as step, the start point goes to the end and stop at the front.

Hướng dẫn reverse for loop python

We will develop a program to reverse a number in python using for loop. To reverse a number we need to extract the last digit of the number and add that number into a temporary variable with some calculation, then remove the last digit of the number. Do these processes until the number becomes zero.

Example of reverse number:-
Number: 49813
Reverse Number: 31894

We will take integer numbers as a string while declaring the variables. Then, find the reverse of the number using for loop. Finally, the result will be displayed on the screen.

Program Description:- Write a program to reverse a number in Python

# Python program to reverse a number using for loop

# take inputs
num = '105'

# calculate reverse of number
reverse = ''
for i in range(len(num), 0, -1):
   reverse += num[i-1]

# print reverse of number
print('The reverse number is =', reverse)

Output:-

The reverse number is = 501

Reverse a Number in Python using For Loop

In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.

# Python program to reverse a number using for loop

# take inputs
num = input('Enter the number: ')

# calculate reverse of number
reverse = ''
for i in range(len(num), 0, -1):
   reverse += num[i-1]

# print reverse of number
print('The reverse number is =', reverse)

Output for the input values test-case-1:-

Enter the number: 984061
The reverse number is = 160489

Output for the input values test-case-2:-

Enter the number: 49815
The reverse number is = 51894

Python Program to Reverse a Number using For Loop

We can also take the help of a function to reverse a number in python using for loop. A function is a block of code that performs a specific task.

# Python program to reverse a number using for loop

def findReverse(n):  #user-defined function
   # calculate reverse of number
   reverse = ''
   for i in range(len(n), 0, -1):
      reverse += n[i-1]
   return reverse

# take inputs
num = input('Enter the number: ')

# calling function and display result
reverse = findReverse(num)
print('The reverse number is =', reverse)

Output:-

Enter the number: 765496
The reverse number is = 694567

Program to Reverse a Number using For Loop in Python

We can also take the help of a built-in function to reverse a number in python using for loop. Python reversed() method returns an iterator that accesses the given sequence in the reverse order.

# Python program to reverse a number using for loop

# take inputs
num = input("Enter a number: ")

# calculate reverse of number
print('The reverse number is:')
for i in reversed(num):
   print(i, end='')

Output:-

Enter a number: 980456
The reverse number is:
654089

How do you print a reverse number in for loop in Python?

Example 1: Reverse a Number using a while loop.

First, the remainder of the num divided by 10 is stored in the variable digit . ... .

After second iteration, digit equals 3, reversed equals 4 * 10 + 3 = 43 and num = 12 ..

After third iteration, digit equals 2, reversed equals 43 * 10 + 2 = 432 and num = 1 ..

How do I print a list of numbers in reverse order in Python?

In Python, there is a built-in function called reverse() that is used to revers the list. This is a simple and quick way to reverse a list that requires little memory. Syntax- list_name. reverse() Here, list_name means you have to write the name of the list which have to be reversed.

How do you print a list in reverse order in Python while loop?

To reverse a list using a while loop you will first of all need to check the condition if the index number more than equal to zero. If not then add list[index number] to a new empty list then deduct one from the index number. This is done until the while loop conditional statement is false leaving a reversed list.