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 - Decrementing for loops - Stack Overflow
What does "for i in range" mean in Python?
Python: for i in range (1, 10), print (i). Why does it not print the number 10?
Videos
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
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:
So, I had a question where I was supposed to find the sum of the first natural numbers using Python.
Here's the problem:
Write a program to find the sum of the cubes of the first n natural numbers, where the value of n is provided by the user.
And this is the code that my professor used to allow the interpreter to produce the result:
= int(input("Enter a number:"))
sum = 0
for i in range (n+1): sum = sum + i*i*i
# for i in range starts as a loop, and then tries to get to (n + 1), whatever that may be
print("the sum of the first", n, "integers is", sum)
However, I can't seem to understand what "for i in range (n + 1): sum = sum + i * i * i" means. In other words, I don't understand what role this part of the code is doing to produce the result. Especially, I don't understand what the role of "for i in range (n + 1)" is doing. Does anyone mind explaining this to me?