python - Pythonic way to iterate through a range starting at 1 -
currently if want iterate 1
through n
use following method:
for _ in range(1, n+1): print(_)
is there cleaner way accomplish without having reference n + 1
?
it seems odd if want iterate range ordinally starting @ 1, not uncommon, have specify increase 1 twice:
- with
1
@ start of range. - with
+ 1
@ end of range.
range(1, n+1)
not considered duplication, can see might become hassle if going change 1
number.
this removes duplication using generator:
for _ in (number+1 number in range(5)): print(_)
Comments
Post a Comment