In this tutorial, we will learn how to **split a number into digits** using Python. This can be useful in various scenarios, such as when you want to analyze individual digits of a number or convert the number into a list of digits.

We will look at a few different methods to achieve this, and by the end of this tutorial, you will be able to split a number into digits using Python.

### Method 1: Using List Comprehension

List comprehension is a concise way of creating lists in Python. We can use it to split a number into digits by converting the number to a string and then creating a list with the integer value of each character.

Here are the steps to split a number into digits using list comprehension:

- Convert the number to a string.
- Use a list comprehension to create a list with the integer values of each character in the string.

Here’s an example:

1 2 3 4 5 6 7 8 9 10 11 |
# input number number = 12345 # convert number to string number_str = str(number) # split the number into digits and store them in a list digits = [int(char) for char in number_str] # output the list of digits print(digits) |

[1, 2, 3, 4, 5]

### Method 2: Using Integer Division and Modulo Operator

We can split a number into digits using integer division (//) and modulo operator (%) without converting the number to a string. The modulo operator finds the remainder after division and can be used to extract the least significant digit of a number. Integer division can be used to remove the least significant digit of the number.

Here are the steps to split a number into digits using integer division and modulo operator:

- Initialize an empty list to store the digits.
- Find the least significant digit of the number using the modulo operator.
- Append the least significant digit to the list of digits.
- Remove the least significant digit of the number using integer division.
- Repeat steps 2-4 until the number becomes 0.

Here’s an example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# input number number = 12345 # initialize an empty list to store the digits digits = [] # loop until the number becomes 0 while number: # find the least significant digit digit = number % 10 # append the digit to the list of digits digits.append(digit) # remove the least significant digit number //= 10 # reverse the list of digits digits.reverse() # output the list of digits print(digits) |

[1, 2, 3, 4, 5]

## Conclusion

In this tutorial, we learned two methods to split a number into digits in Python: using list comprehension and using integer division and modulo operator. Both methods have their advantages, and you can choose the one you find more convenient or suitable for your use case. Overall, splitting numbers into digits is a handy technique to have in your Python toolkit.