I’ve written a very simple piece of code:
””
a=[]
for i in range(1,5):
b = i**3
a.append(b)
for i in range(1,5):
print(a[i])
””
when I run this, I get the output 8 27 64 and the error message IndexError: list index is out of range.
Basically, a[5] is showing up as an error since it hasn’t been defined in the previous loop, which terminated at i=4
My question is, given that the second loop is also exiting at i=4, why is a(5) being checked for at all?
Also, I’m getting no error message for i=1. Is this because NULL has been assigned to a(1)?
Thanks
1