Use range. In Python 2, it returns a list directly:
>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
In Python 3, range is an iterator. To convert it to a list:
>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.
To increment by steps of 0.5, consider using numpy's arange() and .tolist():
>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
See: How do I use a decimal step value for range()?
Answer from Jared on Stack OverflowUse range. In Python 2, it returns a list directly:
>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
In Python 3, range is an iterator. To convert it to a list:
>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.
To increment by steps of 0.5, consider using numpy's arange() and .tolist():
>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
See: How do I use a decimal step value for range()?
You seem to be looking for range():
>>> x1=11
>>> x2=16
>>> range(x1, x2+1)
[11, 12, 13, 14, 15, 16]
>>> list1 = range(x1, x2+1)
>>> list1
[11, 12, 13, 14, 15, 16]
For incrementing by 0.5 instead of 1, say:
>>> list2 = [x*0.5 for x in range(2*x1, 2*x2+1)]
>>> list2
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]
Videos
You can just construct a list from the range object:
my_list = list(range(1, 1001))
This is how you do it with generators in python2.x as well. Typically speaking, you probably don't need a list though since you can come by the value of my_list[i] more efficiently (i + 1), and if you just need to iterate over it, you can just fall back on range.
Also note that on python2.x, xrange is still indexable1. This means that range on python3.x also has the same property2
1print xrange(30)[12] works for python2.x
2The analogous statement to 1 in python3.x is print(range(30)[12]) and that works also.
In Pythons <= 3.4 you can, as others suggested, use list(range(10)) in order to make a list out of a range (In general, any iterable).
Another alternative, introduced in Python 3.5 with its unpacking generalizations, is by using * in a list literal []:
>>> r = range(10)
>>> l = [*r]
>>> print(l)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Though this is equivalent to list(r), it's literal syntax and the fact that no function call is involved does let it execute faster. It's also less characters, if you need to code golf :-)