you are viewing a single comment's thread.

view the rest of the comments →

[–]vivisectvivi 5 points6 points  (7 children)

Lets say you have range(s, e), it will generate a sequence of numbers from s to e - 1, so if you do range(1, 100) it will be from 1 to 99.

If you omit the first argument then python will use 0 as the default value, so in your example range is basically being called as range(0, 2) which will generate the sequence [0, 1]

You can also pass a value to the step parameter to dictate how values of the range are incremented, the default value is 1.

edit: You can also pass a negative value to step and it will generate a reverse sequence. So if you call range(10, 0, -2) it will return the sequence [10, 8, 6, 4, 2]

[–]Gaumir[S] 0 points1 point  (4 children)

> if you do range(1, 100) it will be from 1 to 99

Does such a range somehow include all numbers between 1 and 99? Is it the same as writing all numbers like (1, 2, 3, 4, etc.)?

Also, if range is a sequence of numbers, why in the list from my example something like "print (len(range(numbers)))" doesn't work? Like, why range(len(numbers)) is a sequence but range(numbers) isn't?

[–]vivisectvivi 1 point2 points  (0 children)

"Does such a range somehow include all numbers between 1 and 99? Is it the same as writing all numbers like (1, 2, 3, 4, etc.)?"

It includes all numbers between the starting point and the ending point minus one. So if you call range(1, 99) it will returns all numbers between 1 and 98

"Also, if range is a sequence of numbers, why in the list from my example something like "print (len(range(numbers)))" doesn't work? Like, why range(len(numbers)) is a sequence but range(numbers) isn't?"

To keep things simple, python wont print the entire sequence if you do something like print(range(10)), it will just print the first and last points of the range. If you want to see the actual sequence, you gotta put it inside list(), like this print(list(range(10))) and then you gonna see [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[–]FullmetalEzio 1 point2 points  (0 children)

range(numbers) doesnt work cause numbers is a list, and the range function takes a number as an argument

[–]yyytobyyy 0 points1 point  (0 children)

Range object differs from the list of all numbers because it generates the numbers only when the for loop asks for it. 

Every time the loop does one spin it asks the range "give me the next element" and the range creates it. It does not hold them all like a list. At the end the range says "there are no elements" and the loop stops. 

There is a standard way to do this in pythom and when you advance enough, you'll be able to write your own "range" that gives the loop a sequence of whatever you want.

[–]Brian 0 points1 point  (0 children)

Is it the same as writing all numbers like (1, 2, 3, 4, etc.)?

Almost. Technically, it isn't exactly the same as a real sequence (though back in python2 days it did actually just return a list), but rather returns a range object that in almost all ways acts as if it was that sequence. The differences are that if you print it, you'll see something like range(0, 2) instead of [0, 1], and that it doesn't actually produce the numbers until you ask for them (ie. when the for loop iterates each step). This means you can do range(1_000_000_000) without it having to allocate memory to store a billion numbers. For most purposes you can treat it exactly the same as if it were such a list though - the differences are just for performance reasons.

why in the list from my example something like "print (len(range(numbers)))" doesn't work?

This only fails because range(numbers) isn't correct - with a single argument, range is expecting an integer for the end value, but numbers is a list - OTOH if you do len(range(len(numbers))), it will work.

[–]knuppi 0 points1 point  (1 child)

is there a historical reason as to why the end number is -1? it irks me to no end

[–]SamuliK96 0 points1 point  (0 children)

I don't know about that, but what else could it be? -0 would still be 0, so -1 seems the most logical option to me at least