Python Iterate Over Digits in Integer

Python Iterate over Digits in Integer | Iteration is a term that refers to the process of going through each item one by one. Iteration occurs whenever you use an explicit or implicit loop to run over a set of elements.

This article will help you learn how to iterate over digits in number in python. We will use three different methods to achieve our expected result and we will also look at different examples which will help you understand the concept better.

Example 1:
Digits = 1432

Output:
1
4
3
2

Example 2:
a = [9,8,7,6]

Output:
9 8 7 6

Let’s see how to make this possible using the following methods.

Python Iterate Over Digits in Integer using for loop

Iterables are objects in Python that may be walked via one item at a time using a for loop. It is not possible to iterate all objects; for example, we might not be able to iterate an integer because it is a solitary value.

Approach:

1. We first defined the string x.
2. Initialized the for….in loop.
3. Each number is accessed by going through the digits.
4. Final output.

Example 1:– Python iterate over digits in integer using for loop

x = 78912
for digit in str(x):
   print(int(digit))

Output:-

7
8
9
1
2

Example 2:- If the input is user-defined

num = int(input("Enter a number: "))
for digit in str(num):
   print(f'{int(digit)}')

Output:-

Enter a number: 454615
4
5
4
6
1
5

Iterate Over Digits In Number Python using While Loop

In the previous program, we have seen a solution using for loop. Now let us see a program for python iterate over digits in integer using the while loop.

Program to Iterate Over Digits In Number Python using While Loop

from math import log

def number(n):
   x = int(log(n, 10).real)
   factor = 10**x
   while n:
      yield int(n/factor)
      n = n % factor
      try:
         x, old_x = int(log(n, 10).real), x
      except ValueError:
         for _ in range(x):
            yield 0
         return
      factor = 10**x

for z in number(7657):
   print(z)

Output:-

7
6
5
7

Iterate Through Digits Of A Number Python Using recursion

The recursive method can also be used to iterate over digits in number python. Recursion is a means of defining a process that involves a call to itself. The recursion feature helps to break down a difficult problem into smaller, more manageable chunks. Divide and conquer is a very well computer coding strategy.

Program:- Python iterate over digits in integer using recursion

def number(n):
   # if n is a zero stop the recursion
   if(n == 0):
      return
   # Call the function recursively for n // 10
   # which calls for the remaining numbet
   # after getting rid of the last digit
   number(n // 10)
   print(n % 10)


if __name__ == "__main__":
   number(4598)

Output:-

4
5
9
8

This brings us to the end of this article where we learned how to iterate over digits in number python using for loop, while loop, and recursion.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *