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
?
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).
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.
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.
["{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)]
python 3.6
for i in range(121):
print(f'{i:03}')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With