Skip to content
Advertisement

Assign a range to a variable

Whenever I try to assign a range to a variable like so:

Var1 = range(10, 50)

Then try to print the variable:

Var1 = range(10, 50)
print(Var1)

It simply prints ‘range(10, 50)’ instead of all the numbers in the range. Why is this?

Advertisement

Answer

Thats because range returns a range object in Python 3. Put it in list to make it do what you want:

>>> Var1 = range(10, 50)
>>> print(list(Var1))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,  
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
>>>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement