for n in range(x,y) ?? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: for n in range(x,y) ?? (/thread-15494.html) |
for n in range(x,y) ?? - MuntyScruntfundle - Jan-19-2019 Why does >> for n in range(1,5) give 4 iterations? >> for n in range(0,5) give 4 iterations? >> for n in tange(1,6) give 5 iterations? Surely this makes little sense. Do I need to set a base number somewhere like you used to have to in Basic many years ago? Thanks. RE: for n in range(x,y) ?? - stullis - Jan-19-2019 The iterator returned by range() starts at either 0 or the start number provided. It stops when the index equals the end number. for n in range(1,5): print(n) # is equivalent to n = 1 while 1 <= n < 5: print(n) n += 1range(0,5) returns five iterations from 0 through 4. A common way of returning the end number of a range is to increase the end number by 1; instead of range(0,5), use range(0,6). RE: for n in range(x,y) ?? - DeaD_EyE - Jan-19-2019 Slicing and the range function has following rule: Start is inclusive Stop is exclusive |