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
1
at the start of the range. - With the
+ 1
at the end of the range.
n+1
evaluates once, so this is not adirty
way. As an option, if you don't liken+1
as therange
parameter, evaluate it earlier and save the result to a variable, then use this variable in therange
call.1
) and the end (n+1
) of the range. You're not specifying an increase/increment at alln
is 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+=1
because a range is indexed from0
, sorange(2) == [0,1]
, etc.