Table of Contents
In this example, you will learn to reverse a number.
To understand this example, you should have the knowledge of the following Python programming topics:
1. 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
4321
In this program, while loop is used to reverse a number as given in the following steps:
- First, the remainder of the
numdivided by 10 is stored in the variabledigit. Now, thedigitcontains the last digit ofnum, i.e. 4.digitis 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_numcontains0 * 10 + 4 = 4.numis then divided by 10 so that now it only contains the first three digits: 123. - After second iteration,
digitequals 3, reversed equals4 * 10 + 3 = 43andnum = 12. - After third iteration,
digitequals 2, reversed equals43 * 10 + 2 = 432andnum = 1. - After fourth iteration,
digitequals 1, reversed equals432 * 10 + 1 = 4321andnum = 0. - Now
num = 0, so the test expressionnum != 0fails and while loop exits. reversed already contains the reversed number 4321.
2. 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.
Related posts:
Python Program to Find Hash of File
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Multiple Inheritance
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Convert String to Datetime
Python Program to Count the Number of Occurrence of a Character in String
Python Decorators
Deep Learning with Python - Francois Cholletf
Python del Statement
Python Deep Learning Cookbook - Indra den Bakker
Python String rjust()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Find the Sum of Natural Numbers
Python Exception Handling Using try, except and finally statement
Python Program to Count the Number of Digits Present In a Number
Python Program to Concatenate Two Lists
Python Program to Differentiate Between del, remove, and pop on a List
Python Set issubset()
Python Closures
Python Dictionary fromkeys()
Python Generators
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Delete an Element From a Dictionary
Python Program to Create a Long Multiline String
Python oct()
Python String isspace()
Python sorted()
Python setattr()
Node.js vs Python for Backend Development
Python Program to Compute the Power of a Number
Python Program to Access Index of a List Using for Loop