Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate numbers with 3 digits

I wanted to generate numbers starting from 000 to 120 in sequence. I know you can generate numbers from 0 to 120 by using a loop.But I want all the numbers to have 3 digits.

The output should be

000
001
002 
...
...
120

Instead of

0
1
2
...
...
120

Is there any easy way to achieve this in python or should I be making separate code for 0-9 10-99 and 100-120 ?

like image 352
Stormvirux Avatar asked Mar 06 '14 04:03

Stormvirux


People also ask

What are lucky 3 digit numbers?

Lucky primes3, 7, 13, 31, 37, 43, 67, 73, 79, 127, 151, 163, 193, 211, 223, 241, 283, 307, 331, 349, 367, 409, 421, 433, 463, 487, 541, 577, 601, 613, 619, 631, 643, 673, 727, 739, 769, 787, 823, 883, 937, 991, 997, ... (sequence A031157 in the OEIS).

How do I generate a random 3 digit number in Excel?

Select the cells in which you want to get the random numbers. In the active cell, enter =RAND() Hold the Control key and Press Enter. Select all the cell (where you have the result of the RAND function) and convert it to values.

How many ways can you make a 3 digit number?

Therefore in that set of 720 possibilities, each unique combination of three digits is represented 6 times. So we just divide by 6. 720 / 6 = 120. That's your answer.


2 Answers

["{0:03}".format(i) for i in range(121)]

or

["%03d" % i for i in range(121)]

To print:

print "\n".join(<either of the above expressions>)

More terse in Python 3.6+:

[f"{i:03}" for i in range(121)]
like image 133
mshsayem Avatar answered Sep 24 '22 01:09

mshsayem


python 3.6

for i in range(121):
    print(f'{i:03}')
like image 31
raphaelts Avatar answered Sep 22 '22 01:09

raphaelts