Several things to note:
- Python is zero-indexed
This explains theIndexErrorwhen executingmyList[6]because the last element is #5 not #6. range(start, stop)is "front-inclusive" and "end-exclusive"
So while iterating, you printmyList[0]but notmyList[6], which is why you did not get anIndexErrorhere.range(n)
nrepresents the number of times to execute the for loop, so because you would like to print 6 elements, you should dorange(len(myList))without-1.range(start, stop, step)
Again, Python's for loops are "front-inclusive" and "end-exclusive" so you should start with the index of the last element (len(myList)-1) and end with the index of the first element minus 1 (-1). Step size can be thought of as the "displacement" while iterating, so with a step size of-1, you decrease the iteratoriby one at each epoch.
Hope this helped!
Answer from ccl on Stack OverflowSeveral things to note:
- Python is zero-indexed
This explains theIndexErrorwhen executingmyList[6]because the last element is #5 not #6. range(start, stop)is "front-inclusive" and "end-exclusive"
So while iterating, you printmyList[0]but notmyList[6], which is why you did not get anIndexErrorhere.range(n)
nrepresents the number of times to execute the for loop, so because you would like to print 6 elements, you should dorange(len(myList))without-1.range(start, stop, step)
Again, Python's for loops are "front-inclusive" and "end-exclusive" so you should start with the index of the last element (len(myList)-1) and end with the index of the first element minus 1 (-1). Step size can be thought of as the "displacement" while iterating, so with a step size of-1, you decrease the iteratoriby one at each epoch.
Hope this helped!
The first prameter is inclusive, whereas the second parameter is exclusive. Which means if a range function is called like range(a,b) it would result in values between [a,b). Therefore when you use range(0, len(myList)), this loops through 0..len(myList)-1.
As you would have guessed by now, you need to subtract 1 while looping in reverse order because the last index of the array is len(myList) -1 and you are missing a because 0 is not included. Therefore, if you wish to iterate in reverse order, you could do range(len(myList)-1, -1, -1).
What's the difference between for i in list: vs for i in range(len(list)):
What does "for i in range" mean in Python?
Is range(len(list)) ever Pythonic?
Loops - for i and for j in range(n) explained
Sorry this may seem like a super python noob question, but I just started learning and I am stuck on this idea. Why is it that sometimes one makes the code work but not the other? What's the difference between them and can someone explain it in a way that a beginner can understand?
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?