Last updated: Apr 10, 2024
Reading timeยท3 min
To find the ones, tens, hundreds and thousands:
str()
class to convert the number to a string.number = 3217 # โ Get ones, tens, hundreds and thousands ones = number % 10 print(ones) # ๐๏ธ 7 tens = (number % 100) // 10 print(tens) # ๐๏ธ 1 hundreds = (number % 1000) // 100 print(hundreds) # ๐๏ธ 2 thousands = number // 1000 print(thousands) # ๐๏ธ 3 # ----------------------------------------------------- # โ Get ones, tens, hundreds and thousands in a list result = [int(digit) for digit in str(number)[::-1]] print(result) # ๐๏ธ [7, 1, 2, 3]
The first examples use math to get the ones, tens, hundreds and the thousands digits of a number.
The modulo (%) operator returns the remainder from the division of the first value by the second.
print(10 % 2) # ๐๏ธ 0 print(10 % 4) # ๐๏ธ 2
If the value on the right-hand side is zero, the operator raises a
ZeroDivisionError
exception.
For example, to get the ones, we calculate the remainder of dividing the number by 10.
number = 3217 ones = number % 10 print(ones) # ๐๏ธ 7 print(3217 - 321 * 10) # ๐๏ธ 7
We used floor division in the next examples.
/
of integers yields a float, while floor division //
of integers results 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.
my_num = 50 print(my_num / 5) # ๐๏ธ 10.0 (float) print(my_num // 5) # ๐๏ธ 10 (int)
For example, to get the thousands, we floor-divide the number by 1000
.
number = 3217 thousands = number // 1000 print(thousands) # ๐๏ธ 3
To get the tens, we calculate the remainder of dividing the number by 100
and
divide the result by 10.
number = 3217 print(number % 100) # ๐๏ธ 17 tens = (number % 100) // 10 print(tens) # ๐๏ธ 1
Similarly, to get the hundreds, we calculate the remainder of dividing the
number by 1000
and divide the result by 100
.
number = 3217 ones = number % 10 print(number % 1000) # ๐๏ธ 217 hundreds = (number % 1000) // 100 print(hundreds) # ๐๏ธ 2
Alternatively, you can convert the number to a string and use a list comprehension.
number = 3217 result = [int(digit) for digit in str(number)[::-1]] print(result) # ๐๏ธ [7, 1, 2, 3]
We used the str() class to convert the number to a string and used string slicing to reverse the string.
The syntax for string slicing is my_str[start:stop:step]
.
We specified a step
of -1
to reverse the string and used a list
comprehension to iterate over the reversed string.
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
The list contains the digits of the ones, tens, hundreds and thousands.
Alternatively, you can use a while
loop to divide the number by 10 until it
gets set to 0
.
number = 3217 result = [] while number != 0: result.append(number % 10) number = number // 10 print(result) # ๐๏ธ [7, 1, 2, 3]
We calculate the digits of the ones, tens, hundreds and thousands and
simultaneously floor-divide the number by 10
until it gets set to 0
.
You can learn more about the related topics by checking out the following tutorials: