Code output not correct


#1
to_21 = range(1,22)
odds = to_21[::2]
middle_third = to_21[7:14]
print to_21
print odds
print middle_third

This is the code i wrote in codecademy console and it gave me the correct output that is a list of numbers, but when i try to run it in a different compiler, it gives
range(1,22)
range(1,22,2)
range(8,15)
can anyone guide me


#2

Different python version, python2’s range creates a list, python3 creates a value that in a lot of ways behaves like a list, but it doesn’t actually contain the numbers, it computes them one at a time when asked for instead (no need to store something that can be trivially computed)

It probably? supports slicing, and you can also create a list from it if you really want one

python2’s xrange behaves like python3’s range (it might even be the same thing under a different name, but I won’t swear by that)


#3

so, if i use that list further ahead in a program, that will work without any errors?


#4

Depends on whether what you ask python makes sense or not, doesn’t it…

Every value supports some particular operations, if you use those and you use them in ways that make sense (empty list doesn’t have a seventh element for example) … then sure?

You can for example not assign to elements of a range object, because there’s no underlying storage unlike a list. But what you probably should have done anyway is to apply a function to each value in the range, and create a new list containing the results. Or if you just mean to change one value, then you’d create a list from the range object and then update that one value


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.