I talked with my data science tutor for almost 20 minutes and he couldn't give me an answer beyond: "It just doesn't give you the last value. It's just something you remember."
python - why for i in range(0,10,-1): wont print anything - Stack Overflow
python - Print range of numbers on same line - Stack Overflow
How to print (for ... in range () ) on the same line instead of spacing to the next lines below?
how can I print one number at a time from a range of numbers?
With a negative "step", python keeps on yielding1 elements while the current value is greater than end. In this case, you start at 0. 0 is not greater than or equal to 10 so python's done and nothing gets yielded.
1This is a simplification of course -- range returns a range object on python3.x which is an indexable sequence type so it doesn't exactly yield, but the basic idea is the same ...
There is no evaluation of the first element by the range() call, and Python's range() function will not return anything if step is negative and start + i * step is not greater than stop. For your example, start = 0 + 0 * -1 is not greater than stop = 10, so your range call returns the empty list, and your for loop has nothing to iterate over.
$ python -c 'print(range(0,10,-1))'
[]
range()'s documentation:
range(stop)
range(start, stop[, step])
This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised). Example:
How to print (for ... in range () )( or using other methods )on the same line instead of spacing or skipping to the next lines below?
For example, I want to print 1 from this range of numbers:
for n in range(1,101):
When you call range() with two arguments, the first argument is the starting number, and the second argument is the end (non-inclusive). So you're starting from len(list_of_numbers), which is 5, and you're ending at 6. So it just prints 5.
To get the results you want, the starting number should be 0, and the end should be len(list_of_numbers)+1. If you call it with one argument, that's the end, and 0 is the default start. So use
for i in range(len(list_of_numbers)+1):
or if you want to pass the start explicitly:
for i in range(0, len(list_of_numbers)+1):
range gives you and iterator between (start, end) end not included.
So in your case the iterator is (start=len(list_of_numbers), end=6).
Since len(list_of_numbers) = 5, this translates to range(5,6) which is 1 element, 5, since 6 is excluded.
https://docs.python.org/3/library/functions.html#func-range