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
num
divided by 10 is stored in the variabledigit
. Now, thedigit
contains the last digit ofnum
, 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
contains0 * 10 + 4 = 4
.num
is then divided by 10 so that now it only contains the first three digits: 123. - After second iteration,
digit
equals 3, reversed equals4 * 10 + 3 = 43
andnum = 12
. - After third iteration,
digit
equals 2, reversed equals43 * 10 + 2 = 432
andnum = 1
. - After fourth iteration,
digit
equals 1, reversed equals432 * 10 + 1 = 4321
andnum = 0
. - Now
num = 0
, so the test expressionnum != 0
fails 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 oct()
Python Program to Count the Number of Each Vowel
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Dictionary values()
Python Set difference_update()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Convert Bytes to a String
Python Program to Check Whether a String is Palindrome or Not
Python Input, Output and Import
Python String format()
Python Program to Display Fibonacci Sequence Using Recursion
Python Generators
Python List index()
Python Dictionary fromkeys()
Python all()
Python Namespace and Scope
Python Program to Remove Duplicate Element From a List
Python Set difference()
Python format()
Python pass statement
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Find ASCII Value of Character
Python Program to Find the Largest Among Three Numbers
Python String isdigit()
Python String join()
Deep Learning in Python - LazyProgrammer
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Make a Flattened List from Nested List
Python Shallow Copy and Deep Copy
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Return Multiple Values From a Function