Borislav Hadzhiev
Last updated: Jul 3, 2022
Photo from Unsplash
To sum the digits of a number:
str()
class to convert the number to a string.int()
class.sum()
function.num = 135 result_1 = sum(int(digit) for digit in str(num)) print(result_1) # 👉️ 9 # -------------------------------------------------- result_2 = sum(map(int, str(num))) print(result_2) # 👉️ 9 # --------------------------------------------------- # ✅ sum the digits of a number without converting to string result_3 = 0 while num: result_3 += num % 10 print(result_3) num = num // 10 print(result_3) # 👉️ 9
The first example uses the str()
class and a generator expression to sum the
digits in a number.
num = 135 result_1 = sum(int(digit) for digit in str(num)) print(result_1) # 👉️ 9
Strings are iterable and integers are not, so we have to convert the integer to a string.
On each iteration, we convert the digit that is wrapped in a string to an integer and return the result.
The last step is to pass the generator object to the sum()
function.
The sum function takes an iterable, sums its items from left to right and returns the total.
The second example uses the map()
function instead of a generator expression
for the conversion.
num = 135 result_2 = sum(map(int, str(num))) print(result_2) # 👉️ 9
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
map
function calls the int()
class with each digit in the string and returns a map
object that we can directly pass to the sum()
function.Alternatively, you can use a while
loop.
To sum the digits of a number:
0
.while
loop to iterate while the number variable is truthy.num % 10
and add it to the sum
variable.10
.num = 135 result = 0 while num: result += num % 10 num = num // 10 print(result) # 👉️ 9
We used a while
loop to iterate while the num
variable stores a truthy
value.
0
is a falsy value, so as soon as the num
variable gets set to 0
, the condition is no longer met.We used the modulo %
operator to get the rightmost digit of the number.
The modulo (%) operator returns the remainder from the division of the first value by the second.
print(135 % 10) # 👉️ 5 print(13 % 10) # 👉️ 3
Once we reassign the result
variable to its current value plus the rightmost
digit of the number, we have to remove the rightmost digit.
We can remove the rightmost digit of a number by using floor division //
with
10
.
print(135 // 10) # 👉️ 13 print(13 // 10) # 👉️ 1
Division /
of integers yields a float, while floor division //
of integers
result in an integer.
The result of using the floor division operator is that of a mathematical
division with the floor()
function applied to the result.
Eventually 1
gets floor-divided by 10
, which returns 0
, and the while
loop stops iterating.
num = 135 result = 0 while num: result += num % 10 num = num // 10 print(result) # 👉️ 9 print(num) # 👉️ 0
num
variable on each iteration and eventually sets it to 0
.If you need to keep the variable around, make sure to make a copy.
num = 135 # 👇️ make copy of num num_copy = num result = 0 while num_copy: result += num_copy % 10 num_copy = num_copy // 10 print(result) # 👉️ 9 print(num_copy) # 👉️ 0 print(num) # 👉️ 135