I have such a list:
for i in [100, 1000, 10000]:
print(i)
How could I reproduce it with range
for i in range(100, 10000, 100)
print(i)
the above code does not work as expected.
I have such a list:
for i in [100, 1000, 10000]:
print(i)
How could I reproduce it with range
for i in range(100, 10000, 100)
print(i)
the above code does not work as expected.
You are printing increasing powers of ten, so you can do this:
>>> for i in range(2, 5):
... print(pow(10, i))
...
100
1000
10000
As Graham observes in the comments, you can also do
>>> for i in range(2, 5):
... print(10 ** i)
...
100
1000
10000
if you prefer the **
notation for exponentiation.
With a single line:
print(*(10 ** n for n in range(2, 5)), sep='\n')
Not the *
operator, which is used to unpack the tuple. The **
finds powers of 10, and sep denotes the string which is put between elements print
outputs.
As @Walter notes in the comments, this method is not particularly efficient for larger ranges. @snakecharmerb's method with a for loop is the recommenced choice in those cases.
There are a couple of options
import numpy as np
for i in np.logspace(2, 4, num = 3, endpoint = True, dtype = np.int):
print(i)
or (edit based on @Graham comment)
for i in (10**k for k in range(2, 5)):
print(i)