Currently if I want to iterate 1 through n I would likely use the following method:
for _ in range(1, n+1):
print(_)
Is there a cleaner way to accomplish this without having to reference n + 1 ?
It seems odd that if I want to iterate a range ordinally starting at 1, which is not uncommon, that I have to specify the increase by one twice:
- With the
1at the start of the range. - With the
+ 1at the end of the range.
n+1evaluates once, so this is not adirtyway. As an option, if you don't liken+1as therangeparameter, evaluate it earlier and save the result to a variable, then use this variable in therangecall. - user7845401) and the end (n+1) of the range. You're not specifying an increase/increment at all - David Zemensnis improperly assigned. If you correct the assignment ofn(wherever that is, elsewhere in your code) then you can just dofor i in range(1,n):but you may need ton+=1because a range is indexed from0, sorange(2) == [0,1], etc. - David Zemens