If I print(range(start, 101, 3))
, it doesn't [return an array]. Why?
Among the changes that Python 3 made from Python 2 was an emphasis on generators rather than lists or tuples. A list takes up memory for every item in the list, while a generator only creates one item at a time, whenever needed. A generator thus almost always uses less memory, a precious resource in a program. Also, since a generator needs to create only one item (at most) immediately after it is created and the other items may never be needed, it can also save a great deal of time compared to a list which needs to create all its items whether they are needed or not.
Thus many things that made a list in Python 2 were changed to create a generator instead. range
is one of those items. So range is an object, but the items are created only when asked for in one way or another. So the command print(range(start, 101, 3))
creates the range object then prints the object. Python prints a note that basically says that the range
is an object, a special range
object, and gives a few more details.
As @ShadowRanger points out in a comment (and I meant to include but forgot), range
is more than a generator. It can work as a sequence (you can pull one item out from the middle, not just the beginning) and has other special characteristics. That is why your command to print a range
does not just show that it is an object, it shows that it is a range
object which differs from other objects. I was speaking in general terms when I wrote of "generators"--there are other objects that are similar.
Are there any ways the range
function prints an array without using list()
?
If you want to print a range
as a list, ask for the items. There are multiple ways:
print(*range(start, 101, 3))
That uses Pythons "splat" operator *
to get the items of the range
in a form suitable as parameters to a function--in this case, the print
function. This method comes the closest to never using a "list" to show the contents of the range
. The printout is not an "array", just the items separated by spaces.
print([*range(start, 101, 3)])
This uses splat to create a list, then prints the list.
print(list(range(start, 101, 3))
That uses apparent type-casting to convert the range to a list immediately so the list can be printed. You said you do not want this method, but I show it for completeness.
print(tuple(range(start, 101, 3))
This uses a tuple rather than a list.
print([v for v in range(start, 101, 3)])
That uses a list comprehension to create the list. By adding an if
clause in the comprehension you could get just some few desired items from the list. You could use a set or other kind of comprehension if you don't want a list.
lst = []
lst += range(start, 101, 3)
print(list)
For details on this one, see the end of this answer.
lst = []
for v in range(start, 101, 3):
lst.append(v)
print(lst)
You could use a loop to get a list to print. There are many variations on this loop that would work.
How does this code lst += range(start, 101, 3)
turn range
into an array of numbers?
It doesn't. Using the += command on a list uses whatever is on the right side of the operator as an iterable, not a list. Your code picks the items one-by-one out of the range and appends them one-by-one to the list. The range is never converted to a list, it is just used to extend a list. You can see that by trying to execute lst = lst + range(start, 101, 3)
--the range is not converted to a list so an error results.. See this link for a closely related question, though it talks about tuples rather than ranges.