+ 5

# numbers = list(range(3, 8)) print(numbers) >>> [3, 4, 5, 6, 7] ... Why is 8 not included there if its a part of the range, yet 3 is included in there?

maybe I'm failing to understand it but is it like that when you range from set number to set number instead of numbers = list(range(8)) print(numbers) >>> [0, 1, 2, 3, 4, 5, 6, 7, 8] help me understand this someone please. thanks.

5 Answers

+ 3

I had the same question as you. I google it, and in the link below there is the logical explanation, why the notation is better.
That paper was written by E.W Dijkstra, considered one of the most inffluent people on computer science founding generation.
In that paper also explains why is better to start indexing in 0 rather than one. Is amazing!! and it's just a few lines!!
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

+ 2

It is bcoz upper limit is not included and range will give the difference of numbers . mathematically represented as [3,8)

+ 2

range(3,8) represents a range beggining in 3 that has 8-3=5 elements in it: [3,4,5,6,7] hope this helps

+ 1

I think in the range with the two arguments the first number is a value and the second represent the index of the last element in the range.

+ 1

for i in range(a,b)
means for i going from a to b
for example for i in range(3,8): do somthg
i=3 do smthg
i=4 do smthg
i=5 do smthg
i=6 do smthg
i=7 do smthg
i=8 okay we've reached 8 the loop is done.. we have 'done something' 5 times