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):
Answer from Barmar on Stack OverflowWhen 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
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
for loop in Python - Stack Overflow
Python's range function. Why doesn't (1-10) include the integer 10?
For loop syntax in Python without using range() or xrange() - Stack Overflow
Videos
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."
Try using this:
for k in range(1,c+1,2):
You should also know that in Python, iterating over integer indices is bad style, and also slower than the alternative. If you just want to look at each of the items in a list or dict, loop directly through the list or dict.
mylist = [1,2,3]
for item in mylist:
print item
mydict = {1:'one', 2:'two', 3:'three'}
for key in mydict:
print key, mydict[key]
This is actually faster than using the above code with range(), and removes the extraneous i variable.
If you need to edit items of a list in-place, then you do need the index, but there's still a better way:
for i, item in enumerate(mylist):
mylist[i] = item**2
Again, this is both faster and considered more readable. This one of the main shifts in thinking you need to make when coming from C++ to Python.
Hey all, so in my beginner programming attempts I have made a simple number game app in which the user enters a number that is stored as the "secret number" the computer attempts to guess in 10 trys. This number is a number between 1 and 10. So in order to anticipate the potential error of someone entering a number greater than 10, I have made used of the range function. "if secret_number not in numb_range re-enter secret number until number is within range 1-10" basically.
And it worked. except when I entered 10 it would continually ask me to re-enter as if 10 is not in the range 1-10. so when i made numb_range = range(1,11) the problem was solved.
Why is this? Thanks
This is what list slicing is about, you can take part of your list from i'th element through
lst[i:]
furthermore, in order to have both index and value you need enumerate operation, which changes the list into list of pairs (index, value)
thus
for ind, i in enumerate(lst):
for j in lst[ind+1: ]:
#Do Something
It looks like you might want to use enumerate():
for index, item in enumerate(lst):
for j in lst[index+1:]:
#Do Something