Python for loop with a more intuitive upper bound? -
sometimes little confusing me keep in mind upperbound for loop excluded default. there way make inclusive?
yes, for in range(upper + 1) or if like, for in range(lower, upper + 1) work,
a lot of programming languages use zero-based indexing, non-inclusive upper bound common practice (this due memory addressing , adding offset)
just example: if had array of size 5, ar, starting index 0, your largest valid index value 4 (i.e., 0, 1, 2, 3, 4), loop construct refer size of array (5) so:
for in range(5):
or more common , better:
for in range(len(ar)):
.. ensuring legal index values 0 .. 4.
Comments
Post a Comment